gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package oauth2
  4. import (
  5. "code.gitea.io/gitea/modules/setting"
  6. "github.com/markbates/goth"
  7. "github.com/markbates/goth/providers/azureadv2"
  8. "github.com/markbates/goth/providers/gitea"
  9. "github.com/markbates/goth/providers/github"
  10. "github.com/markbates/goth/providers/gitlab"
  11. "github.com/markbates/goth/providers/mastodon"
  12. "github.com/markbates/goth/providers/nextcloud"
  13. )
  14. // CustomProviderNewFn creates a goth.Provider using a custom url mapping
  15. type CustomProviderNewFn func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error)
  16. // CustomProvider is a GothProvider that has CustomURL features
  17. type CustomProvider struct {
  18. BaseProvider
  19. customURLSettings *CustomURLSettings
  20. newFn CustomProviderNewFn
  21. }
  22. // CustomURLSettings returns the CustomURLSettings for this provider
  23. func (c *CustomProvider) CustomURLSettings() *CustomURLSettings {
  24. return c.customURLSettings
  25. }
  26. // CreateGothProvider creates a GothProvider from this Provider
  27. func (c *CustomProvider) CreateGothProvider(providerName, callbackURL string, source *Source) (goth.Provider, error) {
  28. custom := c.customURLSettings.OverrideWith(source.CustomURLMapping)
  29. return c.newFn(source.ClientID, source.ClientSecret, callbackURL, custom, source.Scopes)
  30. }
  31. // NewCustomProvider is a constructor function for custom providers
  32. func NewCustomProvider(name, displayName string, customURLSetting *CustomURLSettings, newFn CustomProviderNewFn) *CustomProvider {
  33. return &CustomProvider{
  34. BaseProvider: BaseProvider{
  35. name: name,
  36. displayName: displayName,
  37. },
  38. customURLSettings: customURLSetting,
  39. newFn: newFn,
  40. }
  41. }
  42. var _ GothProvider = &CustomProvider{}
  43. func init() {
  44. RegisterGothProvider(NewCustomProvider(
  45. "github", "GitHub", &CustomURLSettings{
  46. TokenURL: availableAttribute(github.TokenURL),
  47. AuthURL: availableAttribute(github.AuthURL),
  48. ProfileURL: availableAttribute(github.ProfileURL),
  49. EmailURL: availableAttribute(github.EmailURL),
  50. },
  51. func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
  52. if setting.OAuth2Client.EnableAutoRegistration {
  53. scopes = append(scopes, "user:email")
  54. }
  55. return github.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, custom.EmailURL, scopes...), nil
  56. }))
  57. RegisterGothProvider(NewCustomProvider(
  58. "gitlab", "GitLab", &CustomURLSettings{
  59. AuthURL: availableAttribute(gitlab.AuthURL),
  60. TokenURL: availableAttribute(gitlab.TokenURL),
  61. ProfileURL: availableAttribute(gitlab.ProfileURL),
  62. }, func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
  63. scopes = append(scopes, "read_user")
  64. return gitlab.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, scopes...), nil
  65. }))
  66. RegisterGothProvider(NewCustomProvider(
  67. "gitea", "Gitea", &CustomURLSettings{
  68. TokenURL: requiredAttribute(gitea.TokenURL),
  69. AuthURL: requiredAttribute(gitea.AuthURL),
  70. ProfileURL: requiredAttribute(gitea.ProfileURL),
  71. },
  72. func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
  73. return gitea.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, scopes...), nil
  74. }))
  75. RegisterGothProvider(NewCustomProvider(
  76. "nextcloud", "Nextcloud", &CustomURLSettings{
  77. TokenURL: requiredAttribute(nextcloud.TokenURL),
  78. AuthURL: requiredAttribute(nextcloud.AuthURL),
  79. ProfileURL: requiredAttribute(nextcloud.ProfileURL),
  80. },
  81. func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
  82. return nextcloud.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, scopes...), nil
  83. }))
  84. RegisterGothProvider(NewCustomProvider(
  85. "mastodon", "Mastodon", &CustomURLSettings{
  86. AuthURL: requiredAttribute(mastodon.InstanceURL),
  87. },
  88. func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
  89. return mastodon.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, scopes...), nil
  90. }))
  91. RegisterGothProvider(NewCustomProvider(
  92. "azureadv2", "Azure AD v2", &CustomURLSettings{
  93. Tenant: requiredAttribute("organizations"),
  94. },
  95. func(clientID, secret, callbackURL string, custom *CustomURLMapping, scopes []string) (goth.Provider, error) {
  96. azureScopes := make([]azureadv2.ScopeType, len(scopes))
  97. for i, scope := range scopes {
  98. azureScopes[i] = azureadv2.ScopeType(scope)
  99. }
  100. return azureadv2.New(clientID, secret, callbackURL, azureadv2.ProviderOptions{
  101. Tenant: azureadv2.TenantType(custom.Tenant),
  102. Scopes: azureScopes,
  103. }), nil
  104. },
  105. ))
  106. }