Skip to content

Commit 6ba0c37

Browse files
authored
Allow preferred_username as username source for OIDC (#30454)
This PR adds the preferred_username claim as a possible username source for the oauth2_client. Closes #21518
1 parent cf9061f commit 6ba0c37

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,8 +1553,9 @@ LEVEL = Info
15531553
;; The source of the username for new oauth2 accounts:
15541554
;; userid = use the userid / sub attribute
15551555
;; nickname = use the nickname attribute
1556+
;; preferred_username = use the preferred_username attribute
15561557
;; email = use the username part of the email attribute
1557-
;; Note: `nickname` and `email` options will normalize input strings using the following criteria:
1558+
;; Note: `nickname`, `preferred_username` and `email` options will normalize input strings using the following criteria:
15581559
;; - diacritics are removed
15591560
;; - the characters in the set `['´\x60]` are removed
15601561
;; - the characters in the set `[\s~+]` are replaced with `-`

docs/content/administration/config-cheat-sheet.en-us.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,10 @@ And the following unique queues:
608608
- `ENABLE_AUTO_REGISTRATION`: **false**: Automatically create user accounts for new oauth2 users.
609609
- `USERNAME`: **nickname**: The source of the username for new oauth2 accounts:
610610
- `userid` - use the userid / sub attribute
611-
- `nickname` - use the nickname attribute
611+
- `nickname` - use the nickname
612+
- `preferred_username` - use the preferred_username
612613
- `email` - use the username part of the email attribute
613-
- Note: `nickname` and `email` options will normalize input strings using the following criteria:
614+
- Note: `nickname`, `preferred_username` and `email` options will normalize input strings using the following criteria:
614615
- diacritics are removed
615616
- the characters in the set `['´\x60]` are removed
616617
- the characters in the set `[\s~+]` are replaced with `-`

modules/setting/oauth2.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ const (
2222
OAuth2UsernameNickname OAuth2UsernameType = "nickname"
2323
// OAuth2UsernameEmail username of oauth2 email field will be used as gitea name
2424
OAuth2UsernameEmail OAuth2UsernameType = "email"
25+
// OAuth2UsernameEmail username of oauth2 preferred_username field will be used as gitea name
26+
OAuth2UsernamePreferredUsername OAuth2UsernameType = "preferred_username"
2527
)
2628

2729
func (username OAuth2UsernameType) isValid() bool {
2830
switch username {
29-
case OAuth2UsernameUserid, OAuth2UsernameNickname, OAuth2UsernameEmail:
31+
case OAuth2UsernameUserid, OAuth2UsernameNickname, OAuth2UsernameEmail, OAuth2UsernamePreferredUsername:
3032
return true
3133
}
3234
return false

routers/web/auth/auth.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,13 @@ func getUserName(gothUser *goth.User) (string, error) {
386386
switch setting.OAuth2Client.Username {
387387
case setting.OAuth2UsernameEmail:
388388
return user_model.NormalizeUserName(strings.Split(gothUser.Email, "@")[0])
389+
case setting.OAuth2UsernamePreferredUsername:
390+
preferredUsername, exists := gothUser.RawData["preferred_username"]
391+
if exists {
392+
return user_model.NormalizeUserName(preferredUsername.(string))
393+
} else {
394+
return "", fmt.Errorf("preferred_username is missing in received user data but configured as username source for user_id %q. Check if OPENID_CONNECT_SCOPES contains profile", gothUser.UserID)
395+
}
389396
case setting.OAuth2UsernameNickname:
390397
return user_model.NormalizeUserName(gothUser.NickName)
391398
default: // OAuth2UsernameUserid

0 commit comments

Comments
 (0)