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.

auth.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package auth
  6. import (
  7. "fmt"
  8. "net/http"
  9. "reflect"
  10. "regexp"
  11. "strings"
  12. "code.gitea.io/gitea/models/db"
  13. user_model "code.gitea.io/gitea/models/user"
  14. "code.gitea.io/gitea/modules/auth/webauthn"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/session"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/web/middleware"
  19. )
  20. // authMethods contains the list of authentication plugins in the order they are expected to be
  21. // executed.
  22. //
  23. // The OAuth2 plugin is expected to be executed first, as it must ignore the user id stored
  24. // in the session (if there is a user id stored in session other plugins might return the user
  25. // object for that id).
  26. //
  27. // The Session plugin is expected to be executed second, in order to skip authentication
  28. // for users that have already signed in.
  29. var authMethods = []Method{
  30. &OAuth2{},
  31. &Basic{},
  32. &Session{},
  33. }
  34. // The purpose of the following three function variables is to let the linter know that
  35. // those functions are not dead code and are actually being used
  36. var (
  37. _ = handleSignIn
  38. )
  39. // Methods returns the instances of all registered methods
  40. func Methods() []Method {
  41. return authMethods
  42. }
  43. // Register adds the specified instance to the list of available methods
  44. func Register(method Method) {
  45. authMethods = append(authMethods, method)
  46. }
  47. // Init should be called exactly once when the application starts to allow plugins
  48. // to allocate necessary resources
  49. func Init() {
  50. if setting.Service.EnableReverseProxyAuth {
  51. Register(&ReverseProxy{})
  52. }
  53. specialInit()
  54. for _, method := range Methods() {
  55. initializable, ok := method.(Initializable)
  56. if !ok {
  57. continue
  58. }
  59. err := initializable.Init()
  60. if err != nil {
  61. log.Error("Could not initialize '%s' auth method, error: %s", reflect.TypeOf(method).String(), err)
  62. }
  63. }
  64. webauthn.Init()
  65. }
  66. // Free should be called exactly once when the application is terminating to allow Auth plugins
  67. // to release necessary resources
  68. func Free() {
  69. for _, method := range Methods() {
  70. freeable, ok := method.(Freeable)
  71. if !ok {
  72. continue
  73. }
  74. err := freeable.Free()
  75. if err != nil {
  76. log.Error("Could not free '%s' auth method, error: %s", reflect.TypeOf(method).String(), err)
  77. }
  78. }
  79. }
  80. // isAttachmentDownload check if request is a file download (GET) with URL to an attachment
  81. func isAttachmentDownload(req *http.Request) bool {
  82. return strings.HasPrefix(req.URL.Path, "/attachments/") && req.Method == "GET"
  83. }
  84. var gitRawReleasePathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/))`)
  85. var lfsPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`)
  86. func isGitRawReleaseOrLFSPath(req *http.Request) bool {
  87. if gitRawReleasePathRe.MatchString(req.URL.Path) {
  88. return true
  89. }
  90. if setting.LFS.StartServer {
  91. return lfsPathRe.MatchString(req.URL.Path)
  92. }
  93. return false
  94. }
  95. // handleSignIn clears existing session variables and stores new ones for the specified user object
  96. func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore, user *user_model.User) {
  97. // We need to regenerate the session...
  98. newSess, err := session.RegenerateSession(resp, req)
  99. if err != nil {
  100. log.Error(fmt.Sprintf("Error regenerating session: %v", err))
  101. } else {
  102. sess = newSess
  103. }
  104. _ = sess.Delete("openid_verified_uri")
  105. _ = sess.Delete("openid_signin_remember")
  106. _ = sess.Delete("openid_determined_email")
  107. _ = sess.Delete("openid_determined_username")
  108. _ = sess.Delete("twofaUid")
  109. _ = sess.Delete("twofaRemember")
  110. _ = sess.Delete("webauthnAssertion")
  111. _ = sess.Delete("linkAccount")
  112. err = sess.Set("uid", user.ID)
  113. if err != nil {
  114. log.Error(fmt.Sprintf("Error setting session: %v", err))
  115. }
  116. err = sess.Set("uname", user.Name)
  117. if err != nil {
  118. log.Error(fmt.Sprintf("Error setting session: %v", err))
  119. }
  120. // Language setting of the user overwrites the one previously set
  121. // If the user does not have a locale set, we save the current one.
  122. if len(user.Language) == 0 {
  123. lc := middleware.Locale(resp, req)
  124. user.Language = lc.Language()
  125. if err := user_model.UpdateUserCols(db.DefaultContext, user, "language"); err != nil {
  126. log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", user.ID, user.Language))
  127. return
  128. }
  129. }
  130. middleware.SetLocaleCookie(resp, user.Language, 0)
  131. // Clear whatever CSRF has right now, force to generate a new one
  132. middleware.DeleteCSRFCookie(resp)
  133. }