Skip to content

Commit 1295e75

Browse files
KN4CK3Rtechknowlogicklafriks
authored
Add OpenID claims "profile" and "email". (#16141)
* Added OpenID claims "profile" and "email". * Splitted error. * Added scopes_supported and claims_supported. * Added more metadata. Co-authored-by: techknowlogick <[email protected]> Co-authored-by: Lauris BH <[email protected]>
1 parent 2b39357 commit 1295e75

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

models/oauth2_application.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ func (grant *OAuth2Grant) TableName() string {
394394
return "oauth2_grant"
395395
}
396396

397-
// GenerateNewAuthorizationCode generates a new authorization code for a grant and saves it to the databse
397+
// GenerateNewAuthorizationCode generates a new authorization code for a grant and saves it to the database
398398
func (grant *OAuth2Grant) GenerateNewAuthorizationCode(redirectURI, codeChallenge, codeChallengeMethod string) (*OAuth2AuthorizationCode, error) {
399399
return grant.generateNewAuthorizationCode(x, redirectURI, codeChallenge, codeChallengeMethod)
400400
}
@@ -567,6 +567,19 @@ func (token *OAuth2Token) SignToken() (string, error) {
567567
type OIDCToken struct {
568568
jwt.StandardClaims
569569
Nonce string `json:"nonce,omitempty"`
570+
571+
// Scope profile
572+
Name string `json:"name,omitempty"`
573+
PreferredUsername string `json:"preferred_username,omitempty"`
574+
Profile string `json:"profile,omitempty"`
575+
Picture string `json:"picture,omitempty"`
576+
Website string `json:"website,omitempty"`
577+
Locale string `json:"locale,omitempty"`
578+
UpdatedAt timeutil.TimeStamp `json:"updated_at,omitempty"`
579+
580+
// Scope email
581+
Email string `json:"email,omitempty"`
582+
EmailVerified bool `json:"email_verified,omitempty"`
570583
}
571584

572585
// SignToken signs an id_token with the (symmetric) client secret key

routers/web/user/oauth.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,21 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, clientSecret string) (*Ac
185185
ErrorDescription: "cannot find application",
186186
}
187187
}
188+
err = app.LoadUser()
189+
if err != nil {
190+
if models.IsErrUserNotExist(err) {
191+
return nil, &AccessTokenError{
192+
ErrorCode: AccessTokenErrorCodeInvalidRequest,
193+
ErrorDescription: "cannot find user",
194+
}
195+
}
196+
log.Error("Error loading user: %v", err)
197+
return nil, &AccessTokenError{
198+
ErrorCode: AccessTokenErrorCodeInvalidRequest,
199+
ErrorDescription: "server error",
200+
}
201+
}
202+
188203
idToken := &models.OIDCToken{
189204
StandardClaims: jwt.StandardClaims{
190205
ExpiresAt: expirationDate.AsTime().Unix(),
@@ -194,6 +209,20 @@ func newAccessTokenResponse(grant *models.OAuth2Grant, clientSecret string) (*Ac
194209
},
195210
Nonce: grant.Nonce,
196211
}
212+
if grant.ScopeContains("profile") {
213+
idToken.Name = app.User.FullName
214+
idToken.PreferredUsername = app.User.Name
215+
idToken.Profile = app.User.HTMLURL()
216+
idToken.Picture = app.User.AvatarLink()
217+
idToken.Website = app.User.Website
218+
idToken.Locale = app.User.Language
219+
idToken.UpdatedAt = app.User.UpdatedUnix
220+
}
221+
if grant.ScopeContains("email") {
222+
idToken.Email = app.User.Email
223+
idToken.EmailVerified = app.User.IsActive
224+
}
225+
197226
signedIDToken, err = idToken.SignToken(clientSecret)
198227
if err != nil {
199228
return nil, &AccessTokenError{

templates/user/auth/oidc_wellknown.tmpl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,34 @@
66
"response_types_supported": [
77
"code",
88
"id_token"
9+
],
10+
"scopes_supported": [
11+
"openid",
12+
"profile",
13+
"email"
14+
],
15+
"claims_supported": [
16+
"aud",
17+
"exp",
18+
"iat",
19+
"iss",
20+
"sub",
21+
"name",
22+
"preferred_username",
23+
"profile",
24+
"picture",
25+
"website",
26+
"locale",
27+
"updated_at",
28+
"email",
29+
"email_verified"
30+
],
31+
"code_challenge_methods_supported": [
32+
"plain",
33+
"S256"
34+
],
35+
"grant_types_supported": [
36+
"authorization_code",
37+
"refresh_token"
938
]
1039
}

0 commit comments

Comments
 (0)