-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
176 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package helper | ||
|
||
import ( | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
const ( | ||
// PasswordHashCost TODO: перенести в конфиг | ||
PasswordHashCost = 14 | ||
) | ||
|
||
// HashPassword создает хеш пароля | ||
func HashPassword(password string) (string, error) { | ||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), PasswordHashCost) | ||
return string(bytes), err | ||
} | ||
|
||
func СomparePasswords(hashedPwd string, plainPwd []byte) bool { | ||
byteHash := []byte(hashedPwd) | ||
err := bcrypt.CompareHashAndPassword(byteHash, plainPwd) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package helper | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
) | ||
|
||
// ValidateLogin проверяет длину имени пользователя. | ||
func ValidateLogin(username string) error { | ||
if len(username) < 3 || len(username) > 16 { | ||
return errors.New("имя пользователя должно быть от 3 до 16 символов") | ||
} | ||
// TODO: проверка на уникальность | ||
// TODO: Проверка на запрещенные символы или слова | ||
return nil | ||
} | ||
|
||
// ValidatePassword проверяет пароль на длину, наличие цифр, заглавных и строчных букв, специальных символов. | ||
func ValidatePassword(password string) error { | ||
if len(password) < 6 || len(password) > 64 { | ||
return errors.New("пароль должен быть от 8 до 64 символов") | ||
} | ||
|
||
hasNumber := regexp.MustCompile(`[0-9]+`).MatchString | ||
hasUpper := regexp.MustCompile(`[A-Z]+`).MatchString | ||
hasLower := regexp.MustCompile(`[a-z]+`).MatchString | ||
hasSpecial := regexp.MustCompile(`[!@#$%^&*]+`).MatchString | ||
|
||
if !hasNumber(password) || !hasUpper(password) || !hasLower(password) || !hasSpecial(password) { | ||
return errors.New("пароль должен содержать хотя бы одну цифру, одну заглавную и строчную букву, а также один специальный символ") | ||
} | ||
|
||
// TODO: игнорировать общеизвестные пароли | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.