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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package auth
  5. import (
  6. "fmt"
  7. "net/http"
  8. "regexp"
  9. "strings"
  10. user_model "code.gitea.io/gitea/models/user"
  11. "code.gitea.io/gitea/modules/auth/webauthn"
  12. gitea_context "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/session"
  15. "code.gitea.io/gitea/modules/setting"
  16. "code.gitea.io/gitea/modules/web/middleware"
  17. )
  18. // Init should be called exactly once when the application starts to allow plugins
  19. // to allocate necessary resources
  20. func Init() {
  21. webauthn.Init()
  22. }
  23. // isAttachmentDownload check if request is a file download (GET) with URL to an attachment
  24. func isAttachmentDownload(req *http.Request) bool {
  25. return strings.HasPrefix(req.URL.Path, "/attachments/") && req.Method == "GET"
  26. }
  27. // isContainerPath checks if the request targets the container endpoint
  28. func isContainerPath(req *http.Request) bool {
  29. return strings.HasPrefix(req.URL.Path, "/v2/")
  30. }
  31. var (
  32. gitRawOrAttachPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/(?:(?:git-(?:(?:upload)|(?:receive))-pack$)|(?:info/refs$)|(?:HEAD$)|(?:objects/)|(?:raw/)|(?:releases/download/)|(?:attachments/))`)
  33. lfsPathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/info/lfs/`)
  34. archivePathRe = regexp.MustCompile(`^/[a-zA-Z0-9_.-]+/[a-zA-Z0-9_.-]+/archive/`)
  35. )
  36. func isGitRawOrAttachPath(req *http.Request) bool {
  37. return gitRawOrAttachPathRe.MatchString(req.URL.Path)
  38. }
  39. func isGitRawOrAttachOrLFSPath(req *http.Request) bool {
  40. if isGitRawOrAttachPath(req) {
  41. return true
  42. }
  43. if setting.LFS.StartServer {
  44. return lfsPathRe.MatchString(req.URL.Path)
  45. }
  46. return false
  47. }
  48. func isArchivePath(req *http.Request) bool {
  49. return archivePathRe.MatchString(req.URL.Path)
  50. }
  51. // handleSignIn clears existing session variables and stores new ones for the specified user object
  52. func handleSignIn(resp http.ResponseWriter, req *http.Request, sess SessionStore, user *user_model.User) {
  53. // We need to regenerate the session...
  54. newSess, err := session.RegenerateSession(resp, req)
  55. if err != nil {
  56. log.Error(fmt.Sprintf("Error regenerating session: %v", err))
  57. } else {
  58. sess = newSess
  59. }
  60. _ = sess.Delete("openid_verified_uri")
  61. _ = sess.Delete("openid_signin_remember")
  62. _ = sess.Delete("openid_determined_email")
  63. _ = sess.Delete("openid_determined_username")
  64. _ = sess.Delete("twofaUid")
  65. _ = sess.Delete("twofaRemember")
  66. _ = sess.Delete("webauthnAssertion")
  67. _ = sess.Delete("linkAccount")
  68. err = sess.Set("uid", user.ID)
  69. if err != nil {
  70. log.Error(fmt.Sprintf("Error setting session: %v", err))
  71. }
  72. err = sess.Set("uname", user.Name)
  73. if err != nil {
  74. log.Error(fmt.Sprintf("Error setting session: %v", err))
  75. }
  76. // Language setting of the user overwrites the one previously set
  77. // If the user does not have a locale set, we save the current one.
  78. if len(user.Language) == 0 {
  79. lc := middleware.Locale(resp, req)
  80. user.Language = lc.Language()
  81. if err := user_model.UpdateUserCols(req.Context(), user, "language"); err != nil {
  82. log.Error(fmt.Sprintf("Error updating user language [user: %d, locale: %s]", user.ID, user.Language))
  83. return
  84. }
  85. }
  86. middleware.SetLocaleCookie(resp, user.Language, 0)
  87. // Clear whatever CSRF has right now, force to generate a new one
  88. if ctx := gitea_context.GetWebContext(req); ctx != nil {
  89. ctx.Csrf.DeleteCookie(ctx)
  90. }
  91. }