github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/setting/oauth2_client.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package setting 7 8 import ( 9 "github.com/gitbundle/modules/log" 10 11 "gopkg.in/ini.v1" 12 ) 13 14 // OAuth2UsernameType is enum describing the way gitbundle 'name' should be generated from oauth2 data 15 type OAuth2UsernameType string 16 17 const ( 18 // OAuth2UsernameUserid oauth2 userid field will be used as gitbundle name 19 OAuth2UsernameUserid OAuth2UsernameType = "userid" 20 // OAuth2UsernameNickname oauth2 nickname field will be used as gitbundle name 21 OAuth2UsernameNickname OAuth2UsernameType = "nickname" 22 // OAuth2UsernameEmail username of oauth2 email filed will be used as gitbundle name 23 OAuth2UsernameEmail OAuth2UsernameType = "email" 24 ) 25 26 func (username OAuth2UsernameType) isValid() bool { 27 switch username { 28 case OAuth2UsernameUserid, OAuth2UsernameNickname, OAuth2UsernameEmail: 29 return true 30 } 31 return false 32 } 33 34 // OAuth2AccountLinkingType is enum describing behaviour of linking with existing account 35 type OAuth2AccountLinkingType string 36 37 const ( 38 // OAuth2AccountLinkingDisabled error will be displayed if account exist 39 OAuth2AccountLinkingDisabled OAuth2AccountLinkingType = "disabled" 40 // OAuth2AccountLinkingLogin account linking login will be displayed if account exist 41 OAuth2AccountLinkingLogin OAuth2AccountLinkingType = "login" 42 // OAuth2AccountLinkingAuto account will be automatically linked if account exist 43 OAuth2AccountLinkingAuto OAuth2AccountLinkingType = "auto" 44 ) 45 46 func (accountLinking OAuth2AccountLinkingType) isValid() bool { 47 switch accountLinking { 48 case OAuth2AccountLinkingDisabled, OAuth2AccountLinkingLogin, OAuth2AccountLinkingAuto: 49 return true 50 } 51 return false 52 } 53 54 // OAuth2Client settings 55 var OAuth2Client struct { 56 RegisterEmailConfirm bool 57 OpenIDConnectScopes []string 58 EnableAutoRegistration bool 59 Username OAuth2UsernameType 60 UpdateAvatar bool 61 AccountLinking OAuth2AccountLinkingType 62 } 63 64 func newOAuth2Client() { 65 sec := Cfg.Section("oauth2_client") 66 OAuth2Client.RegisterEmailConfirm = sec.Key("REGISTER_EMAIL_CONFIRM").MustBool(Service.RegisterEmailConfirm) 67 OAuth2Client.OpenIDConnectScopes = parseScopes(sec, "OPENID_CONNECT_SCOPES") 68 OAuth2Client.EnableAutoRegistration = sec.Key("ENABLE_AUTO_REGISTRATION").MustBool() 69 OAuth2Client.Username = OAuth2UsernameType(sec.Key("USERNAME").MustString(string(OAuth2UsernameNickname))) 70 if !OAuth2Client.Username.isValid() { 71 log.Warn("Username setting is not valid: '%s', will fallback to '%s'", OAuth2Client.Username, OAuth2UsernameNickname) 72 OAuth2Client.Username = OAuth2UsernameNickname 73 } 74 OAuth2Client.UpdateAvatar = sec.Key("UPDATE_AVATAR").MustBool() 75 OAuth2Client.AccountLinking = OAuth2AccountLinkingType(sec.Key("ACCOUNT_LINKING").MustString(string(OAuth2AccountLinkingLogin))) 76 if !OAuth2Client.AccountLinking.isValid() { 77 log.Warn("Account linking setting is not valid: '%s', will fallback to '%s'", OAuth2Client.AccountLinking, OAuth2AccountLinkingLogin) 78 OAuth2Client.AccountLinking = OAuth2AccountLinkingLogin 79 } 80 } 81 82 func parseScopes(sec *ini.Section, name string) []string { 83 parts := sec.Key(name).Strings(" ") 84 scopes := make([]string, 0, len(parts)) 85 for _, scope := range parts { 86 if scope != "" { 87 scopes = append(scopes, scope) 88 } 89 } 90 return scopes 91 }