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