gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/azuread"
  8. "github.com/markbates/goth/providers/bitbucket"
  9. "github.com/markbates/goth/providers/discord"
  10. "github.com/markbates/goth/providers/dropbox"
  11. "github.com/markbates/goth/providers/facebook"
  12. "github.com/markbates/goth/providers/google"
  13. "github.com/markbates/goth/providers/microsoftonline"
  14. "github.com/markbates/goth/providers/twitter"
  15. "github.com/markbates/goth/providers/yandex"
  16. )
  17. // SimpleProviderNewFn create goth.Providers without custom url features
  18. type SimpleProviderNewFn func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider
  19. // SimpleProvider is a GothProvider which does not have custom url features
  20. type SimpleProvider struct {
  21. BaseProvider
  22. scopes []string
  23. newFn SimpleProviderNewFn
  24. }
  25. // CreateGothProvider creates a GothProvider from this Provider
  26. func (c *SimpleProvider) CreateGothProvider(providerName, callbackURL string, source *Source) (goth.Provider, error) {
  27. scopes := make([]string, len(c.scopes)+len(source.Scopes))
  28. copy(scopes, c.scopes)
  29. copy(scopes[len(c.scopes):], source.Scopes)
  30. return c.newFn(source.ClientID, source.ClientSecret, callbackURL, scopes...), nil
  31. }
  32. // NewSimpleProvider is a constructor function for simple providers
  33. func NewSimpleProvider(name, displayName string, scopes []string, newFn SimpleProviderNewFn) *SimpleProvider {
  34. return &SimpleProvider{
  35. BaseProvider: BaseProvider{
  36. name: name,
  37. displayName: displayName,
  38. },
  39. scopes: scopes,
  40. newFn: newFn,
  41. }
  42. }
  43. var _ GothProvider = &SimpleProvider{}
  44. func init() {
  45. RegisterGothProvider(
  46. NewSimpleProvider("bitbucket", "Bitbucket", []string{"account"},
  47. func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
  48. return bitbucket.New(clientKey, secret, callbackURL, scopes...)
  49. }))
  50. RegisterGothProvider(
  51. NewSimpleProvider("dropbox", "Dropbox", nil,
  52. func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
  53. return dropbox.New(clientKey, secret, callbackURL, scopes...)
  54. }))
  55. RegisterGothProvider(NewSimpleProvider("facebook", "Facebook", nil,
  56. func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
  57. return facebook.New(clientKey, secret, callbackURL, scopes...)
  58. }))
  59. // named gplus due to legacy gplus -> google migration (Google killed Google+). This ensures old connections still work
  60. RegisterGothProvider(NewSimpleProvider("gplus", "Google", []string{"email"},
  61. func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
  62. if setting.OAuth2Client.UpdateAvatar || setting.OAuth2Client.EnableAutoRegistration {
  63. scopes = append(scopes, "profile")
  64. }
  65. return google.New(clientKey, secret, callbackURL, scopes...)
  66. }))
  67. RegisterGothProvider(NewSimpleProvider("twitter", "Twitter", nil,
  68. func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
  69. return twitter.New(clientKey, secret, callbackURL)
  70. }))
  71. RegisterGothProvider(NewSimpleProvider("discord", "Discord", []string{discord.ScopeIdentify, discord.ScopeEmail},
  72. func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
  73. return discord.New(clientKey, secret, callbackURL, scopes...)
  74. }))
  75. // See https://tech.yandex.com/passport/doc/dg/reference/response-docpage/
  76. RegisterGothProvider(NewSimpleProvider("yandex", "Yandex", []string{"login:email", "login:info", "login:avatar"},
  77. func(clientKey, secret, callbackURL string, scopes ...string) goth.Provider {
  78. return yandex.New(clientKey, secret, callbackURL, scopes...)
  79. }))
  80. RegisterGothProvider(NewSimpleProvider(
  81. "azuread", "Azure AD", nil,
  82. func(clientID, secret, callbackURL string, scopes ...string) goth.Provider {
  83. return azuread.New(clientID, secret, callbackURL, nil, scopes...)
  84. },
  85. ))
  86. RegisterGothProvider(NewSimpleProvider(
  87. "microsoftonline", "Microsoft Online", nil,
  88. func(clientID, secret, callbackURL string, scopes ...string) goth.Provider {
  89. return microsoftonline.New(clientID, secret, callbackURL, scopes...)
  90. },
  91. ))
  92. }