mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-21 03:13:26 +00:00
Everything code-shaped was switched off because nothing was ever bound to the
delivery seam: BindSender was defined and never called, so DeliveryConfigured
answered false and the login descriptor correctly hid email sign-in, SMS
sign-in, and the email and SMS second factors. Both halves already existed --
IAM declared the seam, and notify's own send surface names this caller ("IAM
OTP") -- so this connects them rather than building anything new. Binding one
transport turns all four on at once, which is why availability is read from the
bound sender and never from an address.
The seam now carries the ORG. notify resolves the provider credential from the
tenant's own KMS entry, so a send without one cannot be routed and a send with
the wrong one reaches another account's Twilio. It is a parameter, not a
construction argument: one bound sender serves every tenant this process
answers for, and the value read is the record's own Owner so the code that was
persisted and the code that goes out are attributed identically.
A code is accepted where a password goes, in the SAME handler. Sign-in by email
or SMS proves possession of an address the account holds, which is one factor
exactly as a password is, so it joins one arm further in rather than at a second
endpoint -- the MFA gate, device approval and the PKCE tail are then true of it
by construction. Both arms now leave through afterFirstFactor, which also lets
the gate be told WHICH factor was already proven, so an emailed code is never
answered by demanding a second emailed code.
Two things had to change for a code to be a credential rather than a signup
gate. It is now spent on use, because a code that verifies twice is a
replayable credential; and wrong guesses are counted, because six digits live
for ten minutes is a million tries if nothing counts them. The bound is five,
not one: burning the code on the first miss is the tighter bound and the wrong
trade, since anyone who knows your address could then destroy your live code by
posting a wrong one.
Phone joins name and email as an identifier, last and only for something shaped
like a number. GetUserByPhone FAILS CLOSED when two rows in an org carry one
number -- Phone is indexed but not unique, so a First() would hand back an
arbitrary row and authenticate someone against another account. One
NormalizePhone runs at every write site and at the lookup, because a number
typed by a human and a number written by SCIM are the same number in different
clothes and equality cannot see through clothes. It infers no country code:
guessing +1 would claim a number in one country for a user in another.
55 lines
1.8 KiB
Go
55 lines
1.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package store
|
|
|
|
import "testing"
|
|
|
|
// One number typed five ways is one value, or an equality lookup can never match
|
|
// what a human writes against what SCIM wrote.
|
|
func TestNormalizePhone(t *testing.T) {
|
|
for _, tc := range []struct{ in, want string }{
|
|
{"+14155550134", "+14155550134"},
|
|
{"+1 (415) 555-0134", "+14155550134"},
|
|
{"+1-415-555-0134", "+14155550134"},
|
|
{" +1 415.555.0134 ", "+14155550134"},
|
|
{"4155550134", "4155550134"},
|
|
{"(415) 555-0134", "4155550134"},
|
|
|
|
// Blank-ish input must NOT become a value: a phone lookup for "" would
|
|
// match the many rows that legitimately store no phone at all.
|
|
{"", ""},
|
|
{" ", ""},
|
|
{"+", ""},
|
|
{"()- .", ""},
|
|
|
|
// A "+" is only a country-code marker in the leading position; anywhere
|
|
// else it is punctuation and is dropped like the rest.
|
|
{"415+555+0134", "4155550134"},
|
|
} {
|
|
if got := NormalizePhone(tc.in); got != tc.want {
|
|
t.Errorf("NormalizePhone(%q) = %q, want %q", tc.in, got, tc.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The normalizer must not invent a country code. Guessing "+1" would silently
|
|
// claim a number in one country for a user in another.
|
|
func TestNormalizePhoneDoesNotInferACountryCode(t *testing.T) {
|
|
if got := NormalizePhone("4155550134"); got == "+14155550134" {
|
|
t.Fatalf("NormalizePhone invented a country code: %q", got)
|
|
}
|
|
}
|
|
|
|
// Normalizing twice is normalizing once — the property the backfill relies on to
|
|
// be safe to re-run, and the reason a value written through a normalizing write
|
|
// site is already canonical.
|
|
func TestNormalizePhoneIsIdempotent(t *testing.T) {
|
|
for _, in := range []string{"+1 (415) 555-0134", "4155550134", "", "+"} {
|
|
once := NormalizePhone(in)
|
|
if twice := NormalizePhone(once); twice != once {
|
|
t.Errorf("NormalizePhone not idempotent for %q: %q then %q", in, once, twice)
|
|
}
|
|
}
|
|
}
|