auth.go 3.4 KB

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