mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
A password signup records its address unproven and nothing a password account
could reach ever proved it, so every such account stayed unproven for good. The
federation broker refuses to link a social identity onto one, and cloud refuses
the coding path to one; neither refusal could ever be lifted.
Two places now prove it, both where the person holding the code is the person
holding the password:
- a signup may carry the code /v1/iam/verification-codes sent to its
address; a right code creates the account with the address proven, a
wrong one creates nothing;
- a password reset that spends a code sent to the account's own address
records the proof in the write that replaces the password.
otp.Prove is the signup gate's check: it reads only a code minted for no
account and spends it, counting misses exactly as Consume does. Check, which
compared without spending or counting and so bounded no guesses, is gone.
Co-authored-by: Hanzo Dev <[email protected]>
60 lines
2.1 KiB
Go
60 lines
2.1 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package otp
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A code sent to an address nobody holds yet proves the address to whoever reads
|
|
// it — the signup gate — and nothing more. It is spent by the proof, and a wrong
|
|
// guess counts exactly as it does against a sign-in code, because the six digits
|
|
// are the whole of what stands between a stranger and somebody else's address.
|
|
|
|
func TestProveSpendsAnAddressCode(t *testing.T) {
|
|
db := openDB(t)
|
|
now := time.Now()
|
|
file(t, db, "hanzo", "[email protected]", "", "424242", now)
|
|
|
|
if ok, err := Prove(context.Background(), db, "hanzo", "[email protected]", "424242", now); err != nil || !ok {
|
|
t.Fatalf("the code sent to the address does not prove it: ok=%v err=%v", ok, err)
|
|
}
|
|
if ok, _ := Prove(context.Background(), db, "hanzo", "[email protected]", "424242", now); ok {
|
|
t.Fatal("one code proved an address twice")
|
|
}
|
|
}
|
|
|
|
func TestProveCountsWrongGuesses(t *testing.T) {
|
|
db := openDB(t)
|
|
now := time.Now()
|
|
file(t, db, "hanzo", "[email protected]", "", "424242", now)
|
|
|
|
for i := 0; i < MaxAttempts; i++ {
|
|
if ok, _ := Prove(context.Background(), db, "hanzo", "[email protected]", "000000", now); ok {
|
|
t.Fatal("a wrong code proved the address")
|
|
}
|
|
}
|
|
if ok, _ := Prove(context.Background(), db, "hanzo", "[email protected]", "424242", now); ok {
|
|
t.Fatalf("the code survived %d wrong guesses", MaxAttempts)
|
|
}
|
|
}
|
|
|
|
// A code minted for an ACCOUNT is that account's credential; it proves no address
|
|
// for anybody else, and another org's code proves nothing here.
|
|
func TestProveReadsOnlyAddressCodesInItsOrg(t *testing.T) {
|
|
db := openDB(t)
|
|
now := time.Now()
|
|
file(t, db, "hanzo", "[email protected]", "hanzo/held", "424242", now)
|
|
file(t, db, "lux", "[email protected]", "", "515151", now)
|
|
|
|
if ok, _ := Prove(context.Background(), db, "hanzo", "[email protected]", "424242", now); ok {
|
|
t.Fatal("an account's sign-in code proved its address to somebody else")
|
|
}
|
|
if ok, _ := Prove(context.Background(), db, "hanzo", "[email protected]", "515151", now); ok {
|
|
t.Fatal("another org's code proved an address here")
|
|
}
|
|
}
|