auth.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2014 The Gogs 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 auth
  5. import (
  6. "strings"
  7. "github.com/go-macaron/session"
  8. gouuid "github.com/satori/go.uuid"
  9. "gopkg.in/macaron.v1"
  10. log "unknwon.dev/clog/v2"
  11. "gogs.io/gogs/internal/conf"
  12. "gogs.io/gogs/internal/db"
  13. "gogs.io/gogs/internal/tool"
  14. )
  15. func IsAPIPath(url string) bool {
  16. return strings.HasPrefix(url, "/api/")
  17. }
  18. // SignedInID returns the id of signed in user, along with one bool value which indicates whether user uses token
  19. // authentication.
  20. func SignedInID(c *macaron.Context, sess session.Store) (_ int64, isTokenAuth bool) {
  21. if !db.HasEngine {
  22. return 0, false
  23. }
  24. // Check access token.
  25. if IsAPIPath(c.Req.URL.Path) {
  26. tokenSHA := c.Query("token")
  27. if len(tokenSHA) <= 0 {
  28. tokenSHA = c.Query("access_token")
  29. }
  30. if len(tokenSHA) == 0 {
  31. // Well, check with header again.
  32. auHead := c.Req.Header.Get("Authorization")
  33. if len(auHead) > 0 {
  34. auths := strings.Fields(auHead)
  35. if len(auths) == 2 && auths[0] == "token" {
  36. tokenSHA = auths[1]
  37. }
  38. }
  39. }
  40. // Let's see if token is valid.
  41. if len(tokenSHA) > 0 {
  42. t, err := db.AccessTokens.GetBySHA(tokenSHA)
  43. if err != nil {
  44. if !db.IsErrAccessTokenNotExist(err) {
  45. log.Error("GetAccessTokenBySHA: %v", err)
  46. }
  47. return 0, false
  48. }
  49. if err = db.AccessTokens.Save(t); err != nil {
  50. log.Error("UpdateAccessToken: %v", err)
  51. }
  52. return t.UserID, true
  53. }
  54. }
  55. uid := sess.Get("uid")
  56. if uid == nil {
  57. return 0, false
  58. }
  59. if id, ok := uid.(int64); ok {
  60. if _, err := db.GetUserByID(id); err != nil {
  61. if !db.IsErrUserNotExist(err) {
  62. log.Error("Failed to get user by ID: %v", err)
  63. }
  64. return 0, false
  65. }
  66. return id, false
  67. }
  68. return 0, false
  69. }
  70. // SignedInUser returns the user object of signed in user, along with two bool values,
  71. // which indicate whether user uses HTTP Basic Authentication or token authentication respectively.
  72. func SignedInUser(ctx *macaron.Context, sess session.Store) (_ *db.User, isBasicAuth bool, isTokenAuth bool) {
  73. if !db.HasEngine {
  74. return nil, false, false
  75. }
  76. uid, isTokenAuth := SignedInID(ctx, sess)
  77. if uid <= 0 {
  78. if conf.Auth.EnableReverseProxyAuthentication {
  79. webAuthUser := ctx.Req.Header.Get(conf.Auth.ReverseProxyAuthenticationHeader)
  80. if len(webAuthUser) > 0 {
  81. u, err := db.GetUserByName(webAuthUser)
  82. if err != nil {
  83. if !db.IsErrUserNotExist(err) {
  84. log.Error("Failed to get user by name: %v", err)
  85. return nil, false, false
  86. }
  87. // Check if enabled auto-registration.
  88. if conf.Auth.EnableReverseProxyAutoRegistration {
  89. u := &db.User{
  90. Name: webAuthUser,
  91. Email: gouuid.NewV4().String() + "@localhost",
  92. Passwd: webAuthUser,
  93. IsActive: true,
  94. }
  95. if err = db.CreateUser(u); err != nil {
  96. // FIXME: should I create a system notice?
  97. log.Error("Failed to create user: %v", err)
  98. return nil, false, false
  99. } else {
  100. return u, false, false
  101. }
  102. }
  103. }
  104. return u, false, false
  105. }
  106. }
  107. // Check with basic auth.
  108. baHead := ctx.Req.Header.Get("Authorization")
  109. if len(baHead) > 0 {
  110. auths := strings.Fields(baHead)
  111. if len(auths) == 2 && auths[0] == "Basic" {
  112. uname, passwd, _ := tool.BasicAuthDecode(auths[1])
  113. u, err := db.Users.Authenticate(uname, passwd, -1)
  114. if err != nil {
  115. if !db.IsErrUserNotExist(err) {
  116. log.Error("Failed to authenticate user: %v", err)
  117. }
  118. return nil, false, false
  119. }
  120. return u, true, false
  121. }
  122. }
  123. return nil, false, false
  124. }
  125. u, err := db.GetUserByID(uid)
  126. if err != nil {
  127. log.Error("GetUserByID: %v", err)
  128. return nil, false, false
  129. }
  130. return u, false, isTokenAuth
  131. }