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 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 oauth2
  5. import (
  6. "math"
  7. "net/http"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/go-xorm/xorm"
  11. "github.com/lafriks/xormstore"
  12. "github.com/markbates/goth"
  13. "github.com/markbates/goth/gothic"
  14. "github.com/markbates/goth/providers/bitbucket"
  15. "github.com/markbates/goth/providers/discord"
  16. "github.com/markbates/goth/providers/dropbox"
  17. "github.com/markbates/goth/providers/facebook"
  18. "github.com/markbates/goth/providers/github"
  19. "github.com/markbates/goth/providers/gitlab"
  20. "github.com/markbates/goth/providers/gplus"
  21. "github.com/markbates/goth/providers/openidConnect"
  22. "github.com/markbates/goth/providers/twitter"
  23. "github.com/satori/go.uuid"
  24. )
  25. var (
  26. sessionUsersStoreKey = "gitea-oauth2-sessions"
  27. providerHeaderKey = "gitea-oauth2-provider"
  28. )
  29. // CustomURLMapping describes the urls values to use when customizing OAuth2 provider URLs
  30. type CustomURLMapping struct {
  31. AuthURL string
  32. TokenURL string
  33. ProfileURL string
  34. EmailURL string
  35. }
  36. // Init initialize the setup of the OAuth2 library
  37. func Init(x *xorm.Engine) error {
  38. store, err := xormstore.NewOptions(x, xormstore.Options{
  39. TableName: "oauth2_session",
  40. }, []byte(sessionUsersStoreKey))
  41. if err != nil {
  42. return err
  43. }
  44. // according to the Goth lib:
  45. // set the maxLength of the cookies stored on the disk to a larger number to prevent issues with:
  46. // securecookie: the value is too long
  47. // when using OpenID Connect , since this can contain a large amount of extra information in the id_token
  48. // Note, when using the FilesystemStore only the session.ID is written to a browser cookie, so this is explicit for the storage on disk
  49. store.MaxLength(math.MaxInt16)
  50. gothic.Store = store
  51. gothic.SetState = func(req *http.Request) string {
  52. return uuid.NewV4().String()
  53. }
  54. gothic.GetProviderName = func(req *http.Request) (string, error) {
  55. return req.Header.Get(providerHeaderKey), nil
  56. }
  57. return nil
  58. }
  59. // Auth OAuth2 auth service
  60. func Auth(provider string, request *http.Request, response http.ResponseWriter) error {
  61. // not sure if goth is thread safe (?) when using multiple providers
  62. request.Header.Set(providerHeaderKey, provider)
  63. // don't use the default gothic begin handler to prevent issues when some error occurs
  64. // normally the gothic library will write some custom stuff to the response instead of our own nice error page
  65. //gothic.BeginAuthHandler(response, request)
  66. url, err := gothic.GetAuthURL(response, request)
  67. if err == nil {
  68. http.Redirect(response, request, url, http.StatusTemporaryRedirect)
  69. }
  70. return err
  71. }
  72. // ProviderCallback handles OAuth callback, resolve to a goth user and send back to original url
  73. // this will trigger a new authentication request, but because we save it in the session we can use that
  74. func ProviderCallback(provider string, request *http.Request, response http.ResponseWriter) (goth.User, error) {
  75. // not sure if goth is thread safe (?) when using multiple providers
  76. request.Header.Set(providerHeaderKey, provider)
  77. user, err := gothic.CompleteUserAuth(response, request)
  78. if err != nil {
  79. return user, err
  80. }
  81. return user, nil
  82. }
  83. // RegisterProvider register a OAuth2 provider in goth lib
  84. func RegisterProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL string, customURLMapping *CustomURLMapping) error {
  85. provider, err := createProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL, customURLMapping)
  86. if err == nil && provider != nil {
  87. goth.UseProviders(provider)
  88. }
  89. return err
  90. }
  91. // RemoveProvider removes the given OAuth2 provider from the goth lib
  92. func RemoveProvider(providerName string) {
  93. delete(goth.GetProviders(), providerName)
  94. }
  95. // used to create different types of goth providers
  96. func createProvider(providerName, providerType, clientID, clientSecret, openIDConnectAutoDiscoveryURL string, customURLMapping *CustomURLMapping) (goth.Provider, error) {
  97. callbackURL := setting.AppURL + "user/oauth2/" + providerName + "/callback"
  98. var provider goth.Provider
  99. var err error
  100. switch providerType {
  101. case "bitbucket":
  102. provider = bitbucket.New(clientID, clientSecret, callbackURL, "account")
  103. case "dropbox":
  104. provider = dropbox.New(clientID, clientSecret, callbackURL)
  105. case "facebook":
  106. provider = facebook.New(clientID, clientSecret, callbackURL, "email")
  107. case "github":
  108. authURL := github.AuthURL
  109. tokenURL := github.TokenURL
  110. profileURL := github.ProfileURL
  111. emailURL := github.EmailURL
  112. if customURLMapping != nil {
  113. if len(customURLMapping.AuthURL) > 0 {
  114. authURL = customURLMapping.AuthURL
  115. }
  116. if len(customURLMapping.TokenURL) > 0 {
  117. tokenURL = customURLMapping.TokenURL
  118. }
  119. if len(customURLMapping.ProfileURL) > 0 {
  120. profileURL = customURLMapping.ProfileURL
  121. }
  122. if len(customURLMapping.EmailURL) > 0 {
  123. emailURL = customURLMapping.EmailURL
  124. }
  125. }
  126. provider = github.NewCustomisedURL(clientID, clientSecret, callbackURL, authURL, tokenURL, profileURL, emailURL)
  127. case "gitlab":
  128. authURL := gitlab.AuthURL
  129. tokenURL := gitlab.TokenURL
  130. profileURL := gitlab.ProfileURL
  131. if customURLMapping != nil {
  132. if len(customURLMapping.AuthURL) > 0 {
  133. authURL = customURLMapping.AuthURL
  134. }
  135. if len(customURLMapping.TokenURL) > 0 {
  136. tokenURL = customURLMapping.TokenURL
  137. }
  138. if len(customURLMapping.ProfileURL) > 0 {
  139. profileURL = customURLMapping.ProfileURL
  140. }
  141. }
  142. provider = gitlab.NewCustomisedURL(clientID, clientSecret, callbackURL, authURL, tokenURL, profileURL, "read_user")
  143. case "gplus":
  144. provider = gplus.New(clientID, clientSecret, callbackURL, "email")
  145. case "openidConnect":
  146. if provider, err = openidConnect.New(clientID, clientSecret, callbackURL, openIDConnectAutoDiscoveryURL); err != nil {
  147. log.Warn("Failed to create OpenID Connect Provider with name '%s' with url '%s': %v", providerName, openIDConnectAutoDiscoveryURL, err)
  148. }
  149. case "twitter":
  150. provider = twitter.NewAuthenticate(clientID, clientSecret, callbackURL)
  151. case "discord":
  152. provider = discord.New(clientID, clientSecret, callbackURL, discord.ScopeIdentify, discord.ScopeEmail)
  153. }
  154. // always set the name if provider is created so we can support multiple setups of 1 provider
  155. if err == nil && provider != nil {
  156. provider.SetName(providerName)
  157. }
  158. return provider, err
  159. }
  160. // GetDefaultTokenURL return the default token url for the given provider
  161. func GetDefaultTokenURL(provider string) string {
  162. switch provider {
  163. case "github":
  164. return github.TokenURL
  165. case "gitlab":
  166. return gitlab.TokenURL
  167. }
  168. return ""
  169. }
  170. // GetDefaultAuthURL return the default authorize url for the given provider
  171. func GetDefaultAuthURL(provider string) string {
  172. switch provider {
  173. case "github":
  174. return github.AuthURL
  175. case "gitlab":
  176. return gitlab.AuthURL
  177. }
  178. return ""
  179. }
  180. // GetDefaultProfileURL return the default profile url for the given provider
  181. func GetDefaultProfileURL(provider string) string {
  182. switch provider {
  183. case "github":
  184. return github.ProfileURL
  185. case "gitlab":
  186. return gitlab.ProfileURL
  187. }
  188. return ""
  189. }
  190. // GetDefaultEmailURL return the default email url for the given provider
  191. func GetDefaultEmailURL(provider string) string {
  192. if provider == "github" {
  193. return github.EmailURL
  194. }
  195. return ""
  196. }