feat(identity): confirm registration email atomically
This commit is contained in:
1 parent
8e7ce8d71c
commit
fade4e76e4
75 files changed
+4188
-403
No files matched your search
@@ -12,21 +12,30 @@ import (
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
const testPasswordPHC = "$argon2id$v=19$m=65536,t=3,p=4$c2FsdHNhbHRzYWx0c2FsdA$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaGhhc2g"
|
||||
|
||||
// countingHasher records calls so a test can prove that validation stops a
|
||||
// password before any hashing.
|
||||
type countingHasher struct {
|
||||
hashCalls int
|
||||
hashed domain.Password
|
||||
hashCalls int
|
||||
hashed domain.Password
|
||||
verifyCalls int
|
||||
verified domain.Password
|
||||
stored domain.PasswordHash
|
||||
verifyErr error
|
||||
}
|
||||
|
||||
func (hasher *countingHasher) Hash(_ context.Context, password domain.Password) (domain.PasswordHash, error) {
|
||||
hasher.hashCalls++
|
||||
hasher.hashed = password
|
||||
return domain.NewPasswordHash("$argon2id$v=19$m=65536,t=3,p=4$c2FsdHNhbHRzYWx0c2FsdA$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaGhhc2g")
|
||||
return domain.NewPasswordHash(testPasswordPHC)
|
||||
}
|
||||
|
||||
func (hasher *countingHasher) Verify(context.Context, domain.Password, domain.PasswordHash) (bool, error) {
|
||||
return false, errors.New("countingHasher.Verify - not used")
|
||||
func (hasher *countingHasher) Verify(_ context.Context, password domain.Password, stored domain.PasswordHash) (bool, error) {
|
||||
hasher.verifyCalls++
|
||||
hasher.verified = password
|
||||
hasher.stored = stored
|
||||
return false, hasher.verifyErr
|
||||
}
|
||||
|
||||
func (hasher *countingHasher) VerifyDummy(context.Context, domain.Password) error {
|
||||
@@ -81,3 +90,29 @@ func TestHashNewPasswordHashesTheNormalizedPassword(t *testing.T) {
|
||||
t.Fatalf("hasher received %d calls, want one with the NFKC form", hasher.hashCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyLoginPasswordUsesLoginBoundsAndNFKC(t *testing.T) {
|
||||
hasher := &countingHasher{}
|
||||
stored, err := domain.NewPasswordHash(testPasswordPHC)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := NewCredentials(hasher).VerifyLoginPassword(context.Background(), "x", stored); err != nil {
|
||||
t.Fatalf("VerifyLoginPassword() error = %v", err)
|
||||
}
|
||||
if hasher.verifyCalls != 1 || hasher.verified.Reveal() != "x" || hasher.stored.PHC() != stored.PHC() {
|
||||
t.Fatalf("Verify calls = %d, password = %q, stored hash matched = %v", hasher.verifyCalls, hasher.verified.Reveal(), hasher.stored.PHC() == stored.PHC())
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyLoginPasswordRejectsEmptyInputBeforeKDF(t *testing.T) {
|
||||
hasher := &countingHasher{}
|
||||
stored, err := domain.NewPasswordHash(testPasswordPHC)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = NewCredentials(hasher).VerifyLoginPassword(context.Background(), "", stored)
|
||||
if !errors.Is(err, domain.ErrInvalidPassword) || hasher.verifyCalls != 0 {
|
||||
t.Fatalf("VerifyLoginPassword(empty) = %v after %d verify calls, want ErrInvalidPassword before KDF", err, hasher.verifyCalls)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user