Перенести пользователей в PostgreSQL и добавить пароли
This commit is contained in:
+28
-8
@@ -1,6 +1,7 @@
|
||||
// Package user is the users module. The entity mirrors the future table row
|
||||
// and carries no behaviour; New only enforces input invariants and CreatedAt
|
||||
// derives from the UUIDv7 id.
|
||||
// Package user is the users module. The entity mirrors the table row and
|
||||
// carries no behaviour; New only enforces input invariants and CreatedAt
|
||||
// derives from the UUIDv7 id. The password itself is never kept: the service
|
||||
// validates it and stores only its hash.
|
||||
package user
|
||||
|
||||
import (
|
||||
@@ -16,20 +17,28 @@ import (
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
const MaxDisplayNameCodePoints = 100
|
||||
const (
|
||||
MaxDisplayNameCodePoints = 100
|
||||
MinPasswordCodePoints = 12
|
||||
MaxPasswordCodePoints = 128
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = apperr.NotFound("user not found")
|
||||
ErrUsernameTaken = apperr.Conflict("username already taken")
|
||||
// ErrInvalidCredentials covers an unknown username and a wrong password
|
||||
// alike, so a login attempt cannot probe which usernames exist.
|
||||
ErrInvalidCredentials = apperr.Unauthorized("invalid username or password")
|
||||
)
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func New(id uuid.UUID, username, displayName string) (User, error) {
|
||||
@@ -49,3 +58,14 @@ func New(id uuid.UUID, username, displayName string) (User, error) {
|
||||
CreatedAt: ident.TimeOf(id),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ValidatePassword checks the contract length in Unicode code points.
|
||||
func ValidatePassword(plain string) error {
|
||||
if strings.TrimSpace(plain) == "" {
|
||||
return apperr.Invalid("password must not be blank")
|
||||
}
|
||||
if n := utf8.RuneCountInString(plain); n < MinPasswordCodePoints || n > MaxPasswordCodePoints {
|
||||
return apperr.Invalid(fmt.Sprintf("password must be %d to %d characters", MinPasswordCodePoints, MaxPasswordCodePoints))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user