mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
The second factor could not be ENROLLED by anyone and, for text and email, could
not be USED by anyone. Both suites were green the whole time, because each side
tested its own shape and the two shapes never met.
WHY NOBODY COULD ENROL
hanzo.id posts every /v1/iam/mfa/setup/* parameter on the QUERY STRING with an
empty body; three of the five handlers read only the body. So `enable` and
`preferred` answered 400 "invalid body" to the exact request the shipped bundle
sends, and any org that set a Required factor was hard-locked out — the enrol
screen has no skip, and its only way forward is the call that 400s.
decode now reads the body, then lets the query fill what the body did not carry.
That is the SAME precedence zip's typed binder applies to every typed op and the
same one loginHandler applies to the authorize parameters; the surface was the
only place in the repo that insisted on one source. The asymmetry that hid it —
initiate and disable ignoring the decode error while three others returned it —
is gone with it.
WHY SMS AND EMAIL WERE UNREACHABLE
MfaPhoneEnabled and MfaEmailEnabled had four readers and NO writer: the gate
consulted flags nothing could set, so those factors were never offered. And even
enabled they could not be completed — the challenge answered NextMfa and
delivered nothing.
Three pieces, each in its own place:
- internal/otp is the one-time code, extracted out of oidc: mint it, word it,
file it, deliver it, spend it. The canonical receiver moves here with it, so
the one form a destination is keyed in now also covers the code a CHALLENGE
sends, not just the two readers in the send endpoint. store.LooksLikePhone is
the one shape test behind it — sign-in's identifier resolution asked the same
question with its own copy.
- factor.Add/Remove/Prefer are the writers those columns never had, and
factor.Destination (moved from oidc) is where a delivered factor's code goes:
the address on the ACCOUNT, never one from the request.
- the gate SENDS the code for the factor it offers, to that same
factor.Destination, so the send key and the verify key are one value.
ENROLMENT NOW PROVES POSSESSION
enable wrote on the strength of a `secret` field alone, accepted an empty
recovery list, and stored whatever digests the body carried. A client that
skipped the advisory verify — or scanned the QR into the wrong app — switched on
a factor no code could answer, and the account was then locked out with no
self-service exit: the gate holds the sign-in before minting, so the person can
never obtain the bearer that disable requires.
enable now requires a passcode and checks it against the secret it is being asked
to store; for sms and email the delivered code IS that proof. Recovery codes are
minted SERVER-side and returned once, so what the person writes down is what the
digests were made from — and there are eight, because one code is a single
mistake away from an account nobody can recover. The separate verify step is
DELETED rather than kept: two calls where the first is unenforced is exactly how
the unanswerable factor got written.
A CHALLENGE IS ANSWERED ONLY BY WHAT IT OFFERED
finishMfa checked only that the answering factor differed from the one already
proven, so an authenticator-only account was passable with an emailed code: a
deliberate choice of TOTP silently degraded to possession of a mailbox. The
offered set now rides on the challenge row — a property of the challenge, not of
either kind's payload, since both kinds have one — and is decided once, when the
sign-in is held.
The verify switch existed TWICE and had already drifted: the password path
accepted a delivered code and the federated path refused it, so an account
holding only email or SMS could answer one login and not the other. There is now
one proveFactor, called by both.
AND THREE MORE THAT ALL SAID "PROCEED" WHEN THEY SHOULD NOT
- PreferredMfaType accepted any string, and factor.Enabled reads that column —
so a factor nobody enrolled reported "MFA is on" and left the gate nothing to
ask for. Prefer is the one writer and holds the invariant: the preferred type
names a factor the account HOLDS.
- the gate's empty allow-list collapsed three situations into a pass. They are
now distinct: nothing enrolled but claimed -> enrolment; held but unservable
(no notify, no address) -> fail closed; already proven -> proceed. That last
one has to hand the sign-in back rather than answer it, or the request hangs
on an empty 200.
- "remember this device" was a deadline on the user row, so a factor proven in
one browser skipped it in EVERY browser. It is now a bearer token held by the
browser that answered, compared against a digest on the account — the same
shape as a recovery code and as the challenge id, no new scheme.
Finally, changing a factor ends the account's other sessions and closes any open
remember window. The session branch of login reaches loginGrant with no
credential at all, so turning 2FA on used to evict nobody — including whoever the
factor was being added because of — for the full 14-day session life.
133 lines
4.8 KiB
Go
133 lines
4.8 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/iam/internal/otp"
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
)
|
|
|
|
// fakeSender records what it was asked to deliver and fails on demand. It keeps the
|
|
// whole Message, because the code now travels inside the worded body — where a person
|
|
// reads it, and the only place a test can learn it from either (see codeIn).
|
|
type fakeSender struct {
|
|
err error
|
|
sent []otp.Message
|
|
}
|
|
|
|
func (f *fakeSender) Send(_ context.Context, m otp.Message) error {
|
|
f.sent = append(f.sent, m)
|
|
return f.err
|
|
}
|
|
|
|
// bindSender installs s for the duration of one test and unbinds after, so these
|
|
// tests can run in any order. Nothing binds a sender at package init, so unbound is
|
|
// the state to return to.
|
|
func bindSender(t *testing.T, s otp.Sender) {
|
|
t.Helper()
|
|
otp.BindSender(s)
|
|
t.Cleanup(func() { otp.BindSender(nil) })
|
|
}
|
|
|
|
// A code sign-in is offered only when a code can actually reach a person.
|
|
//
|
|
// Two independent facts have to hold and they were conflated into one: the
|
|
// application switch says the ORG wants email/SMS codes, and DeliveryConfigured
|
|
// says the SERVER can send one. Only the first was consulted, so every app
|
|
// advertised `code: true` while the delivery seam was unbound — measured against
|
|
// production, where a send to [email protected], an address that cannot exist,
|
|
// answered {status:"ok"}.
|
|
func TestCodeSigninNeedsBothTheSwitchAndDelivery(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
enabled bool
|
|
bound bool
|
|
want bool
|
|
}{
|
|
{"wanted and deliverable", true, true, true},
|
|
{"wanted but nothing can send it", true, false, false},
|
|
{"deliverable but the org said no", false, true, false},
|
|
{"neither", false, false, false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if tc.bound {
|
|
bindSender(t, &fakeSender{})
|
|
} else {
|
|
bindSender(t, nil)
|
|
}
|
|
if got := tc.enabled && otp.DeliveryConfigured(); got != tc.want {
|
|
t.Errorf("code offered = %v, want %v (switch=%v bound=%v)",
|
|
got, tc.want, tc.enabled, tc.bound)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// DeliveryConfigured must answer from the BOUND SENDER, never from configuration.
|
|
//
|
|
// The first version of this gate keyed on IAM_NOTIFY_ADDR. Nothing else in the
|
|
// repo read that variable, so setting it would have restored the button and
|
|
// silenced the endpoint's refusal while still sending nothing — re-arming the
|
|
// exact {status:"ok"} lie the gate exists to remove. An address is a CLAIM that
|
|
// delivery exists; a sender IS delivery.
|
|
func TestDeliveryIsDecidedByTheSenderNotAnAddress(t *testing.T) {
|
|
bindSender(t, nil)
|
|
t.Setenv("IAM_NOTIFY_ADDR", "notify.hanzo.svc:8000")
|
|
if otp.DeliveryConfigured() {
|
|
t.Error("an address alone reported delivery configured — nothing would have been sent")
|
|
}
|
|
|
|
bindSender(t, &fakeSender{})
|
|
t.Setenv("IAM_NOTIFY_ADDR", "")
|
|
if !otp.DeliveryConfigured() {
|
|
t.Error("a bound sender must report delivery configured, address or not")
|
|
}
|
|
}
|
|
|
|
// The login descriptor is the screen's source of truth, so the switch must be
|
|
// masked THERE too — leaving it on would draw the button whatever authMethods says.
|
|
// The org's stored setting is not modified; only what the browser is told.
|
|
func TestLoginViewMasksUndeliverableCodeSignin(t *testing.T) {
|
|
app := &schema.Application{EnableCodeSignin: true, EnablePassword: true}
|
|
|
|
bindSender(t, nil)
|
|
if v := loginView(app); v.EnableCodeSignin {
|
|
t.Error("code sign-in advertised with no delivery configured")
|
|
}
|
|
if !app.EnableCodeSignin {
|
|
t.Error("the org's stored setting was mutated; only the VIEW may be masked")
|
|
}
|
|
if v := loginView(app); !v.EnablePassword {
|
|
t.Error("password sign-in must be unaffected")
|
|
}
|
|
|
|
bindSender(t, &fakeSender{})
|
|
if v := loginView(app); !v.EnableCodeSignin {
|
|
t.Error("code sign-in must return once a sender is bound — no second switch to flip")
|
|
}
|
|
}
|
|
|
|
// A sender that fails must be reported as a failure. Answering ok because the code
|
|
// was minted recreates the same lie one layer down: the caller asked for a send.
|
|
func TestSendFailureIsReportedNotSwallowed(t *testing.T) {
|
|
f := &fakeSender{err: errors.New("twilio: 21608 unverified number")}
|
|
bindSender(t, f)
|
|
|
|
// Addressed by hand: what the words say is otp's business (and otp's test); what
|
|
// this asserts is that a transport failure reaches the endpoint.
|
|
m := otp.Message{Org: "hanzo", Channel: otp.Email, To: "[email protected]", Body: "code 123456"}
|
|
if err := f.Send(context.Background(), m); err == nil {
|
|
t.Fatal("a failing sender must surface its error to the endpoint")
|
|
}
|
|
if len(f.sent) != 1 {
|
|
t.Fatalf("sender was called %d times, want 1", len(f.sent))
|
|
}
|
|
if got := f.sent[0]; got.Org != "hanzo" || got.Channel != otp.Email || got.To != "[email protected]" {
|
|
t.Errorf("sender got %+v — org, channel and destination must all reach it", got)
|
|
}
|
|
}
|