Compare commits

...

3 commits

Author SHA1 Message Date
f87a6d8b5a Merge remote-tracking branch 'upstream/forgejo' into feat/sso-preferred-username
Some checks failed
Integration tests for the release process / release-simulation (push) Has been cancelled
2024-04-23 22:00:46 -05:00
f59a2ae7e1 Replace function name 2024-04-20 20:57:27 -05:00
d5628bc98d Add preferred username option for SSO 2024-04-20 20:47:19 -05:00
3 changed files with 11 additions and 1 deletions

View file

@ -22,11 +22,13 @@ const (
OAuth2UsernameNickname OAuth2UsernameType = "nickname"
// OAuth2UsernameEmail username of oauth2 email field will be used as gitea name
OAuth2UsernameEmail OAuth2UsernameType = "email"
// @OAuth2UsernamePreferredUsername oauth2 preferred_username field will be used as gitea name
OAuth2UsernamePreferredUsername OAuth2UsernameType = "username"
)
func (username OAuth2UsernameType) isValid() bool {
switch username {
case OAuth2UsernameUserid, OAuth2UsernameNickname, OAuth2UsernameEmail:
case OAuth2UsernameUserid, OAuth2UsernameNickname, OAuth2UsernameEmail, OAuth2UsernamePreferredUsername:
return true
}
return false

View file

@ -387,6 +387,12 @@ func handleSignInFull(ctx *context.Context, u *user_model.User, remember, obeyRe
func getUserName(gothUser *goth.User) (string, error) {
switch setting.OAuth2Client.Username {
case setting.OAuth2UsernamePreferredUsername:
username := gothUser.RawData["preferred_username"].(string)
if strings.Contains(username, "@") {
return user_model.NormalizeUserName(strings.Split(username, "@")[0])
}
return user_model.NormalizeUserName(username)
case setting.OAuth2UsernameEmail:
return user_model.NormalizeUserName(strings.Split(gothUser.Email, "@")[0])
case setting.OAuth2UsernameNickname:

View file

@ -959,6 +959,8 @@ func SignInOAuthCallback(ctx *context.Context) {
}
if setting.OAuth2Client.Username == setting.OAuth2UsernameNickname && gothUser.NickName == "" {
missingFields = append(missingFields, "nickname")
} else if setting.OAuth2Client.Username == setting.OAuth2UsernamePreferredUsername && gothUser.RawData["preferred_username"].(string) == "" {
missingFields = append(missingFields, "preferred_nickname")
}
if len(missingFields) > 0 {
// we don't have enough information to create an account automatically,