You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

auth.go 5.0 kB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. "net/http"
  7. "reflect"
  8. "strings"
  9. "github.com/Unknwon/macaron"
  10. "github.com/macaron-contrib/binding"
  11. "github.com/macaron-contrib/session"
  12. "github.com/gogits/gogs/models"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. // SignedInId returns the id of signed in user.
  18. func SignedInId(req *http.Request, sess session.Store) int64 {
  19. if !models.HasEngine {
  20. return 0
  21. }
  22. // API calls need to check access token.
  23. if strings.HasPrefix(req.URL.Path, "/api/") {
  24. auHead := req.Header.Get("Authorization")
  25. if len(auHead) > 0 {
  26. auths := strings.Fields(auHead)
  27. if len(auths) == 2 && auths[0] == "token" {
  28. t, err := models.GetAccessTokenBySha(auths[1])
  29. if err != nil {
  30. if err != models.ErrAccessTokenNotExist {
  31. log.Error(4, "GetAccessTokenBySha: %v", err)
  32. }
  33. return 0
  34. }
  35. return t.Uid
  36. }
  37. }
  38. }
  39. uid := sess.Get("uid")
  40. if uid == nil {
  41. return 0
  42. }
  43. if id, ok := uid.(int64); ok {
  44. if _, err := models.GetUserById(id); err != nil {
  45. if err != models.ErrUserNotExist {
  46. log.Error(4, "GetUserById: %v", err)
  47. }
  48. return 0
  49. }
  50. return id
  51. }
  52. return 0
  53. }
  54. // SignedInUser returns the user object of signed user.
  55. func SignedInUser(req *http.Request, sess session.Store) (*models.User, bool) {
  56. if !models.HasEngine {
  57. return nil, false
  58. }
  59. uid := SignedInId(req, sess)
  60. if uid <= 0 {
  61. if setting.Service.EnableReverseProxyAuth {
  62. webAuthUser := req.Header.Get(setting.ReverseProxyAuthUser)
  63. if len(webAuthUser) > 0 {
  64. u, err := models.GetUserByName(webAuthUser)
  65. if err != nil {
  66. if err != models.ErrUserNotExist {
  67. log.Error(4, "GetUserByName: %v", err)
  68. }
  69. return nil, false
  70. }
  71. return u, false
  72. }
  73. }
  74. // Check with basic auth.
  75. baHead := req.Header.Get("Authorization")
  76. if len(baHead) > 0 {
  77. auths := strings.Fields(baHead)
  78. if len(auths) == 2 && auths[0] == "Basic" {
  79. uname, passwd, _ := base.BasicAuthDecode(auths[1])
  80. u, err := models.GetUserByName(uname)
  81. if err != nil {
  82. if err != models.ErrUserNotExist {
  83. log.Error(4, "GetUserByName: %v", err)
  84. }
  85. return nil, false
  86. }
  87. if u.ValidtePassword(passwd) {
  88. return u, true
  89. }
  90. }
  91. }
  92. return nil, false
  93. }
  94. u, err := models.GetUserById(uid)
  95. if err != nil {
  96. log.Error(4, "GetUserById: %v", err)
  97. return nil, false
  98. }
  99. return u, false
  100. }
  101. type Form interface {
  102. binding.Validator
  103. }
  104. // AssignForm assign form values back to the template data.
  105. func AssignForm(form interface{}, data map[string]interface{}) {
  106. typ := reflect.TypeOf(form)
  107. val := reflect.ValueOf(form)
  108. if typ.Kind() == reflect.Ptr {
  109. typ = typ.Elem()
  110. val = val.Elem()
  111. }
  112. for i := 0; i < typ.NumField(); i++ {
  113. field := typ.Field(i)
  114. fieldName := field.Tag.Get("form")
  115. // Allow ignored fields in the struct
  116. if fieldName == "-" {
  117. continue
  118. }
  119. data[fieldName] = val.Field(i).Interface()
  120. }
  121. }
  122. func getSize(field reflect.StructField, prefix string) string {
  123. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  124. if strings.HasPrefix(rule, prefix) {
  125. return rule[8 : len(rule)-1]
  126. }
  127. }
  128. return ""
  129. }
  130. func GetMinSize(field reflect.StructField) string {
  131. return getSize(field, "MinSize(")
  132. }
  133. func GetMaxSize(field reflect.StructField) string {
  134. return getSize(field, "MaxSize(")
  135. }
  136. func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaron.Locale) binding.Errors {
  137. if errs.Len() == 0 {
  138. return errs
  139. }
  140. data["HasError"] = true
  141. AssignForm(f, data)
  142. typ := reflect.TypeOf(f)
  143. val := reflect.ValueOf(f)
  144. if typ.Kind() == reflect.Ptr {
  145. typ = typ.Elem()
  146. val = val.Elem()
  147. }
  148. for i := 0; i < typ.NumField(); i++ {
  149. field := typ.Field(i)
  150. fieldName := field.Tag.Get("form")
  151. // Allow ignored fields in the struct
  152. if fieldName == "-" {
  153. continue
  154. }
  155. if errs[0].FieldNames[0] == field.Name {
  156. data["Err_"+field.Name] = true
  157. trName := l.Tr("form." + field.Name)
  158. switch errs[0].Classification {
  159. case binding.RequiredError:
  160. data["ErrorMsg"] = trName + l.Tr("form.require_error")
  161. case binding.AlphaDashError:
  162. data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error")
  163. case binding.AlphaDashDotError:
  164. data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error")
  165. case binding.MinSizeError:
  166. data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinSize(field))
  167. case binding.MaxSizeError:
  168. data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMaxSize(field))
  169. case binding.EmailError:
  170. data["ErrorMsg"] = trName + l.Tr("form.email_error")
  171. case binding.UrlError:
  172. data["ErrorMsg"] = trName + l.Tr("form.url_error")
  173. default:
  174. data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification
  175. }
  176. return errs
  177. }
  178. }
  179. return errs
  180. }