fmt: Reduce cyclo complexity

This commit is contained in:
jamesread 2022-08-24 20:52:13 +01:00
parent c38b0351e4
commit e9fbcce220
2 changed files with 23 additions and 13 deletions

View File

@ -92,22 +92,28 @@ func buildUserAcls(cfg *config.Config, user *AuthenticatedUser) {
} }
} }
func isACLRelevant(cfg *config.Config, actionAcls []string, acl config.AccessControlList, user *AuthenticatedUser) bool {
if !slices.Contains(user.acls, acl.Name) {
return false
}
if acl.AddToEveryAction {
return true
}
if slices.Contains(actionAcls, acl.Name) {
return true
}
return false
}
func getRelevantAcls(cfg *config.Config, actionAcls []string, user *AuthenticatedUser) []*config.AccessControlList { func getRelevantAcls(cfg *config.Config, actionAcls []string, user *AuthenticatedUser) []*config.AccessControlList {
var ret []*config.AccessControlList var ret []*config.AccessControlList
for _, acl := range cfg.AccessControlLists { for _, acl := range cfg.AccessControlLists {
if !slices.Contains(user.acls, acl.Name) { if isACLRelevant(cfg, actionAcls, acl, user) {
continue
}
if acl.AddToEveryAction {
ret = append(ret, &acl) ret = append(ret, &acl)
continue
}
if slices.Contains(actionAcls, acl.Name) {
ret = append(ret, &acl)
continue
} }
} }

View File

@ -23,8 +23,8 @@ var (
cfg *config.Config cfg *config.Config
) )
func getClaimsFromJwtToken(cookieValue string) (jwt.MapClaims, error) { func parseToken(cookieValue string) (*jwt.Token, error) {
token, err := jwt.Parse(cookieValue, func(token *jwt.Token) (interface{}, error) { return jwt.Parse(cookieValue, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect: // Don't forget to validate the alg is what you expect:
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"]) return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
@ -33,6 +33,10 @@ func getClaimsFromJwtToken(cookieValue string) (jwt.MapClaims, error) {
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key") // hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")
return []byte(cfg.AuthJwtSecret), nil return []byte(cfg.AuthJwtSecret), nil
}) })
}
func getClaimsFromJwtToken(cookieValue string) (jwt.MapClaims, error) {
token, err := parseToken(cookieValue)
if err != nil { if err != nil {
log.Errorf("jwt parse failure: %v", err) log.Errorf("jwt parse failure: %v", err)