您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2019 The Gitea 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 password
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "math/big"
  9. "strings"
  10. "sync"
  11. "code.gitea.io/gitea/modules/context"
  12. "code.gitea.io/gitea/modules/setting"
  13. )
  14. // complexity contains information about a particular kind of password complexity
  15. type complexity struct {
  16. ValidChars string
  17. TrNameOne string
  18. }
  19. var (
  20. matchComplexityOnce sync.Once
  21. validChars string
  22. requiredList []complexity
  23. charComplexities = map[string]complexity{
  24. "lower": {
  25. `abcdefghijklmnopqrstuvwxyz`,
  26. "form.password_lowercase_one",
  27. },
  28. "upper": {
  29. `ABCDEFGHIJKLMNOPQRSTUVWXYZ`,
  30. "form.password_uppercase_one",
  31. },
  32. "digit": {
  33. `0123456789`,
  34. "form.password_digit_one",
  35. },
  36. "spec": {
  37. ` !"#$%&'()*+,-./:;<=>?@[\]^_{|}~` + "`",
  38. "form.password_special_one",
  39. },
  40. }
  41. )
  42. // NewComplexity for preparation
  43. func NewComplexity() {
  44. matchComplexityOnce.Do(func() {
  45. setupComplexity(setting.PasswordComplexity)
  46. })
  47. }
  48. func setupComplexity(values []string) {
  49. if len(values) != 1 || values[0] != "off" {
  50. for _, val := range values {
  51. if complex, ok := charComplexities[val]; ok {
  52. validChars += complex.ValidChars
  53. requiredList = append(requiredList, complex)
  54. }
  55. }
  56. if len(requiredList) == 0 {
  57. // No valid character classes found; use all classes as default
  58. for _, complex := range charComplexities {
  59. validChars += complex.ValidChars
  60. requiredList = append(requiredList, complex)
  61. }
  62. }
  63. }
  64. if validChars == "" {
  65. // No complexities to check; provide a sensible default for password generation
  66. validChars = charComplexities["lower"].ValidChars + charComplexities["upper"].ValidChars + charComplexities["digit"].ValidChars
  67. }
  68. }
  69. // IsComplexEnough return True if password meets complexity settings
  70. func IsComplexEnough(pwd string) bool {
  71. NewComplexity()
  72. if len(validChars) > 0 {
  73. for _, req := range requiredList {
  74. if !strings.ContainsAny(req.ValidChars, pwd) {
  75. return false
  76. }
  77. }
  78. }
  79. return true
  80. }
  81. // Generate a random password
  82. func Generate(n int) (string, error) {
  83. NewComplexity()
  84. buffer := make([]byte, n)
  85. max := big.NewInt(int64(len(validChars)))
  86. for {
  87. for j := 0; j < n; j++ {
  88. rnd, err := rand.Int(rand.Reader, max)
  89. if err != nil {
  90. return "", err
  91. }
  92. buffer[j] = validChars[rnd.Int64()]
  93. }
  94. if IsComplexEnough(string(buffer)) && string(buffer[0]) != " " && string(buffer[n-1]) != " " {
  95. return string(buffer), nil
  96. }
  97. }
  98. }
  99. // BuildComplexityError builds the error message when password complexity checks fail
  100. func BuildComplexityError(ctx *context.Context) string {
  101. var buffer bytes.Buffer
  102. buffer.WriteString(ctx.Tr("form.password_complexity"))
  103. buffer.WriteString("<ul>")
  104. for _, c := range requiredList {
  105. buffer.WriteString("<li>")
  106. buffer.WriteString(ctx.Tr(c.TrNameOne))
  107. buffer.WriteString("</li>")
  108. }
  109. buffer.WriteString("</ul>")
  110. return buffer.String()
  111. }