feat(identity): hash passwords with Argon2id and bounded inputs

This commit is contained in:
shira committed 2026-10-06 01:07:55 +03:00
1 parent 6153887eb5
commit dda3825786
26 files changed
+1475 -32

No files matched your search

@@ -0,0 +1,39 @@
// 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
}