Skip to content

Commit 0035609

Browse files
KN4CK3Rtechknowlogick
authored and
Gitea
committed
Fix wrong user in OpenID response (go-gitea#16736) (go-gitea#16741)
* Fix wrong user in OpenID response (go-gitea#16736) * Fixed usage of wrong user. * Added tests. * Fixed wrong import. Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 697c4e6 commit 0035609

File tree

3 files changed

+101
-10
lines changed

3 files changed

+101
-10
lines changed

models/fixtures/oauth2_grant.yml

+16
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,19 @@
55
scope: "openid profile"
66
created_unix: 1546869730
77
updated_unix: 1546869730
8+
9+
- id: 2
10+
user_id: 3
11+
application_id: 1
12+
counter: 1
13+
scope: "openid"
14+
created_unix: 1546869730
15+
updated_unix: 1546869730
16+
17+
- id: 3
18+
user_id: 5
19+
application_id: 1
20+
counter: 1
21+
scope: "openid profile email"
22+
created_unix: 1546869730
23+
updated_unix: 1546869730

routers/web/user/oauth.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
187187
ErrorDescription: "cannot find application",
188188
}
189189
}
190-
err = app.LoadUser()
190+
user, err := models.GetUserByID(grant.UserID)
191191
if err != nil {
192192
if models.IsErrUserNotExist(err) {
193193
return nil, &AccessTokenError{
@@ -212,17 +212,17 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, signingKey oauth2.JWTSign
212212
Nonce: grant.Nonce,
213213
}
214214
if grant.ScopeContains("profile") {
215-
idToken.Name = app.User.FullName
216-
idToken.PreferredUsername = app.User.Name
217-
idToken.Profile = app.User.HTMLURL()
218-
idToken.Picture = app.User.AvatarLink()
219-
idToken.Website = app.User.Website
220-
idToken.Locale = app.User.Language
221-
idToken.UpdatedAt = app.User.UpdatedUnix
215+
idToken.Name = user.FullName
216+
idToken.PreferredUsername = user.Name
217+
idToken.Profile = user.HTMLURL()
218+
idToken.Picture = user.AvatarLink()
219+
idToken.Website = user.Website
220+
idToken.Locale = user.Language
221+
idToken.UpdatedAt = user.UpdatedUnix
222222
}
223223
if grant.ScopeContains("email") {
224-
idToken.Email = app.User.Email
225-
idToken.EmailVerified = app.User.IsActive
224+
idToken.Email = user.Email
225+
idToken.EmailVerified = user.IsActive
226226
}
227227

228228
signedIDToken, err = idToken.SignToken(signingKey)

routers/web/user/oauth_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package user
6+
7+
import (
8+
"testing"
9+
10+
"code.gitea.io/gitea/models"
11+
"code.gitea.io/gitea/modules/auth/oauth2"
12+
13+
"github.com/golang-jwt/jwt"
14+
"github.com/stretchr/testify/assert"
15+
)
16+
17+
func createAndParseToken(t *testing.T, grant *models.OAuth2Grant) *models.OIDCToken {
18+
signingKey, err := oauth2.CreateJWTSingingKey("HS256", make([]byte, 32))
19+
assert.NoError(t, err)
20+
assert.NotNil(t, signingKey)
21+
oauth2.DefaultSigningKey = signingKey
22+
23+
response, terr := newAccessTokenResponse(grant, signingKey)
24+
assert.Nil(t, terr)
25+
assert.NotNil(t, response)
26+
27+
parsedToken, err := jwt.ParseWithClaims(response.IDToken, &models.OIDCToken{}, func(token *jwt.Token) (interface{}, error) {
28+
assert.NotNil(t, token.Method)
29+
assert.Equal(t, signingKey.SigningMethod().Alg(), token.Method.Alg())
30+
return signingKey.VerifyKey(), nil
31+
})
32+
assert.NoError(t, err)
33+
assert.True(t, parsedToken.Valid)
34+
35+
oidcToken, ok := parsedToken.Claims.(*models.OIDCToken)
36+
assert.True(t, ok)
37+
assert.NotNil(t, oidcToken)
38+
39+
return oidcToken
40+
}
41+
42+
func TestNewAccessTokenResponse_OIDCToken(t *testing.T) {
43+
assert.NoError(t, models.PrepareTestDatabase())
44+
45+
grants, err := models.GetOAuth2GrantsByUserID(3)
46+
assert.NoError(t, err)
47+
assert.Len(t, grants, 1)
48+
49+
// Scopes: openid
50+
oidcToken := createAndParseToken(t, grants[0])
51+
assert.Empty(t, oidcToken.Name)
52+
assert.Empty(t, oidcToken.PreferredUsername)
53+
assert.Empty(t, oidcToken.Profile)
54+
assert.Empty(t, oidcToken.Picture)
55+
assert.Empty(t, oidcToken.Website)
56+
assert.Empty(t, oidcToken.UpdatedAt)
57+
assert.Empty(t, oidcToken.Email)
58+
assert.False(t, oidcToken.EmailVerified)
59+
60+
user := models.AssertExistsAndLoadBean(t, &models.User{ID: 5}).(*models.User)
61+
grants, err = models.GetOAuth2GrantsByUserID(user.ID)
62+
assert.NoError(t, err)
63+
assert.Len(t, grants, 1)
64+
65+
// Scopes: openid profile email
66+
oidcToken = createAndParseToken(t, grants[0])
67+
assert.Equal(t, user.FullName, oidcToken.Name)
68+
assert.Equal(t, user.Name, oidcToken.PreferredUsername)
69+
assert.Equal(t, user.HTMLURL(), oidcToken.Profile)
70+
assert.Equal(t, user.AvatarLink(), oidcToken.Picture)
71+
assert.Equal(t, user.Website, oidcToken.Website)
72+
assert.Equal(t, user.UpdatedUnix, oidcToken.UpdatedAt)
73+
assert.Equal(t, user.Email, oidcToken.Email)
74+
assert.Equal(t, user.IsActive, oidcToken.EmailVerified)
75+
}

0 commit comments

Comments
 (0)