|
| 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 setting |
| 6 | + |
| 7 | +import ( |
| 8 | + "code.gitea.io/gitea/modules/log" |
| 9 | + |
| 10 | + "gopkg.in/ini.v1" |
| 11 | +) |
| 12 | + |
| 13 | +// OAuth2UsernameType is enum describing the way gitea 'name' should be generated from oauth2 data |
| 14 | +type OAuth2UsernameType string |
| 15 | + |
| 16 | +const ( |
| 17 | + // OAuth2UsernameUserid oauth2 userid field will be used as gitea name |
| 18 | + OAuth2UsernameUserid OAuth2UsernameType = "userid" |
| 19 | + // OAuth2UsernameNickname oauth2 nickname field will be used as gitea name |
| 20 | + OAuth2UsernameNickname OAuth2UsernameType = "nickname" |
| 21 | + // OAuth2UsernameEmail username of oauth2 email filed will be used as gitea name |
| 22 | + OAuth2UsernameEmail OAuth2UsernameType = "email" |
| 23 | +) |
| 24 | + |
| 25 | +func (username OAuth2UsernameType) isValid() bool { |
| 26 | + switch username { |
| 27 | + case OAuth2UsernameUserid, OAuth2UsernameNickname, OAuth2UsernameEmail: |
| 28 | + return true |
| 29 | + } |
| 30 | + return false |
| 31 | +} |
| 32 | + |
| 33 | +// OAuth2AccountLinkingType is enum describing behaviour of linking with existing account |
| 34 | +type OAuth2AccountLinkingType string |
| 35 | + |
| 36 | +const ( |
| 37 | + // OAuth2AccountLinkingDisabled error will be displayed if account exist |
| 38 | + OAuth2AccountLinkingDisabled OAuth2AccountLinkingType = "disabled" |
| 39 | + // OAuth2AccountLinkingLogin account linking login will be displayed if account exist |
| 40 | + OAuth2AccountLinkingLogin OAuth2AccountLinkingType = "login" |
| 41 | + // OAuth2AccountLinkingAuto account will be automatically linked if account exist |
| 42 | + OAuth2AccountLinkingAuto OAuth2AccountLinkingType = "auto" |
| 43 | +) |
| 44 | + |
| 45 | +func (accountLinking OAuth2AccountLinkingType) isValid() bool { |
| 46 | + switch accountLinking { |
| 47 | + case OAuth2AccountLinkingDisabled, OAuth2AccountLinkingLogin, OAuth2AccountLinkingAuto: |
| 48 | + return true |
| 49 | + } |
| 50 | + return false |
| 51 | +} |
| 52 | + |
| 53 | +// OAuth2Client settings |
| 54 | +var OAuth2Client struct { |
| 55 | + RegisterEmailConfirm bool |
| 56 | + OpenIDConnectScopes []string |
| 57 | + EnableAutoRegistration bool |
| 58 | + Username OAuth2UsernameType |
| 59 | + UpdateAvatar bool |
| 60 | + AccountLinking OAuth2AccountLinkingType |
| 61 | +} |
| 62 | + |
| 63 | +func newOAuth2Client() { |
| 64 | + sec := Cfg.Section("oauth2_client") |
| 65 | + OAuth2Client.RegisterEmailConfirm = sec.Key("REGISTER_EMAIL_CONFIRM").MustBool(Service.RegisterEmailConfirm) |
| 66 | + OAuth2Client.OpenIDConnectScopes = parseScopes(sec, "OPENID_CONNECT_SCOPES") |
| 67 | + OAuth2Client.EnableAutoRegistration = sec.Key("ENABLE_AUTO_REGISTRATION").MustBool() |
| 68 | + OAuth2Client.Username = OAuth2UsernameType(sec.Key("USERNAME").MustString(string(OAuth2UsernameNickname))) |
| 69 | + if !OAuth2Client.Username.isValid() { |
| 70 | + log.Warn("Username setting is not valid: '%s', will fallback to '%s'", OAuth2Client.Username, OAuth2UsernameNickname) |
| 71 | + OAuth2Client.Username = OAuth2UsernameNickname |
| 72 | + } |
| 73 | + OAuth2Client.UpdateAvatar = sec.Key("UPDATE_AVATAR").MustBool() |
| 74 | + OAuth2Client.AccountLinking = OAuth2AccountLinkingType(sec.Key("ACCOUNT_LINKING").MustString(string(OAuth2AccountLinkingDisabled))) |
| 75 | + if !OAuth2Client.AccountLinking.isValid() { |
| 76 | + log.Warn("Account linking setting is not valid: '%s', will fallback to '%s'", OAuth2Client.AccountLinking, OAuth2AccountLinkingDisabled) |
| 77 | + OAuth2Client.AccountLinking = OAuth2AccountLinkingDisabled |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func parseScopes(sec *ini.Section, name string) []string { |
| 82 | + parts := sec.Key(name).Strings(" ") |
| 83 | + scopes := make([]string, 0, len(parts)) |
| 84 | + for _, scope := range parts { |
| 85 | + if scope != "" { |
| 86 | + scopes = append(scopes, scope) |
| 87 | + } |
| 88 | + } |
| 89 | + return scopes |
| 90 | +} |
0 commit comments