mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
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.
128 lines
3.1 KiB
128 lines
3.1 KiB
5 years ago
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package password
|
||
|
|
||
|
import (
|
||
5 years ago
|
"bytes"
|
||
4 years ago
|
goContext "context"
|
||
5 years ago
|
"crypto/rand"
|
||
|
"math/big"
|
||
5 years ago
|
"strings"
|
||
5 years ago
|
"sync"
|
||
|
|
||
|
"code.gitea.io/gitea/modules/setting"
|
||
2 years ago
|
"code.gitea.io/gitea/modules/translation"
|
||
5 years ago
|
)
|
||
|
|
||
5 years ago
|
// complexity contains information about a particular kind of password complexity
|
||
|
type complexity struct {
|
||
|
ValidChars string
|
||
|
TrNameOne string
|
||
|
}
|
||
|
|
||
5 years ago
|
var (
|
||
|
matchComplexityOnce sync.Once
|
||
|
validChars string
|
||
5 years ago
|
requiredList []complexity
|
||
5 years ago
|
|
||
5 years ago
|
charComplexities = map[string]complexity{
|
||
|
"lower": {
|
||
|
`abcdefghijklmnopqrstuvwxyz`,
|
||
|
"form.password_lowercase_one",
|
||
|
},
|
||
|
"upper": {
|
||
|
`ABCDEFGHIJKLMNOPQRSTUVWXYZ`,
|
||
|
"form.password_uppercase_one",
|
||
|
},
|
||
|
"digit": {
|
||
|
`0123456789`,
|
||
|
"form.password_digit_one",
|
||
|
},
|
||
|
"spec": {
|
||
|
` !"#$%&'()*+,-./:;<=>?@[\]^_{|}~` + "`",
|
||
|
"form.password_special_one",
|
||
|
},
|
||
5 years ago
|
}
|
||
|
)
|
||
5 years ago
|
|
||
|
// NewComplexity for preparation
|
||
|
func NewComplexity() {
|
||
|
matchComplexityOnce.Do(func() {
|
||
5 years ago
|
setupComplexity(setting.PasswordComplexity)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func setupComplexity(values []string) {
|
||
|
if len(values) != 1 || values[0] != "off" {
|
||
|
for _, val := range values {
|
||
5 years ago
|
if complex, ok := charComplexities[val]; ok {
|
||
|
validChars += complex.ValidChars
|
||
|
requiredList = append(requiredList, complex)
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
5 years ago
|
if len(requiredList) == 0 {
|
||
5 years ago
|
// No valid character classes found; use all classes as default
|
||
5 years ago
|
for _, complex := range charComplexities {
|
||
|
validChars += complex.ValidChars
|
||
|
requiredList = append(requiredList, complex)
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
}
|
||
|
if validChars == "" {
|
||
|
// No complexities to check; provide a sensible default for password generation
|
||
5 years ago
|
validChars = charComplexities["lower"].ValidChars + charComplexities["upper"].ValidChars + charComplexities["digit"].ValidChars
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
// IsComplexEnough return True if password meets complexity settings
|
||
5 years ago
|
func IsComplexEnough(pwd string) bool {
|
||
5 years ago
|
NewComplexity()
|
||
|
if len(validChars) > 0 {
|
||
5 years ago
|
for _, req := range requiredList {
|
||
|
if !strings.ContainsAny(req.ValidChars, pwd) {
|
||
5 years ago
|
return false
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
4 years ago
|
// Generate a random password
|
||
5 years ago
|
func Generate(n int) (string, error) {
|
||
|
NewComplexity()
|
||
|
buffer := make([]byte, n)
|
||
|
max := big.NewInt(int64(len(validChars)))
|
||
|
for {
|
||
|
for j := 0; j < n; j++ {
|
||
|
rnd, err := rand.Int(rand.Reader, max)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
buffer[j] = validChars[rnd.Int64()]
|
||
|
}
|
||
4 years ago
|
pwned, err := IsPwned(goContext.Background(), string(buffer))
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
if IsComplexEnough(string(buffer)) && !pwned && string(buffer[0]) != " " && string(buffer[n-1]) != " " {
|
||
5 years ago
|
return string(buffer), nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
|
// BuildComplexityError builds the error message when password complexity checks fail
|
||
2 years ago
|
func BuildComplexityError(locale translation.Locale) string {
|
||
5 years ago
|
var buffer bytes.Buffer
|
||
2 years ago
|
buffer.WriteString(locale.Tr("form.password_complexity"))
|
||
5 years ago
|
buffer.WriteString("<ul>")
|
||
|
for _, c := range requiredList {
|
||
|
buffer.WriteString("<li>")
|
||
2 years ago
|
buffer.WriteString(locale.Tr(c.TrNameOne))
|
||
5 years ago
|
buffer.WriteString("</li>")
|
||
|
}
|
||
|
buffer.WriteString("</ul>")
|
||
|
return buffer.String()
|
||
|
}
|