auth.go 3.5 KB

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