Files
iam/internal/otp/otp_test.go
zeekayandHanzo Dev 9a01d6397f An address is proven where IAM watched it receive a code
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]>
2026-09-23 02:07:50 -07:00

239 lines
8.7 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package otp
import (
"context"
"fmt"
"path/filepath"
"regexp"
"strings"
"testing"
"time"
"github.com/hanzoai/orm"
ormdb "github.com/hanzoai/orm/db"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
func openDB(t *testing.T) orm.DB {
t.Helper()
_ = schema.Kinds()
db, err := orm.OpenSQLite(&ormdb.SQLiteDBConfig{
Path: filepath.Join(t.TempDir(), "otp.db"),
Config: ormdb.SQLiteConfig{BusyTimeout: 5000, JournalMode: "WAL"},
})
if err != nil {
t.Fatalf("open sqlite: %v", err)
}
t.Cleanup(func() { _ = db.Close() })
return db
}
// recorder captures what was handed to delivery, which is the only way to learn the
// code that actually went out.
type recorder struct{ sent []string }
func (r *recorder) Send(_ context.Context, m Message) error {
r.sent = append(r.sent, strings.Join([]string{m.Org, m.Channel, m.To, sixDigits.FindString(m.Body)}, ":"))
return nil
}
// sixDigits finds the code inside a worded message — where a person reads it, and the
// only place a test can learn it from.
var sixDigits = regexp.MustCompile(`\b\d{6}\b`)
// account creates the user a code is minted FOR. A code is bound to an account, so a
// test that spends one needs a row to bind to — an address alone is not an identity.
func account(t *testing.T, db orm.DB, owner, name, email, phone string) *schema.User {
t.Helper()
u := orm.New[schema.User](db)
u.Owner, u.Name, u.Email = owner, name, email
u.Phone = store.NormalizePhone(phone)
u.SetId(owner + "/" + name)
if err := u.CreateCtx(context.Background()); err != nil {
t.Fatalf("seed user: %v", err)
}
got, err := store.GetUserByName(context.Background(), db, owner, name)
if err != nil || got == nil {
t.Fatalf("re-read user: %v", err)
}
return got
}
func bind(t *testing.T, s Sender) {
t.Helper()
BindSender(s)
t.Cleanup(func() { BindSender(nil) })
}
// THE defect. A phone number is punctuated differently every place it is typed, so a
// record filed under what somebody typed and looked up under the account's stored
// digits do not meet: the correct code, delivered to the correct phone, was refused,
// and the SMS second factor could not be completed by anyone.
func TestOneNumberIsOneReceiverHoweverItIsTyped(t *testing.T) {
db := openDB(t)
r := &recorder{}
bind(t, r)
now := time.Now()
carol := account(t, db, "hanzo", "carol", "", "+14155550134")
// The code verifies against every PUNCTUATION of the same digits, because the
// digits are the key on both sides. The middle one is the exact pair that was
// broken: a record filed as the caller typed it, presented as the account stores it.
// Each spelling spends its own code, sent a resend interval after the last.
for i, presented := range []string{
"+1 (415) 555-0134", // what the person typed at the send
"+14155550134", // what the account stores, and what the gate looks up
"+1-415-555-0134", // dashes instead of brackets
" +1 415 555 0134 ", // and whatever whitespace came along
} {
at := now.Add(time.Duration(i) * ResendInterval)
if err := Issue(context.Background(), db, "hanzo", "+1 (415) 555-0134", "", carol, at); err != nil {
t.Fatalf("issue: %v", err)
}
code := r.sent[i][strings.LastIndex(r.sent[i], ":")+1:]
// A number missing its country code is a DIFFERENT address, not the same one
// spelled differently. Normalizing punctuation is safe; inferring a country is
// a guess, and a guess here routes somebody's second factor to another country.
if ok, _ := Consume(context.Background(), db, carol, "415-555-0134", code, at); ok {
t.Fatal("a national-format number matched an E.164 record — the key is inferring a country code")
}
ok, err := Consume(context.Background(), db, carol, presented, code, at)
if err != nil {
t.Fatalf("consume %q: %v", presented, err)
}
if !ok {
t.Fatalf("the delivered code was refused for %q — filing and finding disagree about punctuation", presented)
}
}
// And the delivery itself went out normalized, which is what a carrier wants.
if !strings.HasPrefix(r.sent[0], "hanzo:phone:+14155550134:") {
t.Fatalf("sent %q, want the normalized number", r.sent[0])
}
}
// A code is spent by the use that succeeds, so it cannot be replayed.
func TestConsumeSpendsTheCode(t *testing.T) {
db := openDB(t)
r := &recorder{}
bind(t, r)
now := time.Now()
ada := account(t, db, "hanzo", "ada", "[email protected]", "")
if err := Issue(context.Background(), db, "hanzo", "[email protected]", "", ada, now); err != nil {
t.Fatalf("issue: %v", err)
}
code := r.sent[0][strings.LastIndex(r.sent[0], ":")+1:]
if ok, err := Consume(context.Background(), db, ada, "[email protected]", code, now); err != nil || !ok {
t.Fatalf("first use: ok=%v err=%v", ok, err)
}
if ok, _ := Consume(context.Background(), db, ada, "[email protected]", code, now); ok {
t.Fatal("the same code was accepted twice — a replayable credential is left in the table")
}
}
// A wrong guess counts, and the run ends at MaxAttempts with the record spent — the
// bound that makes a six-digit code usable as a credential rather than only as a
// signup gate.
func TestWrongGuessesAreBounded(t *testing.T) {
db := openDB(t)
r := &recorder{}
bind(t, r)
now := time.Now()
ada := account(t, db, "hanzo", "ada", "[email protected]", "")
if err := Issue(context.Background(), db, "hanzo", "[email protected]", "", ada, now); err != nil {
t.Fatalf("issue: %v", err)
}
code := r.sent[0][strings.LastIndex(r.sent[0], ":")+1:]
for i := 0; i < MaxAttempts; i++ {
if ok, _ := Consume(context.Background(), db, ada, "[email protected]", "000000", now); ok {
t.Fatal("a wrong code verified")
}
}
if ok, _ := Consume(context.Background(), db, ada, "[email protected]", code, now); ok {
t.Fatalf("the code survived %d wrong guesses — the search space is not bounded", MaxAttempts)
}
}
// An expired code is a plain refusal, indistinguishable from an absent one: telling
// them apart answers "that address has a code outstanding" to anyone who asks.
func TestExpiredCodeIsRefused(t *testing.T) {
db := openDB(t)
bind(t, &recorder{})
now := time.Now()
ada := account(t, db, "hanzo", "ada", "[email protected]", "")
if err := Issue(context.Background(), db, "hanzo", "[email protected]", "", ada, now); err != nil {
t.Fatalf("issue: %v", err)
}
rec, err := store.GetLatestVerificationRecord(context.Background(), db, "hanzo", "[email protected]")
if err != nil || rec == nil {
t.Fatalf("record not persisted: %v", err)
}
if ok, _ := Consume(context.Background(), db, ada, "[email protected]", rec.Code, now.Add(TTL+time.Second)); ok {
t.Fatal("a code outlived its TTL")
}
}
// With nothing bound to deliver, the caller is TOLD the send did not happen and
// NOTHING IS MINTED. Reporting success would leave somebody waiting for a message
// that is not coming — the exact {status:"ok"} lie this refusal exists to remove —
// and filing the record anyway left a code nobody could receive for a redeeming
// caller to nonetheless have to trust.
func TestUnboundDeliveryIsReportedNotFaked(t *testing.T) {
db := openDB(t)
BindSender(nil)
err := Issue(context.Background(), db, "hanzo", "[email protected]", "", nil, time.Now())
if err != ErrNoDelivery {
t.Fatalf("issue with nothing bound = %v, want ErrNoDelivery", err)
}
if DeliveryConfigured() {
t.Fatal("delivery reported configured with no sender bound")
}
if rec, _ := store.GetLatestVerificationRecord(context.Background(), db, "hanzo", "[email protected]"); rec != nil {
t.Fatal("a send that cannot happen left a redeemable code behind")
}
}
// The words are OTP policy: the sentence a person reads renders the SAME constant
// that expires the record, so the two cannot part company when the TTL moves. A
// transport composing its own sentence would be restating a policy it cannot read.
func TestTheMessageStatesTheRealExpiry(t *testing.T) {
m := message("hanzo", Email, "[email protected]", "123456")
want := fmt.Sprintf("It expires in %d minutes.", int(TTL.Minutes()))
if !strings.Contains(m.Body, want) {
t.Errorf("body = %q, want it to state %q", m.Body, want)
}
if !strings.Contains(m.Body, "123456") {
t.Errorf("body = %q, want it to carry the code", m.Body)
}
if m.Subject == "" {
t.Error("an email needs a subject")
}
// An SMS has nowhere to put one.
if s := message("hanzo", Phone, "+14155550134", "123456"); s.Subject != "" {
t.Errorf("sms subject = %q, want none", s.Subject)
}
}
// The channel is read off the address, so no caller carries a mapping table.
func TestChannelComesFromTheAddress(t *testing.T) {
if got := Channel("[email protected]"); got != Email {
t.Errorf("Channel(email) = %q, want %q", got, Email)
}
if got := Channel("+14155550134"); got != Phone {
t.Errorf("Channel(number) = %q, want %q", got, Phone)
}
}