Files
lincodex/backend/internal/port/outbound/password.go
T

63 lines
3.0 KiB
Go

package outbound
import (
"context"
"errors"
"fmt"
"lincodex/backend/internal/domain"
)
var (
// ErrPasswordMismatch reports a password that does not match the hash.
// VerifyDummy ends with it whenever it completes the work.
ErrPasswordMismatch = errors.New("password does not match")
// ErrPasswordHashCorrupt reports a stored hash that cannot be verified:
// it is not a well-formed Argon2id v19 PHC string, or its parameters are
// outside the bounds the hasher accepts. It wraps
// domain.ErrInvalidPasswordHash, which the repository returns when a
// stored hash fails domain.NewPasswordHash on read, so
// errors.Is(err, domain.ErrInvalidPasswordHash) matches both cases. It is
// a storage problem, not a wrong password.
ErrPasswordHashCorrupt = fmt.Errorf("password hash corrupt: %w", domain.ErrInvalidPasswordHash)
)
// PasswordHasher derives and checks password hashes with a slow, salted KDF.
//
// Callers validate the password with domain.NewPassword or
// domain.ParseLoginPassword first, so the hasher only receives bounded
// input; a zero domain.Password returns an error wrapping
// domain.ErrInvalidPassword without hashing. Every derivation waits for its
// share of a fixed memory budget, which bounds the memory and CPU of
// concurrent hashing; a context that ends while waiting returns the context
// error and computes nothing. Errors never contain the password or the hash.
//
// Login must answer invalid_credentials for an unknown email, a
// wrong password, and a stored hash that is invalid
// (domain.ErrInvalidPasswordHash, from the repository or from Verify). For
// an invalid hash it must also log an error and call VerifyDummy, because a
// corrupt hash is refused without a derivation and would otherwise answer
// faster than a real check.
type PasswordHasher interface {
// Hash returns a new hash with a fresh random salt and the current
// parameters.
Hash(ctx context.Context, password domain.Password) (domain.PasswordHash, error)
// Verify returns nil when password matches stored. needsRehash then
// reports that stored was made with other parameters, salt length, or key
// length than the current ones, so a login can store Hash(password) in
// its place. A wrong password returns ErrPasswordMismatch and a malformed
// or out-of-bounds hash returns ErrPasswordHashCorrupt without a
// derivation; needsRehash is false with any error.
Verify(ctx context.Context, password domain.Password, stored domain.PasswordHash) (needsRehash bool, err error)
// VerifyDummy does the same work as Verify against a hash with the current
// parameters that no password matches. It returns ErrPasswordMismatch
// after the work, the context error when the context ended before the
// derivation, or domain.ErrInvalidPassword for a zero password; it never
// returns nil. A login calls it for an unknown email so that the response
// time does not reveal whether the account exists.
VerifyDummy(ctx context.Context, password domain.Password) error
}