Skip to content

Commit a9415a6

Browse files
committed
fine tune
1 parent 4597729 commit a9415a6

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

services/oauth2_provider/access_token.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,21 @@ func GrantAdditionalScopes(grantScopes string) auth.AccessTokenScope {
8383

8484
var accessScopes []string // the scopes for access control, but not for general information
8585
for _, scope := range strings.Split(grantScopes, " ") {
86-
if !slices.Contains(generalScopesSupported, scope) {
86+
if scope != "" && !slices.Contains(generalScopesSupported, scope) {
8787
accessScopes = append(accessScopes, scope)
8888
}
8989
}
9090

9191
// since version 1.22, access tokens grant full access to the API
9292
// with this access is reduced only if additional scopes are provided
93-
// TODO: if there are invalid access scopes, then it is treated as "all", but would we really always treat invalid scopes as "all"?
94-
accessTokenScope := auth.AccessTokenScope(strings.Join(accessScopes, ","))
95-
if normalizedAccessTokenScope, err := accessTokenScope.Normalize(); err == nil && normalizedAccessTokenScope != "" {
96-
return normalizedAccessTokenScope
93+
if len(accessScopes) > 0 {
94+
accessTokenScope := auth.AccessTokenScope(strings.Join(accessScopes, ","))
95+
if normalizedAccessTokenScope, err := accessTokenScope.Normalize(); err == nil {
96+
return normalizedAccessTokenScope
97+
}
98+
// TODO: if there are invalid access scopes (err != nil),
99+
// then it is treated as "all", maybe in the future we should make it stricter to return an error
100+
// at the moment, to avoid breaking 1.22 behavior, invalid tokens are also treated as "all"
97101
}
98102
// fallback, empty access scope is treated as "all" access
99103
return auth.AccessTokenScopeAll

services/oauth2_provider/additional_scopes_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestGrantAdditionalScopes(t *testing.T) {
2424
{"read:user write:issue public-only", "public-only,write:issue,read:user"},
2525
{"openid profile email read:user", "read:user"},
2626

27-
// TODO: would we always treat invalid scopes as "all"?
27+
// TODO: at the moment invalid tokens are treated as "all" to avoid breaking 1.22 behavior (more details are in GrantAdditionalScopes)
2828
{"read:invalid_scope", "all"},
2929
{"read:invalid_scope,write:scope_invalid,just-plain-wrong", "all"},
3030
}

0 commit comments

Comments
 (0)