refactor: split scope extraction from scope/roles claims into separate functions
@@ -327,10 +327,31 @@ var errRolesValue = fmt.Errorf(errValueMsg, "roles", "roles", "roles")
func (j *JWT) getScopeValues(tokenClaims map[string]interface{}) ([]string, error) {
var scopeValues []string
+ var err error
+
+ scopeValues, err = j.addScopeValueFromScope(tokenClaims, scopeValues)
+ if err != nil {
+ return nil, err
+ }
+
+ scopeValues, err = j.addScopeValueFromRoles(tokenClaims, scopeValues)
+ if err != nil {
+ return nil, err
+ }
+
+ return scopeValues, nil
+}
+
+func (j *JWT) addScopeValueFromScope(tokenClaims map[string]interface{}, scopeValues []string) ([]string, error) {
+ if j.scopeClaim == "" {
+ return scopeValues, nil
+ }
- if j.scopeClaim != "" {
+ scopesFromClaim, exists := tokenClaims[j.scopeClaim]
+ if !exists {
+ return scopeValues, nil
+ }
- if scopesFromClaim, exists := tokenClaims[j.scopeClaim]; exists {
// ["foo", "bar"] is stored as []interface{}, not []string, unfortunately
scopesArray, ok := scopesFromClaim.([]interface{})
if ok {
@@ -350,11 +371,19 @@ func (j *JWT) getScopeValues(tokenClaims map[string]interface{}) ([]string, erro
scopeValues = addScopeValue(scopeValues, s)
}
}
+ return scopeValues, nil
}
+
+func (j *JWT) addScopeValueFromRoles(tokenClaims map[string]interface{}, scopeValues []string) ([]string, error) {
+ if j.rolesClaim == "" || j.rolesMap == nil {
+ return scopeValues, nil
+ }
+
+ rolesClaimValue, exists := tokenClaims[j.rolesClaim]
+ if !exists {
+ return scopeValues, nil
}
- if j.rolesClaim != "" {
- if rolesClaimValue, exists := tokenClaims[j.rolesClaim]; exists {
var roleValues []string
// ["foo", "bar"] is stored as []interface{}, not []string, unfortunately
rolesArray, ok := rolesClaimValue.([]interface{})
@@ -386,9 +415,6 @@ func (j *JWT) getScopeValues(tokenClaims map[string]interface{}) ([]string, erro
scopeValues = addScopeValue(scopeValues, s)
}
}
- }
- }
-
return scopeValues, nil
}
