[MM-33794] Improve password generation during bulk import (#17147)
Automatic Merge
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
2789be220b
Коммит
6a65b6ceca
60
app/import_utils.go
Обычный файл
60
app/import_utils.go
Обычный файл
@@ -0,0 +1,60 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"math/big"
|
||||
)
|
||||
|
||||
const (
|
||||
passwordSpecialChars = "!$%^&*(),."
|
||||
passwordNumbers = "0123456789"
|
||||
passwordUpperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
passwordLowerCaseLetters = "abcdefghijklmnopqrstuvwxyz"
|
||||
passwordAllChars = passwordSpecialChars + passwordNumbers + passwordUpperCaseLetters + passwordLowerCaseLetters
|
||||
)
|
||||
|
||||
func randInt(max int) (int, error) {
|
||||
val, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return int(val.Int64()), nil
|
||||
}
|
||||
|
||||
func generatePassword(minimumLength int) (string, error) {
|
||||
upperIdx, err := randInt(len(passwordUpperCaseLetters))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
numberIdx, err := randInt(len(passwordNumbers))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
lowerIdx, err := randInt(len(passwordLowerCaseLetters))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
specialIdx, err := randInt(len(passwordSpecialChars))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Make sure we are guaranteed at least one of each type to meet any possible password complexity requirements.
|
||||
password := string([]rune(passwordUpperCaseLetters)[upperIdx]) +
|
||||
string([]rune(passwordNumbers)[numberIdx]) +
|
||||
string([]rune(passwordLowerCaseLetters)[lowerIdx]) +
|
||||
string([]rune(passwordSpecialChars)[specialIdx])
|
||||
|
||||
for len(password) < minimumLength {
|
||||
i, err := randInt(len(passwordAllChars))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
password = password + string([]rune(passwordAllChars)[i])
|
||||
}
|
||||
|
||||
return password, nil
|
||||
}
|
||||
Ссылка в новой задаче
Block a user