40 lines
1.4 KiB
Go
40 lines
1.4 KiB
Go
// Package identity holds the account usecases.
|
|
package identity
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
// Credentials applies the password rules in the order that keeps hashing
|
|
// bounded: the cheap length and encoding checks first, the expensive KDF only
|
|
// for a password that passes them.
|
|
type Credentials struct {
|
|
hasher outbound.PasswordHasher
|
|
}
|
|
|
|
func NewCredentials(hasher outbound.PasswordHasher) *Credentials {
|
|
return &Credentials{hasher: hasher}
|
|
}
|
|
|
|
// HashNewPassword applies the registration password policy to raw and hashes
|
|
// the normalized password. A password outside the policy returns an error
|
|
// wrapping domain.ErrInvalidPassword without calling the hasher. Errors
|
|
// never contain raw. Registration (TASK-053, not implemented yet) must call
|
|
// it on every branch before it looks up the email, so that neither the
|
|
// outcome nor the time depends on whether the account exists.
|
|
func (credentials *Credentials) HashNewPassword(ctx context.Context, raw string) (domain.PasswordHash, error) {
|
|
password, err := domain.NewPassword(raw)
|
|
if err != nil {
|
|
return domain.PasswordHash{}, fmt.Errorf("Credentials.HashNewPassword - validate password: %w", err)
|
|
}
|
|
hash, err := credentials.hasher.Hash(ctx, password)
|
|
if err != nil {
|
|
return domain.PasswordHash{}, fmt.Errorf("Credentials.HashNewPassword - hash password: %w", err)
|
|
}
|
|
return hash, nil
|
|
}
|