Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

oauth2.go 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2017 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. package models
  5. import (
  6. "sort"
  7. "code.gitea.io/gitea/modules/auth/oauth2"
  8. "code.gitea.io/gitea/modules/log"
  9. )
  10. // OAuth2Provider describes the display values of a single OAuth2 provider
  11. type OAuth2Provider struct {
  12. Name string
  13. DisplayName string
  14. Image string
  15. CustomURLMapping *oauth2.CustomURLMapping
  16. }
  17. // OAuth2Providers contains the map of registered OAuth2 providers in Gitea (based on goth)
  18. // key is used to map the OAuth2Provider with the goth provider type (also in LoginSource.OAuth2Config.Provider)
  19. // value is used to store display data
  20. var OAuth2Providers = map[string]OAuth2Provider{
  21. "bitbucket": {Name: "bitbucket", DisplayName: "Bitbucket", Image: "/img/auth/bitbucket.png"},
  22. "dropbox": {Name: "dropbox", DisplayName: "Dropbox", Image: "/img/auth/dropbox.png"},
  23. "facebook": {Name: "facebook", DisplayName: "Facebook", Image: "/img/auth/facebook.png"},
  24. "github": {
  25. Name: "github", DisplayName: "GitHub", Image: "/img/auth/github.png",
  26. CustomURLMapping: &oauth2.CustomURLMapping{
  27. TokenURL: oauth2.GetDefaultTokenURL("github"),
  28. AuthURL: oauth2.GetDefaultAuthURL("github"),
  29. ProfileURL: oauth2.GetDefaultProfileURL("github"),
  30. EmailURL: oauth2.GetDefaultEmailURL("github"),
  31. },
  32. },
  33. "gitlab": {
  34. Name: "gitlab", DisplayName: "GitLab", Image: "/img/auth/gitlab.png",
  35. CustomURLMapping: &oauth2.CustomURLMapping{
  36. TokenURL: oauth2.GetDefaultTokenURL("gitlab"),
  37. AuthURL: oauth2.GetDefaultAuthURL("gitlab"),
  38. ProfileURL: oauth2.GetDefaultProfileURL("gitlab"),
  39. },
  40. },
  41. "gplus": {Name: "gplus", DisplayName: "Google", Image: "/img/auth/google.png"},
  42. "openidConnect": {Name: "openidConnect", DisplayName: "OpenID Connect", Image: "/img/auth/openid_connect.svg"},
  43. "twitter": {Name: "twitter", DisplayName: "Twitter", Image: "/img/auth/twitter.png"},
  44. "discord": {Name: "discord", DisplayName: "Discord", Image: "/img/auth/discord.png"},
  45. "gitea": {
  46. Name: "gitea", DisplayName: "Gitea", Image: "/img/auth/gitea.png",
  47. CustomURLMapping: &oauth2.CustomURLMapping{
  48. TokenURL: oauth2.GetDefaultTokenURL("gitea"),
  49. AuthURL: oauth2.GetDefaultAuthURL("gitea"),
  50. ProfileURL: oauth2.GetDefaultProfileURL("gitea"),
  51. },
  52. },
  53. "nextcloud": {
  54. Name: "nextcloud", DisplayName: "Nextcloud", Image: "/img/auth/nextcloud.png",
  55. CustomURLMapping: &oauth2.CustomURLMapping{
  56. TokenURL: oauth2.GetDefaultTokenURL("nextcloud"),
  57. AuthURL: oauth2.GetDefaultAuthURL("nextcloud"),
  58. ProfileURL: oauth2.GetDefaultProfileURL("nextcloud"),
  59. },
  60. },
  61. "yandex": {Name: "yandex", DisplayName: "Yandex", Image: "/img/auth/yandex.png"},
  62. "mastodon": {
  63. Name: "mastodon", DisplayName: "Mastodon", Image: "/img/auth/mastodon.png",
  64. CustomURLMapping: &oauth2.CustomURLMapping{
  65. AuthURL: oauth2.GetDefaultAuthURL("mastodon"),
  66. },
  67. },
  68. }
  69. // OAuth2DefaultCustomURLMappings contains the map of default URL's for OAuth2 providers that are allowed to have custom urls
  70. // key is used to map the OAuth2Provider
  71. // value is the mapping as defined for the OAuth2Provider
  72. var OAuth2DefaultCustomURLMappings = map[string]*oauth2.CustomURLMapping{
  73. "github": OAuth2Providers["github"].CustomURLMapping,
  74. "gitlab": OAuth2Providers["gitlab"].CustomURLMapping,
  75. "gitea": OAuth2Providers["gitea"].CustomURLMapping,
  76. "nextcloud": OAuth2Providers["nextcloud"].CustomURLMapping,
  77. "mastodon": OAuth2Providers["mastodon"].CustomURLMapping,
  78. }
  79. // GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources
  80. func GetActiveOAuth2ProviderLoginSources() ([]*LoginSource, error) {
  81. sources := make([]*LoginSource, 0, 1)
  82. if err := x.Where("is_actived = ? and type = ?", true, LoginOAuth2).Find(&sources); err != nil {
  83. return nil, err
  84. }
  85. return sources, nil
  86. }
  87. // GetActiveOAuth2LoginSourceByName returns a OAuth2 LoginSource based on the given name
  88. func GetActiveOAuth2LoginSourceByName(name string) (*LoginSource, error) {
  89. loginSource := new(LoginSource)
  90. has, err := x.Where("name = ? and type = ? and is_actived = ?", name, LoginOAuth2, true).Get(loginSource)
  91. if !has || err != nil {
  92. return nil, err
  93. }
  94. return loginSource, nil
  95. }
  96. // GetActiveOAuth2Providers returns the map of configured active OAuth2 providers
  97. // key is used as technical name (like in the callbackURL)
  98. // values to display
  99. func GetActiveOAuth2Providers() ([]string, map[string]OAuth2Provider, error) {
  100. // Maybe also separate used and unused providers so we can force the registration of only 1 active provider for each type
  101. loginSources, err := GetActiveOAuth2ProviderLoginSources()
  102. if err != nil {
  103. return nil, nil, err
  104. }
  105. var orderedKeys []string
  106. providers := make(map[string]OAuth2Provider)
  107. for _, source := range loginSources {
  108. prov := OAuth2Providers[source.OAuth2().Provider]
  109. if source.OAuth2().IconURL != "" {
  110. prov.Image = source.OAuth2().IconURL
  111. }
  112. providers[source.Name] = prov
  113. orderedKeys = append(orderedKeys, source.Name)
  114. }
  115. sort.Strings(orderedKeys)
  116. return orderedKeys, providers, nil
  117. }
  118. // InitOAuth2 initialize the OAuth2 lib and register all active OAuth2 providers in the library
  119. func InitOAuth2() error {
  120. if err := oauth2.Init(x); err != nil {
  121. return err
  122. }
  123. return initOAuth2LoginSources()
  124. }
  125. // ResetOAuth2 clears existing OAuth2 providers and loads them from DB
  126. func ResetOAuth2() error {
  127. oauth2.ClearProviders()
  128. return initOAuth2LoginSources()
  129. }
  130. // initOAuth2LoginSources is used to load and register all active OAuth2 providers
  131. func initOAuth2LoginSources() error {
  132. loginSources, _ := GetActiveOAuth2ProviderLoginSources()
  133. for _, source := range loginSources {
  134. oAuth2Config := source.OAuth2()
  135. err := oauth2.RegisterProvider(source.Name, oAuth2Config.Provider, oAuth2Config.ClientID, oAuth2Config.ClientSecret, oAuth2Config.OpenIDConnectAutoDiscoveryURL, oAuth2Config.CustomURLMapping)
  136. if err != nil {
  137. log.Critical("Unable to register source: %s due to Error: %v. This source will be disabled.", source.Name, err)
  138. source.IsActived = false
  139. if err = UpdateSource(source); err != nil {
  140. log.Critical("Unable to update source %s to disable it. Error: %v", err)
  141. return err
  142. }
  143. }
  144. }
  145. return nil
  146. }
  147. // wrapOpenIDConnectInitializeError is used to wrap the error but this cannot be done in modules/auth/oauth2
  148. // inside oauth2: import cycle not allowed models -> modules/auth/oauth2 -> models
  149. func wrapOpenIDConnectInitializeError(err error, providerName string, oAuth2Config *OAuth2Config) error {
  150. if err != nil && "openidConnect" == oAuth2Config.Provider {
  151. err = ErrOpenIDConnectInitialize{ProviderName: providerName, OpenIDConnectAutoDiscoveryURL: oAuth2Config.OpenIDConnectAutoDiscoveryURL, Cause: err}
  152. }
  153. return err
  154. }