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

+11 -5
View File
@@ -105,9 +105,11 @@ func ParseOnboardingStep(value string) (OnboardingStep, error) {
}
// PasswordHash is an Argon2id hash in the PHC string format. It is never the
// password itself. String and LogValue redact it.
// password itself. String and LogValue redact it. The string sits behind a
// pointer so that printing an enclosing struct shows only an address (see
// redactedValue).
type PasswordHash struct {
phc string
phc *string
}
// NewPasswordHash validates the structure of a PHC string produced by the
@@ -129,7 +131,7 @@ func NewPasswordHash(phc string) (PasswordHash, error) {
if err != nil || len(digest) < minPasswordDigestBytes {
return PasswordHash{}, invalidPasswordHash("digest must be unpadded base64 of at least 16 bytes")
}
return PasswordHash{phc: phc}, nil
return PasswordHash{phc: newRedactedValue(phc)}, nil
}
func invalidPasswordHash(reason string) error {
@@ -137,7 +139,7 @@ func invalidPasswordHash(reason string) error {
}
// PHC returns the encoded hash for storage and verification.
func (hash PasswordHash) PHC() string { return hash.phc }
func (hash PasswordHash) PHC() string { return redactedValue(hash.phc) }
func (hash PasswordHash) String() string { return redactedSecret }
@@ -148,8 +150,12 @@ func (hash PasswordHash) LogValue() slog.Value { return slog.StringValue(redacte
// Format redacts every verb; without it %d would print the struct fields.
func (hash PasswordHash) Format(state fmt.State, _ rune) { writeRedacted(state, redactedSecret) }
// MarshalJSON redacts the hash in any JSON output, including a serialized
// User.
func (hash PasswordHash) MarshalJSON() ([]byte, error) { return redactedSecretJSON(), nil }
// IsZero reports a PasswordHash not built by NewPasswordHash.
func (hash PasswordHash) IsZero() bool { return hash.phc == "" }
func (hash PasswordHash) IsZero() bool { return hash.phc == nil }
// Registration is the validated content of one registration request.
type Registration struct {