You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oauth2_client.go 3.2KB

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