fix(auth): new passwords need at least 8 characters (FLAW-09)
auth.ValidatePassword checks every way a password is set: setup, registration, profile, admin create and reset, reset by mail. Same rule in the browser before sending (lib/password.ts), with our own German message instead of the browser popup. Existing shorter passwords still log in. Profile and admin reset no longer trim the password, which made passwords with surrounding spaces unusable for login. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f8989d0c44
commit
b5f926bcf7
13 files changed
+142
-26
No files matched your search
@@ -7,7 +7,9 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
@@ -25,6 +27,27 @@ type Claims struct {
|
||||
jwt.RegisteredClaims
|
||||
}
|
||||
|
||||
// MinPasswordLength applies wherever a password is set (setup, registration, profile, admin, reset).
|
||||
// Existing shorter passwords keep working for login.
|
||||
const MinPasswordLength = 8
|
||||
|
||||
var (
|
||||
ErrPasswordTooShort = fmt.Errorf("Das Passwort muss mindestens %d Zeichen lang sein", MinPasswordLength)
|
||||
// bcrypt only uses the first 72 bytes and rejects longer input
|
||||
ErrPasswordTooLong = errors.New("Das Passwort darf höchstens 72 Bytes lang sein")
|
||||
)
|
||||
|
||||
// ValidatePassword checks a new password: at least MinPasswordLength characters, not only whitespace.
|
||||
func ValidatePassword(password string) error {
|
||||
if strings.TrimSpace(password) == "" || utf8.RuneCountInString(password) < MinPasswordLength {
|
||||
return ErrPasswordTooShort
|
||||
}
|
||||
if len(password) > 72 {
|
||||
return ErrPasswordTooLong
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
return string(bytes), err
|
||||
|
||||
Reference in new issue
Block a user