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
@@ -0,0 +1,90 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// nestedSecrets holds every secret type in unexported fields, where fmt does
|
||||
// not call their Format, String, or GoString methods.
|
||||
type nestedSecrets struct {
|
||||
password Password
|
||||
hash PasswordHash
|
||||
token SecretToken
|
||||
email EmailAddress
|
||||
}
|
||||
|
||||
// exportedSecrets is the same with exported fields, as in domain.User.
|
||||
type exportedSecrets struct {
|
||||
Password Password `json:"password"`
|
||||
Hash PasswordHash `json:"hash"`
|
||||
Token SecretToken `json:"token"`
|
||||
Email EmailAddress `json:"email"`
|
||||
}
|
||||
|
||||
func TestSecretsAreRedactedInsideEnclosingStructs(t *testing.T) {
|
||||
const rawPassword = "correct horse battery staple"
|
||||
const rawEmail = "Secret.Person@Example.com"
|
||||
password, err := NewPassword(rawPassword)
|
||||
if err != nil {
|
||||
t.Fatalf("NewPassword() error = %v", err)
|
||||
}
|
||||
hash, err := NewPasswordHash(testPasswordHash)
|
||||
if err != nil {
|
||||
t.Fatalf("NewPasswordHash() error = %v", err)
|
||||
}
|
||||
token, err := NewSecretToken()
|
||||
if err != nil {
|
||||
t.Fatalf("NewSecretToken() error = %v", err)
|
||||
}
|
||||
email, err := NewEmailAddress(rawEmail)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEmailAddress() error = %v", err)
|
||||
}
|
||||
nested := nestedSecrets{password: password, hash: hash, token: token, email: email}
|
||||
exported := exportedSecrets{Password: password, Hash: hash, Token: token, Email: email}
|
||||
pointer := &nested
|
||||
|
||||
var logs bytes.Buffer
|
||||
for _, logger := range []*slog.Logger{
|
||||
slog.New(slog.NewTextHandler(&logs, nil)),
|
||||
slog.New(slog.NewJSONHandler(&logs, nil)),
|
||||
} {
|
||||
logger.Info("nested", "unexported", nested, "exported", exported, "pointer", pointer)
|
||||
}
|
||||
encoded, err := json.Marshal(exported)
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal() error = %v", err)
|
||||
}
|
||||
output := fmt.Sprintf("%v %+v %#v %v %+v %#v %v %+v %v",
|
||||
nested, nested, nested, exported, exported, exported, pointer, []nestedSecrets{nested}, map[string]nestedSecrets{"k": nested}) +
|
||||
logs.String() + string(encoded)
|
||||
|
||||
secrets := []string{
|
||||
rawPassword, testPasswordHash, "c2FsdHNhbHRzYWx0", token.Reveal(), rawEmail, strings.ToLower(rawEmail),
|
||||
}
|
||||
for _, secret := range secrets {
|
||||
if strings.Contains(output, secret) {
|
||||
t.Fatalf("printing an enclosing struct exposes a secret: %s", output)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestZeroSecretValuesAreEmptyAndInvalid(t *testing.T) {
|
||||
if !(Password{}).IsZero() || (Password{}).Reveal() != "" {
|
||||
t.Fatal("zero Password is not empty and zero")
|
||||
}
|
||||
if !(PasswordHash{}).IsZero() || (PasswordHash{}).PHC() != "" {
|
||||
t.Fatal("zero PasswordHash is not empty and zero")
|
||||
}
|
||||
if !(SecretToken{}).IsZero() || (SecretToken{}).Reveal() != "" {
|
||||
t.Fatal("zero SecretToken is not empty and zero")
|
||||
}
|
||||
if !(EmailAddress{}).IsZero() || (EmailAddress{}).Address() != "" || (EmailAddress{}).Normalized() != "" {
|
||||
t.Fatal("zero EmailAddress is not empty and zero")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user