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.3KB

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