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.

basic.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "net/http"
  7. "strings"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/modules/base"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/modules/timeutil"
  14. "code.gitea.io/gitea/modules/web/middleware"
  15. )
  16. // Ensure the struct implements the interface.
  17. var (
  18. _ Method = &Basic{}
  19. _ Named = &Basic{}
  20. )
  21. // BasicMethodName is the constant name of the basic authentication method
  22. const BasicMethodName = "basic"
  23. // Basic implements the Auth interface and authenticates requests (API requests
  24. // only) by looking for Basic authentication data or "x-oauth-basic" token in the "Authorization"
  25. // header.
  26. type Basic struct{}
  27. // Name represents the name of auth method
  28. func (b *Basic) Name() string {
  29. return BasicMethodName
  30. }
  31. // Verify extracts and validates Basic data (username and password/token) from the
  32. // "Authorization" header of the request and returns the corresponding user object for that
  33. // name/token on successful validation.
  34. // Returns nil if header is empty or validation fails.
  35. func (b *Basic) Verify(req *http.Request, w http.ResponseWriter, store DataStore, sess SessionStore) (*user_model.User, error) {
  36. // Basic authentication should only fire on API, Download or on Git or LFSPaths
  37. if !middleware.IsAPIPath(req) && !isContainerPath(req) && !isAttachmentDownload(req) && !isGitRawReleaseOrLFSPath(req) {
  38. return nil, nil
  39. }
  40. baHead := req.Header.Get("Authorization")
  41. if len(baHead) == 0 {
  42. return nil, nil
  43. }
  44. auths := strings.SplitN(baHead, " ", 2)
  45. if len(auths) != 2 || (strings.ToLower(auths[0]) != "basic") {
  46. return nil, nil
  47. }
  48. uname, passwd, _ := base.BasicAuthDecode(auths[1])
  49. // Check if username or password is a token
  50. isUsernameToken := len(passwd) == 0 || passwd == "x-oauth-basic"
  51. // Assume username is token
  52. authToken := uname
  53. if !isUsernameToken {
  54. log.Trace("Basic Authorization: Attempting login for: %s", uname)
  55. // Assume password is token
  56. authToken = passwd
  57. } else {
  58. log.Trace("Basic Authorization: Attempting login with username as token")
  59. }
  60. uid := CheckOAuthAccessToken(authToken)
  61. if uid != 0 {
  62. log.Trace("Basic Authorization: Valid OAuthAccessToken for user[%d]", uid)
  63. u, err := user_model.GetUserByID(req.Context(), uid)
  64. if err != nil {
  65. log.Error("GetUserByID: %v", err)
  66. return nil, err
  67. }
  68. store.GetData()["IsApiToken"] = true
  69. return u, nil
  70. }
  71. token, err := auth_model.GetAccessTokenBySHA(authToken)
  72. if err == nil {
  73. log.Trace("Basic Authorization: Valid AccessToken for user[%d]", uid)
  74. u, err := user_model.GetUserByID(req.Context(), token.UID)
  75. if err != nil {
  76. log.Error("GetUserByID: %v", err)
  77. return nil, err
  78. }
  79. token.UpdatedUnix = timeutil.TimeStampNow()
  80. if err = auth_model.UpdateAccessToken(token); err != nil {
  81. log.Error("UpdateAccessToken: %v", err)
  82. }
  83. store.GetData()["IsApiToken"] = true
  84. return u, nil
  85. } else if !auth_model.IsErrAccessTokenNotExist(err) && !auth_model.IsErrAccessTokenEmpty(err) {
  86. log.Error("GetAccessTokenBySha: %v", err)
  87. }
  88. if !setting.Service.EnableBasicAuth {
  89. return nil, nil
  90. }
  91. log.Trace("Basic Authorization: Attempting SignIn for %s", uname)
  92. u, source, err := UserSignIn(uname, passwd)
  93. if err != nil {
  94. if !user_model.IsErrUserNotExist(err) {
  95. log.Error("UserSignIn: %v", err)
  96. }
  97. return nil, err
  98. }
  99. if skipper, ok := source.Cfg.(LocalTwoFASkipper); ok && skipper.IsSkipLocalTwoFA() {
  100. store.GetData()["SkipLocalTwoFA"] = true
  101. }
  102. log.Trace("Basic Authorization: Logged in user %-v", u)
  103. return u, nil
  104. }