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

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