feat(identity): hash passwords with Argon2id and bounded inputs
This commit is contained in:
1 parent
6153887eb5
commit
dda3825786
26 files changed
+1475
-32
No files matched your search
@@ -31,8 +31,10 @@ var (
|
||||
// session cookie value. Only the holder sees the raw value; storage keeps
|
||||
// SecretTokenHash. String and LogValue redact the value so it does not reach
|
||||
// logs by accident; Reveal returns it for the one place that must send it.
|
||||
// The value sits behind a pointer so that printing an enclosing struct shows
|
||||
// only an address (see redactedValue).
|
||||
type SecretToken struct {
|
||||
value string
|
||||
value *string
|
||||
}
|
||||
|
||||
// NewSecretToken returns a token with 256 bits from crypto/rand.
|
||||
@@ -41,7 +43,7 @@ func NewSecretToken() (SecretToken, error) {
|
||||
if _, err := rand.Read(entropy); err != nil {
|
||||
return SecretToken{}, fmt.Errorf("domain.NewSecretToken - read random bytes: %w", err)
|
||||
}
|
||||
return SecretToken{value: base64.RawURLEncoding.EncodeToString(entropy)}, nil
|
||||
return SecretToken{value: newRedactedValue(base64.RawURLEncoding.EncodeToString(entropy))}, nil
|
||||
}
|
||||
|
||||
// ParseSecretToken accepts a raw token from a request. It checks only the
|
||||
@@ -50,19 +52,19 @@ func ParseSecretToken(raw string) (SecretToken, error) {
|
||||
if !secretTokenPattern.MatchString(raw) {
|
||||
return SecretToken{}, fmt.Errorf("domain.ParseSecretToken - token must have 32 to 128 base64url characters: %w", ErrInvalidSecretToken)
|
||||
}
|
||||
return SecretToken{value: raw}, nil
|
||||
return SecretToken{value: newRedactedValue(raw)}, nil
|
||||
}
|
||||
|
||||
// Hash returns the SHA-256 digest that storage keeps. A token carries 256 bits
|
||||
// of entropy, so a fast unsalted digest cannot be reversed by guessing, unlike
|
||||
// a password, and lookups by digest stay a single index probe.
|
||||
func (token SecretToken) Hash() SecretTokenHash {
|
||||
return SecretTokenHash(sha256.Sum256([]byte(token.value)))
|
||||
return SecretTokenHash(sha256.Sum256([]byte(token.Reveal())))
|
||||
}
|
||||
|
||||
// Reveal returns the raw value for delivery to its holder: the email link or
|
||||
// the Set-Cookie header. Never store or log it.
|
||||
func (token SecretToken) Reveal() string { return token.value }
|
||||
func (token SecretToken) Reveal() string { return redactedValue(token.value) }
|
||||
|
||||
func (token SecretToken) String() string { return redactedSecret }
|
||||
|
||||
@@ -73,12 +75,26 @@ func (token SecretToken) LogValue() slog.Value { return slog.StringValue(redacte
|
||||
// Format redacts every verb; without it %d would print the struct fields.
|
||||
func (token SecretToken) Format(state fmt.State, _ rune) { writeRedacted(state, redactedSecret) }
|
||||
|
||||
// newRedactedValue and redactedValue hold a secret string behind a pointer.
|
||||
// fmt calls Format, String, or GoString only on values it can reach through
|
||||
// an interface; for an unexported field of an enclosing struct it prints the
|
||||
// fields with reflection instead. A pointer field is then printed as an
|
||||
// address, so the secret never appears. A nil pointer is the zero value.
|
||||
func newRedactedValue(value string) *string { return &value }
|
||||
|
||||
func redactedValue(value *string) string {
|
||||
if value == nil {
|
||||
return ""
|
||||
}
|
||||
return *value
|
||||
}
|
||||
|
||||
func writeRedacted(state fmt.State, placeholder string) {
|
||||
_, _ = state.Write([]byte(placeholder))
|
||||
}
|
||||
|
||||
// IsZero reports a SecretToken not built by NewSecretToken or ParseSecretToken.
|
||||
func (token SecretToken) IsZero() bool { return token.value == "" }
|
||||
func (token SecretToken) IsZero() bool { return token.value == nil }
|
||||
|
||||
// SecretTokenHash is the SHA-256 digest of a SecretToken.
|
||||
type SecretTokenHash [SecretTokenHashSize]byte
|
||||
|
||||
Reference in new issue
Block a user