Skip to content

Commit

Permalink
Added negation of scope logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberinc-ashish committed Jun 7, 2021
1 parent b7793d3 commit fff344e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
31 changes: 26 additions & 5 deletions security/Token.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,36 @@ type JwtToken struct {
}

func (token *JwtToken) isValidForScope(allowedScopes []string) bool {
if ok, _ := inArray("*", token.Scopes); ok {
permissiveTokenScopes := []string{}
nonPermissiveTokenScopes := []string{}

for _, tokenScope := range token.Scopes {
if strings.HasPrefix(tokenScope, "-") {
nonPermissiveTokenScopes = append(nonPermissiveTokenScopes, tokenScope[1:])
} else {
permissiveTokenScopes = append(permissiveTokenScopes, tokenScope)
}
}

if len(nonPermissiveTokenScopes) > 0 {
if isScopePresent(nonPermissiveTokenScopes, allowedScopes) {
return false
}
}

return isScopePresent(permissiveTokenScopes, allowedScopes)
}

func isScopePresent(scopes []string, scopeToCheck []string) bool {
if ok, _ := inArray("*", scopes); ok {
return true
}
allScopesMatched := true
for _, allowedScope := range allowedScopes {
if ok, _ := inArray(allowedScope, token.Scopes); !ok {
for _, allowedScope := range scopeToCheck {
if ok, _ := inArray(allowedScope, scopes); !ok {
scopeParts := strings.Split(allowedScope, ":")
if ok, _ := inArray(scopeParts[0]+":*", token.Scopes); !ok {
if ok, _ := inArray("*:"+scopeParts[1], token.Scopes); !ok {
if ok, _ := inArray(scopeParts[0]+":*", scopes); !ok {
if ok, _ := inArray("*:"+scopeParts[1], scopes); !ok {
allScopesMatched = false
}
}
Expand Down
2 changes: 2 additions & 0 deletions security/Token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var scopeCombinations = []struct {
{[]string{"appliance:*", "trustedGroup:*"}, []string{"trustedGroup:read"}, true},
{[]string{"trustedGroup:read"}, []string{"trustedGroup:read"}, true},
{[]string{"trustedGroup:write"}, []string{"trustedGroup:read"}, false},
{[]string{"*", "-trustedGroup:*"}, []string{"trustedGroup:read"}, false},
{[]string{"trusterdGroup:*", "-trustedGroup:read"}, []string{"trustedGroup:read"}, false},
}

func TestScopes(t *testing.T) {
Expand Down

0 comments on commit fff344e

Please sign in to comment.