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.go 6.0KB

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