Files
lincodex/backend/internal/domain/password.go
T

97 lines
4.1 KiB
Go

package domain
import (
"errors"
"fmt"
"log/slog"
"unicode/utf8"
"golang.org/x/text/unicode/norm"
)
const (
// MinNewPasswordCodePoints follows NIST SP 800-63B-4 for a password used
// as the only authentication factor (the NewPassword contract schema).
MinNewPasswordCodePoints = 15
// MaxPasswordCodePoints bounds the hashing input of new and login
// passwords (NewPassword and LoginPassword in the contract).
MaxPasswordCodePoints = 128
// minLoginPasswordCodePoints only rejects an empty login password; login
// applies no registration policy, so a policy change locks nobody out.
minLoginPasswordCodePoints = 1
// maxPasswordInputBytes caps the raw input before NFKC normalization so
// an oversized value never reaches the normalizer. 128 code points take at
// most 4 bytes each in UTF-8, so every value that the contract schema
// accepts fits.
maxPasswordInputBytes = MaxPasswordCodePoints * utf8.UTFMax
)
// ErrInvalidPassword reports a password outside the length rules. Errors that
// wrap it name the rule, never the value or its length.
var ErrInvalidPassword = errors.New("invalid password")
// Password is a plaintext password normalized to Unicode NFKC, as NIST SP
// 800-63B-4 recommends, so that equivalent input from different keyboards
// and platforms hashes the same. It exists only between the request and the
// password hasher and is never stored. String, GoString, Format, LogValue,
// and MarshalJSON redact it; Reveal returns the value for the hasher. The
// value sits behind a pointer so that printing an enclosing struct shows only
// an address (see redactedValue).
type Password struct {
value *string
}
// NewPassword applies the registration policy: 15 to 128 Unicode code points
// after NFKC normalization. The blocklist of common or breached passwords is
// a separate check.
func NewPassword(raw string) (Password, error) {
return parsePassword("domain.NewPassword", raw, MinNewPasswordCodePoints)
}
// ParseLoginPassword accepts a password entered at login: 1 to 128 Unicode
// code points after NFKC normalization.
func ParseLoginPassword(raw string) (Password, error) {
return parsePassword("domain.ParseLoginPassword", raw, minLoginPasswordCodePoints)
}
// parsePassword runs only cheap checks, so a caller can reject a value before
// any expensive hashing. The raw byte cap comes first, then UTF-8 validity,
// then normalization and the code point count.
func parsePassword(location, raw string, minCodePoints int) (Password, error) {
if len(raw) > maxPasswordInputBytes {
return Password{}, fmt.Errorf("%s - password must have at most %d bytes before normalization: %w",
location, maxPasswordInputBytes, ErrInvalidPassword)
}
if !utf8.ValidString(raw) {
return Password{}, fmt.Errorf("%s - password must be valid UTF-8: %w", location, ErrInvalidPassword)
}
normalized := norm.NFKC.String(raw)
if count := utf8.RuneCountInString(normalized); count < minCodePoints || count > MaxPasswordCodePoints {
return Password{}, fmt.Errorf("%s - password must have %d to %d code points after NFKC normalization: %w",
location, minCodePoints, MaxPasswordCodePoints, ErrInvalidPassword)
}
return Password{value: newRedactedValue(normalized)}, nil
}
// Reveal returns the normalized password for the password hasher. Never
// store, log, or return it.
func (password Password) Reveal() string { return redactedValue(password.value) }
// IsZero reports a Password not built by NewPassword or ParseLoginPassword.
func (password Password) IsZero() bool { return password.value == nil }
func (password Password) String() string { return redactedSecret }
func (password Password) GoString() string { return redactedSecret }
func (password Password) LogValue() slog.Value { return slog.StringValue(redactedSecret) }
// Format redacts every verb; without it %d would print the struct fields.
func (password Password) Format(state fmt.State, _ rune) { writeRedacted(state, redactedSecret) }
// MarshalJSON redacts the value in any JSON output, including a struct that
// embeds a Password.
func (password Password) MarshalJSON() ([]byte, error) { return redactedSecretJSON(), nil }
func redactedSecretJSON() []byte { return []byte(`"` + redactedSecret + `"`) }