From e9fbcce2205ec343a03a4661abb8231fb7c7dcb9 Mon Sep 17 00:00:00 2001 From: jamesread Date: Wed, 24 Aug 2022 20:52:13 +0100 Subject: [PATCH] fmt: Reduce cyclo complexity --- internal/acl/acl.go | 28 +++++++++++++++++----------- internal/httpservers/restapi.go | 8 ++++++-- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/internal/acl/acl.go b/internal/acl/acl.go index f77a52a..6e58c28 100644 --- a/internal/acl/acl.go +++ b/internal/acl/acl.go @@ -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 { var ret []*config.AccessControlList for _, acl := range cfg.AccessControlLists { - if !slices.Contains(user.acls, acl.Name) { - continue - } - - if acl.AddToEveryAction { + if isACLRelevant(cfg, actionAcls, acl, user) { ret = append(ret, &acl) - continue - } - - if slices.Contains(actionAcls, acl.Name) { - ret = append(ret, &acl) - continue } } diff --git a/internal/httpservers/restapi.go b/internal/httpservers/restapi.go index 3437cf7..1ee5aad 100644 --- a/internal/httpservers/restapi.go +++ b/internal/httpservers/restapi.go @@ -23,8 +23,8 @@ var ( cfg *config.Config ) -func getClaimsFromJwtToken(cookieValue string) (jwt.MapClaims, error) { - token, err := jwt.Parse(cookieValue, func(token *jwt.Token) (interface{}, error) { +func parseToken(cookieValue string) (*jwt.Token, error) { + return jwt.Parse(cookieValue, func(token *jwt.Token) (interface{}, error) { // Don't forget to validate the alg is what you expect: if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok { 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") return []byte(cfg.AuthJwtSecret), nil }) +} + +func getClaimsFromJwtToken(cookieValue string) (jwt.MapClaims, error) { + token, err := parseToken(cookieValue) if err != nil { log.Errorf("jwt parse failure: %v", err)