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

auth.go 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package auth
  6. import (
  7. "reflect"
  8. "strings"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth/sso"
  11. "code.gitea.io/gitea/modules/validation"
  12. "gitea.com/macaron/binding"
  13. "gitea.com/macaron/macaron"
  14. "gitea.com/macaron/session"
  15. "github.com/unknwon/com"
  16. )
  17. // IsAPIPath if URL is an api path
  18. func IsAPIPath(url string) bool {
  19. return strings.HasPrefix(url, "/api/")
  20. }
  21. // SignedInUser returns the user object of signed user.
  22. // It returns a bool value to indicate whether user uses basic auth or not.
  23. func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool) {
  24. if !models.HasEngine {
  25. return nil, false
  26. }
  27. // Try to sign in with each of the enabled plugins
  28. for _, ssoMethod := range sso.Methods() {
  29. if !ssoMethod.IsEnabled() {
  30. continue
  31. }
  32. user := ssoMethod.VerifyAuthData(ctx, sess)
  33. if user != nil {
  34. _, isBasic := ssoMethod.(*sso.Basic)
  35. return user, isBasic
  36. }
  37. }
  38. return nil, false
  39. }
  40. // Form form binding interface
  41. type Form interface {
  42. binding.Validator
  43. }
  44. func init() {
  45. binding.SetNameMapper(com.ToSnakeCase)
  46. }
  47. // AssignForm assign form values back to the template data.
  48. func AssignForm(form interface{}, data map[string]interface{}) {
  49. typ := reflect.TypeOf(form)
  50. val := reflect.ValueOf(form)
  51. if typ.Kind() == reflect.Ptr {
  52. typ = typ.Elem()
  53. val = val.Elem()
  54. }
  55. for i := 0; i < typ.NumField(); i++ {
  56. field := typ.Field(i)
  57. fieldName := field.Tag.Get("form")
  58. // Allow ignored fields in the struct
  59. if fieldName == "-" {
  60. continue
  61. } else if len(fieldName) == 0 {
  62. fieldName = com.ToSnakeCase(field.Name)
  63. }
  64. data[fieldName] = val.Field(i).Interface()
  65. }
  66. }
  67. func getRuleBody(field reflect.StructField, prefix string) string {
  68. for _, rule := range strings.Split(field.Tag.Get("binding"), ";") {
  69. if strings.HasPrefix(rule, prefix) {
  70. return rule[len(prefix) : len(rule)-1]
  71. }
  72. }
  73. return ""
  74. }
  75. // GetSize get size int form tag
  76. func GetSize(field reflect.StructField) string {
  77. return getRuleBody(field, "Size(")
  78. }
  79. // GetMinSize get minimal size in form tag
  80. func GetMinSize(field reflect.StructField) string {
  81. return getRuleBody(field, "MinSize(")
  82. }
  83. // GetMaxSize get max size in form tag
  84. func GetMaxSize(field reflect.StructField) string {
  85. return getRuleBody(field, "MaxSize(")
  86. }
  87. // GetInclude get include in form tag
  88. func GetInclude(field reflect.StructField) string {
  89. return getRuleBody(field, "Include(")
  90. }
  91. func validate(errs binding.Errors, data map[string]interface{}, f Form, l macaron.Locale) binding.Errors {
  92. if errs.Len() == 0 {
  93. return errs
  94. }
  95. data["HasError"] = true
  96. // If the field with name errs[0].FieldNames[0] is not found in form
  97. // somehow, some code later on will panic on Data["ErrorMsg"].(string).
  98. // So initialize it to some default.
  99. data["ErrorMsg"] = l.Tr("form.unknown_error")
  100. AssignForm(f, data)
  101. typ := reflect.TypeOf(f)
  102. val := reflect.ValueOf(f)
  103. if typ.Kind() == reflect.Ptr {
  104. typ = typ.Elem()
  105. val = val.Elem()
  106. }
  107. if field, ok := typ.FieldByName(errs[0].FieldNames[0]); ok {
  108. fieldName := field.Tag.Get("form")
  109. if fieldName != "-" {
  110. data["Err_"+field.Name] = true
  111. trName := field.Tag.Get("locale")
  112. if len(trName) == 0 {
  113. trName = l.Tr("form." + field.Name)
  114. } else {
  115. trName = l.Tr(trName)
  116. }
  117. switch errs[0].Classification {
  118. case binding.ERR_REQUIRED:
  119. data["ErrorMsg"] = trName + l.Tr("form.require_error")
  120. case binding.ERR_ALPHA_DASH:
  121. data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_error")
  122. case binding.ERR_ALPHA_DASH_DOT:
  123. data["ErrorMsg"] = trName + l.Tr("form.alpha_dash_dot_error")
  124. case validation.ErrGitRefName:
  125. data["ErrorMsg"] = trName + l.Tr("form.git_ref_name_error")
  126. case binding.ERR_SIZE:
  127. data["ErrorMsg"] = trName + l.Tr("form.size_error", GetSize(field))
  128. case binding.ERR_MIN_SIZE:
  129. data["ErrorMsg"] = trName + l.Tr("form.min_size_error", GetMinSize(field))
  130. case binding.ERR_MAX_SIZE:
  131. data["ErrorMsg"] = trName + l.Tr("form.max_size_error", GetMaxSize(field))
  132. case binding.ERR_EMAIL:
  133. data["ErrorMsg"] = trName + l.Tr("form.email_error")
  134. case binding.ERR_URL:
  135. data["ErrorMsg"] = trName + l.Tr("form.url_error")
  136. case binding.ERR_INCLUDE:
  137. data["ErrorMsg"] = trName + l.Tr("form.include_error", GetInclude(field))
  138. case validation.ErrGlobPattern:
  139. data["ErrorMsg"] = trName + l.Tr("form.glob_pattern_error", errs[0].Message)
  140. default:
  141. data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification
  142. }
  143. return errs
  144. }
  145. }
  146. return errs
  147. }