Skip to content

Commit e6fab4c

Browse files
committed
oauth2 additional scopes
1 parent c53f38c commit e6fab4c

File tree

7 files changed

+36
-27
lines changed

7 files changed

+36
-27
lines changed

modules/setting/oauth2.go

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,23 +90,25 @@ func parseScopes(sec ConfigSection, name string) []string {
9090
}
9191

9292
var OAuth2 = struct {
93-
Enabled bool
94-
AccessTokenExpirationTime int64
95-
RefreshTokenExpirationTime int64
96-
InvalidateRefreshTokens bool
97-
JWTSigningAlgorithm string `ini:"JWT_SIGNING_ALGORITHM"`
98-
JWTSigningPrivateKeyFile string `ini:"JWT_SIGNING_PRIVATE_KEY_FILE"`
99-
MaxTokenLength int
100-
DefaultApplications []string
93+
Enabled bool
94+
AccessTokenExpirationTime int64
95+
RefreshTokenExpirationTime int64
96+
InvalidateRefreshTokens bool
97+
JWTSigningAlgorithm string `ini:"JWT_SIGNING_ALGORITHM"`
98+
JWTSigningPrivateKeyFile string `ini:"JWT_SIGNING_PRIVATE_KEY_FILE"`
99+
MaxTokenLength int
100+
DefaultApplications []string
101+
EnableAdditionalGrantScopes bool
101102
}{
102-
Enabled: true,
103-
AccessTokenExpirationTime: 3600,
104-
RefreshTokenExpirationTime: 730,
105-
InvalidateRefreshTokens: false,
106-
JWTSigningAlgorithm: "RS256",
107-
JWTSigningPrivateKeyFile: "jwt/private.pem",
108-
MaxTokenLength: math.MaxInt16,
109-
DefaultApplications: []string{"git-credential-oauth", "git-credential-manager", "tea"},
103+
Enabled: true,
104+
AccessTokenExpirationTime: 3600,
105+
RefreshTokenExpirationTime: 730,
106+
InvalidateRefreshTokens: false,
107+
JWTSigningAlgorithm: "RS256",
108+
JWTSigningPrivateKeyFile: "jwt/private.pem",
109+
MaxTokenLength: math.MaxInt16,
110+
DefaultApplications: []string{"git-credential-oauth", "git-credential-manager", "tea"},
111+
EnableAdditionalGrantScopes: false,
110112
}
111113

112114
func loadOAuth2From(rootCfg ConfigProvider) {

routers/web/auth/oauth2_provider.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ type userInfoResponse struct {
8585
PreferredUsername string `json:"preferred_username"`
8686
Email string `json:"email"`
8787
Picture string `json:"picture"`
88-
Groups []string `json:"groups"`
88+
Groups []string `json:"groups,omitempty"`
8989
}
9090

9191
// InfoOAuth manages request for userinfo endpoint

routers/web/user/setting/applications.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,6 @@ func loadApplicationsData(ctx *context.Context) {
113113
ctx.ServerError("GetOAuth2GrantsByUserID", err)
114114
return
115115
}
116+
ctx.Data["EnableAdditionalGrantScopes"] = setting.OAuth2.EnableAdditionalGrantScopes
116117
}
117118
}

services/auth/oauth2.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ var (
2626
)
2727

2828
// CheckOAuthAccessToken returns uid of user from oauth token
29-
func CheckOAuthAccessToken(ctx context.Context, accessToken string) int64 {
29+
func CheckOAuthAccessToken(ctx context.Context, accessToken string) (int64, auth_model.AccessTokenScope) {
30+
var accessTokenScope auth_model.AccessTokenScope
3031
if !setting.OAuth2.Enabled {
31-
return 0
32+
return 0, accessTokenScope
3233
}
3334

3435
// JWT tokens require a ".", if the token isn't like that, return early

services/oauth2_provider/access_token.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,14 @@ func GetOAuthGroupsForUser(ctx context.Context, user *user_model.User) ([]string
232232

233233
var groups []string
234234
for _, org := range orgs {
235-
if onlyPublicGroups {
236-
if public, err := org_model.IsPublicMembership(ctx, org.ID, user.ID); err == nil {
237-
if !public || !org.Visibility.IsPublic() {
238-
continue
235+
// process additional scopes only if enabled in settings
236+
// this could be removed once additional scopes get accepted
237+
if setting.OAuth2.EnableAdditionalGrantScopes {
238+
if onlyPublicGroups {
239+
if public, err := org_model.IsPublicMembership(ctx, org.ID, user.ID); err == nil {
240+
if !public || !org.Visibility.IsPublic() {
241+
continue
242+
}
239243
}
240244
}
241245
}

services/oauth2_provider/additional_scopes_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
)
1111

1212
func TestGrantAdditionalScopes(t *testing.T) {
13+
setting.OAuth2.EnableAdditionalGrantScopes = true
1314
tests := []struct {
1415
grantScopes string
1516
expectedScopes string

tests/integration/oauth_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ func TestOAuth_GrantScopesReadUserFailRepos(t *testing.T) {
515515
err := db.Insert(db.DefaultContext, grant)
516516
require.NoError(t, err)
517517

518-
assert.ElementsMatch(t, []string{"openid", "profile", "email", "read:user"}, strings.Split(grant.Scope, " "))
518+
assert.Contains(t, grant.Scope, "openid profile email read:user")
519519

520520
ctx := loginUserWithPasswordRemember(t, user.Name, "password", true)
521521

@@ -596,7 +596,7 @@ func TestOAuth_GrantScopesReadRepositoryFailOrganization(t *testing.T) {
596596
err := db.Insert(db.DefaultContext, grant)
597597
require.NoError(t, err)
598598

599-
assert.ElementsMatch(t, []string{"openid", "profile", "email", "read:user", "read:repository"}, strings.Split(grant.Scope, " "))
599+
assert.Contains(t, grant.Scope, "openid profile email read:user read:repository")
600600

601601
ctx := loginUserWithPasswordRemember(t, user.Name, "password", true)
602602

@@ -790,7 +790,7 @@ func TestOAuth_GrantScopesClaimGroupsAll(t *testing.T) {
790790
}
791791
}
792792

793-
func TestOAuth_GrantScopesClaimGroupsPublicOnly(t *testing.T) {
793+
func TestOAuth_GrantScopesEnabledClaimGroups(t *testing.T) {
794794
defer tests.PrepareTestEnv(t)()
795795

796796
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user2"})
@@ -819,7 +819,7 @@ func TestOAuth_GrantScopesClaimGroupsPublicOnly(t *testing.T) {
819819
err := db.Insert(db.DefaultContext, grant)
820820
require.NoError(t, err)
821821

822-
assert.ElementsMatch(t, []string{"openid", "profile", "email", "groups"}, strings.Split(grant.Scope, " "))
822+
assert.Contains(t, grant.Scope, "openid profile email groups")
823823

824824
ctx := loginUserWithPasswordRemember(t, user.Name, "password", true)
825825

0 commit comments

Comments
 (0)