mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-21 02:59:42 +00:00
POST /v1/iam/auth/identifier {clientId, identifier} answers {account, password}, resolved as login resolves the address, only for an application that registers strangers (codeSignup's gate) and only for an email. One client (CF-Connecting-IP, else the last forwarded hop; an IPv6 /64 is one) asks 20 times in ten minutes, the process answers 10k in all, and at most 100k clients are counted at once. POST /v1/iam/signup takes terms and aup, both or neither, and records them with method "password".
Co-authored-by: Hanzo Dev <[email protected]>
432 lines
17 KiB
Go
432 lines
17 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/orm"
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/iam/internal/users"
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// seedOrg creates an organization row (owner "admin", v1 convention) with the
|
|
// given name and optional password-complexity options.
|
|
func seedOrg(t *testing.T, db orm.DB, name string, passwordOptions ...string) {
|
|
t.Helper()
|
|
o := orm.New[schema.Organization](db)
|
|
o.Owner = "admin"
|
|
o.Name = name
|
|
o.PasswordOptions = passwordOptions
|
|
o.SetId("admin/" + name)
|
|
if err := o.CreateCtx(context.Background()); err != nil {
|
|
t.Fatalf("seed org: %v", err)
|
|
}
|
|
}
|
|
|
|
// signupReq drives POST /v1/iam/signup and returns the status + decoded envelope.
|
|
func signupReq(t *testing.T, app *zip.App, body map[string]string) (int, map[string]any) {
|
|
t.Helper()
|
|
resp, raw := do(t, app, jsonReq("POST", PathSignup, body))
|
|
return resp.StatusCode, decode(t, raw)
|
|
}
|
|
|
|
// The happy path creates the account, returns it REDACTED (owner/name present,
|
|
// no secret), and stores the password as an argon2id hash — never plaintext.
|
|
func TestSignup_HappyPathCreatesRedactedUser(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true})
|
|
seedOrg(t, db, "hanzo")
|
|
|
|
const pw = "correct horse battery staple"
|
|
status, env := signupReq(t, app, map[string]string{
|
|
"application": "conf",
|
|
"organization": "hanzo",
|
|
"username": "newbie",
|
|
"password": pw,
|
|
"name": "New Bie",
|
|
"email": "[email protected]",
|
|
})
|
|
if status != 200 || env["status"] != "ok" {
|
|
t.Fatalf("status=%d env=%v, want 200 ok", status, env)
|
|
}
|
|
data, ok := env["data"].(map[string]any)
|
|
if !ok {
|
|
t.Fatalf("data is not an object: %v", env["data"])
|
|
}
|
|
if data["owner"] != "hanzo" || data["name"] != "newbie" {
|
|
t.Errorf("data owner/name = %v/%v, want hanzo/newbie", data["owner"], data["name"])
|
|
}
|
|
// The response must never carry the digest or any secret.
|
|
for _, secret := range []string{"passwordHash", "passwordSalt", "accessSecret", "accessSecretHash"} {
|
|
if v, present := data[secret]; present && v != "" {
|
|
t.Errorf("signup response leaked %q = %v", secret, v)
|
|
}
|
|
}
|
|
|
|
// The STORED row holds an argon2id hash (PasswordType=argon2id) that verifies the
|
|
// password — and is NOT the plaintext. This is the no-plaintext contract.
|
|
stored, err := store.GetUserByName(context.Background(), db, "hanzo", "newbie")
|
|
if err != nil || stored == nil {
|
|
t.Fatalf("stored user lookup: %v (nil=%v)", err, stored == nil)
|
|
}
|
|
if stored.PasswordHash == "" || stored.PasswordHash == pw {
|
|
t.Fatalf("password stored as plaintext or empty: %q", stored.PasswordHash)
|
|
}
|
|
if stored.PasswordType != "argon2id" {
|
|
t.Errorf("PasswordType = %q, want argon2id", stored.PasswordType)
|
|
}
|
|
if !users.VerifyPassword(stored, pw, "") {
|
|
t.Error("stored hash does not verify the signup password")
|
|
}
|
|
if users.VerifyPassword(stored, "wrong", "") {
|
|
t.Error("a wrong password verified — hashing is broken")
|
|
}
|
|
}
|
|
|
|
// Every failure mode returns {status:"error"} on a 200 (the casibase envelope)
|
|
// and creates no user.
|
|
func TestSignup_Errors(t *testing.T) {
|
|
newbieBody := func() map[string]string {
|
|
return map[string]string{
|
|
"application": "conf", "organization": "hanzo",
|
|
"username": "newbie", "password": "correct horse battery staple",
|
|
}
|
|
}
|
|
|
|
t.Run("missing required fields", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", signup: true})
|
|
seedOrg(t, db, "hanzo")
|
|
status, env := signupReq(t, app, map[string]string{"application": "conf", "organization": "hanzo"})
|
|
if status != 400 || env["status"] != "error" {
|
|
t.Fatalf("status=%d env=%v, want 400 error", status, env)
|
|
}
|
|
})
|
|
|
|
t.Run("application does not exist", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedOrg(t, db, "hanzo")
|
|
body := newbieBody()
|
|
body["application"] = "ghost"
|
|
_, env := signupReq(t, app, body)
|
|
if env["status"] != "error" {
|
|
t.Fatalf("want error, got %v", env)
|
|
}
|
|
})
|
|
|
|
t.Run("signup disabled", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", signup: false}) // EnableSignUp=false
|
|
seedOrg(t, db, "hanzo")
|
|
_, env := signupReq(t, app, newbieBody())
|
|
if env["status"] != "error" {
|
|
t.Fatalf("signup must be refused when disabled, got %v", env)
|
|
}
|
|
if u, _ := store.GetUserByName(context.Background(), db, "hanzo", "newbie"); u != nil {
|
|
t.Error("a user was created despite signup being disabled")
|
|
}
|
|
})
|
|
|
|
t.Run("organization does not exist", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", signup: true}) // no org seeded
|
|
_, env := signupReq(t, app, newbieBody())
|
|
if env["status"] != "error" {
|
|
t.Fatalf("want error for missing org, got %v", env)
|
|
}
|
|
})
|
|
|
|
t.Run("username already taken", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", signup: true})
|
|
seedOrg(t, db, "hanzo")
|
|
seedUser(t, db, "newbie", "[email protected]", "pw") // already exists
|
|
_, env := signupReq(t, app, newbieBody())
|
|
if env["status"] != "error" || env["msg"] != "username already exists" {
|
|
t.Fatalf("want 'username already exists', got %v", env)
|
|
}
|
|
})
|
|
|
|
// A DISPLAY NAME is not a username in any spelling. This is the shape that put
|
|
// `name: "Grace Hopper"` on a live token — the claim was mis-sourced there, but
|
|
// the same string must never be able to arrive as a username either, or the two
|
|
// hypotheses become indistinguishable in the data.
|
|
t.Run("display name refused as username", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", signup: true})
|
|
seedOrg(t, db, "hanzo")
|
|
body := newbieBody()
|
|
body["username"] = "Grace Hopper"
|
|
_, env := signupReq(t, app, body)
|
|
if env["status"] != "error" {
|
|
t.Fatalf("a display name must be refused as a username, got %v", env)
|
|
}
|
|
for _, spelling := range []string{"Grace Hopper", "Grace Hopper"} {
|
|
if u, _ := store.GetUserByName(context.Background(), db, "hanzo", spelling); u != nil {
|
|
t.Errorf("a user named %q was created", spelling)
|
|
}
|
|
}
|
|
})
|
|
|
|
// F-I2: a "/" in a username would inject a spurious owner/name separator into the
|
|
// subject discriminator — forbidden at the endpoint.
|
|
t.Run("username with slash refused", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", signup: true})
|
|
seedOrg(t, db, "hanzo")
|
|
body := newbieBody()
|
|
body["username"] = "foo/bar"
|
|
_, env := signupReq(t, app, body)
|
|
if env["status"] != "error" {
|
|
t.Fatalf("username containing '/' must be refused, got %v", env)
|
|
}
|
|
if u, _ := store.GetUserByName(context.Background(), db, "hanzo", "foo/bar"); u != nil {
|
|
t.Error("a user with a '/' username was created")
|
|
}
|
|
})
|
|
|
|
t.Run("password fails org complexity", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", signup: true})
|
|
seedOrg(t, db, "hanzo", "AtLeast8") // require 8+ chars
|
|
body := newbieBody()
|
|
body["password"] = "short"
|
|
_, env := signupReq(t, app, body)
|
|
if env["status"] != "error" {
|
|
t.Fatalf("short password must be refused under AtLeast8, got %v", env)
|
|
}
|
|
if u, _ := store.GetUserByName(context.Background(), db, "hanzo", "newbie"); u != nil {
|
|
t.Error("a user was created despite the password failing policy")
|
|
}
|
|
})
|
|
}
|
|
|
|
// ── self-serve organizations ─────────────────────────────────────────────────────
|
|
|
|
// Signup does not mint organizations, whatever the app's org-choice mode says.
|
|
// An unauthenticated request must not be able to add a row to the tenant registry,
|
|
// and it does not need to: POST /v1/iam/onboard creates a person's org from their
|
|
// own authenticated session, atomically, with its metered credential.
|
|
func TestSignup_DoesNotMintAnOrganization(t *testing.T) {
|
|
for _, mode := range []string{"", "select", "create"} {
|
|
t.Run("orgChoiceMode="+mode, func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: mode})
|
|
|
|
status, env := signupReq(t, app, map[string]string{
|
|
"application": "conf",
|
|
"organization": "acme",
|
|
"username": "founder",
|
|
"password": "correct horse battery staple",
|
|
})
|
|
if status == 200 && env["status"] == "ok" {
|
|
t.Fatalf("orgChoiceMode=%q minted an org from an unauthenticated signup: %v", mode, env)
|
|
}
|
|
if org, _ := store.GetOrganizationByName(tctx(), db, "acme"); org != nil {
|
|
t.Fatal("an organization row was created")
|
|
}
|
|
if u, _ := store.GetUserByName(tctx(), db, "acme", "founder"); u != nil {
|
|
t.Fatal("a user was created in an org that does not exist")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// THE privilege escalation to refuse: a user under "admin" is a SuperAdmin. Turning
|
|
// on self-serve creation must not open a path to minting the reserved org, and the
|
|
// refusal must hold even with the opt-in set.
|
|
func TestSignup_SelfServeCannotMintReservedOrg(t *testing.T) {
|
|
for _, reserved := range []string{"admin", "built-in"} {
|
|
t.Run(reserved, func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
|
|
|
|
status, env := signupReq(t, app, map[string]string{
|
|
"application": "conf",
|
|
"organization": reserved,
|
|
"username": "attacker",
|
|
"password": "correct horse battery staple",
|
|
})
|
|
if status == 200 && env["status"] == "ok" {
|
|
t.Fatalf("signup into reserved org %q must be refused", reserved)
|
|
}
|
|
if u, _ := store.GetUserByName(tctx(), db, reserved, "attacker"); u != nil {
|
|
t.Fatalf("no user may be created under reserved org %q", reserved)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// ── the password floor ───────────────────────────────────────────────────────
|
|
// An organization that declares NO PasswordOptions must still get a real policy.
|
|
// This is the hole that was live: a self-serve org is created
|
|
// with an empty option set, an empty set used to mean "any non-empty password",
|
|
// and an anonymous caller registered a production account with the password "a"
|
|
// and then logged in with it.
|
|
func TestSignup_PasswordFloorAppliesWithoutOrgOptions(t *testing.T) {
|
|
// Deliberately seeded with NO options — the self-serve org's shape.
|
|
for _, weak := range []string{"a", "aaaaaaaa", "1234567", "short"} {
|
|
t.Run(weak, func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true})
|
|
seedOrg(t, db, "hanzo")
|
|
|
|
status, env := signupReq(t, app, map[string]string{
|
|
"application": "conf", "organization": "hanzo",
|
|
"username": "attacker", "password": weak,
|
|
})
|
|
if status == 200 && env["status"] == "ok" {
|
|
t.Fatalf("password %q must be refused by the floor, got %v", weak, env)
|
|
}
|
|
if u, _ := store.GetUserByName(tctx(), db, "hanzo", "attacker"); u != nil {
|
|
t.Fatalf("a user was created with the weak password %q", weak)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The floor must not cost the funnel: a strong password into an optionless org
|
|
// still succeeds. This is the case that must keep working.
|
|
func TestSignup_PasswordFloorAllowsStrongPassword(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true})
|
|
seedOrg(t, db, "hanzo")
|
|
|
|
status, env := signupReq(t, app, map[string]string{
|
|
"application": "conf", "organization": "hanzo",
|
|
"username": "founder", "password": "correct horse battery staple",
|
|
})
|
|
if status != 200 || env["status"] != "ok" {
|
|
t.Fatalf("a strong password must still be accepted: status=%d env=%v", status, env)
|
|
}
|
|
}
|
|
|
|
// Org choice lets a founder NAME THEIR OWN org; it must never admit them to a
|
|
// tenant that is already standing. In a shared registry an unauthenticated signup
|
|
// that names an app with orgChoiceMode=create and an EXISTING foreign org would
|
|
// otherwise land a user in that tenant — every brand and every customer reachable
|
|
// through one signup. It must be refused.
|
|
func TestSignup_CannotJoinAnExistingForeignTenant(t *testing.T) {
|
|
app, db := newServer(t)
|
|
// The app belongs to org "hanzo" (seedApp) and opts in to self-serve creation.
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
|
|
// A tenant that already exists and belongs to someone else.
|
|
seedOrg(t, db, "lux")
|
|
|
|
status, env := signupReq(t, app, map[string]string{
|
|
"application": "conf",
|
|
"organization": "lux",
|
|
"username": "intruder",
|
|
"password": "correct horse battery staple",
|
|
})
|
|
if status == 200 && env["status"] == "ok" {
|
|
t.Fatalf("REFUSAL FAILED: a stranger signed up into the foreign tenant 'lux': %v", env)
|
|
}
|
|
if u, _ := store.GetUserByName(tctx(), db, "lux", "intruder"); u != nil {
|
|
t.Fatalf("REFUSAL FAILED: user %q now exists inside tenant lux", u.Name)
|
|
}
|
|
// The refusal must not be an authority oracle: it is the same sentence the
|
|
// wrong-tenant and reserved-org refusals use, so a prober learns nothing about
|
|
// which orgs exist or what this app is allowed to do.
|
|
if msg, _ := env["msg"].(string); msg != "the user is not permitted to sign up to this application" {
|
|
t.Fatalf("refusal message %q distinguishes this case from the other tenant refusals", msg)
|
|
}
|
|
}
|
|
|
|
// The legitimate destinations must keep working, or the refusal above has simply
|
|
// broken signup: an org the account founds, and the app's OWN tenant where the app
|
|
// serves no other. A brand-new org the caller names is not one of them — signup
|
|
// does not mint orgs — and neither is a standing tenant a shared app is handed;
|
|
// that tenant is reached by its admin's invitation (signup_invite_test.go).
|
|
func TestSignup_ForeignTenantRefusalKeepsTheLegitimatePaths(t *testing.T) {
|
|
t.Run("the app's own tenant", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
|
|
seedOrg(t, db, "hanzo")
|
|
status, env := signupReq(t, app, map[string]string{
|
|
"application": "conf", "organization": "hanzo",
|
|
"username": "member", "password": "correct horse battery staple",
|
|
})
|
|
if status != 200 || env["status"] != "ok" {
|
|
t.Fatalf("signup into the app's own tenant must still succeed: status=%d env=%v", status, env)
|
|
}
|
|
})
|
|
|
|
// A SHARED app is multi-tenant at sign-in. It once admitted a signup into any
|
|
// standing tenant it was handed, and this subtest asserted that it still did —
|
|
// which is the hole: the tenant never asked for the member.
|
|
t.Run("a shared app does not join an existing tenant by name", func(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, shared: true})
|
|
seedOrg(t, db, "lux")
|
|
status, env := signupReq(t, app, map[string]string{
|
|
"application": "conf", "organization": "lux",
|
|
"username": "member", "password": "correct horse battery staple",
|
|
})
|
|
if status == 200 && env["status"] == "ok" {
|
|
t.Fatalf("a shared app admitted a signup into lux by name: %v", env)
|
|
}
|
|
if u, _ := store.GetUserByName(tctx(), db, "lux", "member"); u != nil {
|
|
t.Fatal("member now exists inside tenant lux")
|
|
}
|
|
})
|
|
}
|
|
|
|
// A password signup records the terms the screen had the person agree to, as a
|
|
// code signup does; one half of the pair is refused, and an account is not made.
|
|
func TestSignup_RecordsTheAcceptedTerms(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true})
|
|
seedOrg(t, db, "hanzo")
|
|
body := func(name string) map[string]string {
|
|
return map[string]string{
|
|
"application": "conf",
|
|
"organization": "hanzo",
|
|
"username": name,
|
|
"password": "correct horse battery staple",
|
|
"email": name + "@hanzo.ai",
|
|
"terms": "terms-2026-09-30",
|
|
"aup": "aup-2026-09-30",
|
|
}
|
|
}
|
|
|
|
if status, env := signupReq(t, app, body("agreed")); status != 200 || env["status"] != "ok" {
|
|
t.Fatalf("status=%d env=%v, want 200 ok", status, env)
|
|
}
|
|
u, err := store.GetUserByName(tctx(), db, "hanzo", "agreed")
|
|
if err != nil || u == nil {
|
|
t.Fatalf("the account is missing: %v", err)
|
|
}
|
|
tm, ok := u.TermsAccepted()
|
|
if !ok || tm.Terms != "terms-2026-09-30" || tm.AUP != "aup-2026-09-30" || tm.Method != methodPassword || tm.Time == "" {
|
|
t.Fatalf("acceptance not recorded: %+v ok=%v", tm, ok)
|
|
}
|
|
|
|
half := body("half")
|
|
delete(half, "aup")
|
|
if _, env := signupReq(t, app, half); env["status"] != "error" {
|
|
t.Fatalf("half the terms made an account: %v", env)
|
|
}
|
|
if u, _ := store.GetUserByName(tctx(), db, "hanzo", "half"); u != nil {
|
|
t.Fatal("an account was made with half the terms")
|
|
}
|
|
|
|
none := body("silent")
|
|
delete(none, "terms")
|
|
delete(none, "aup")
|
|
if status, env := signupReq(t, app, none); status != 200 || env["status"] != "ok" {
|
|
t.Fatalf("no terms: status=%d env=%v, want 200 ok", status, env)
|
|
}
|
|
if u, _ := store.GetUserByName(tctx(), db, "hanzo", "silent"); u == nil {
|
|
t.Fatal("a signup naming no terms was refused")
|
|
} else if _, ok := u.TermsAccepted(); ok {
|
|
t.Fatal("terms recorded that nobody stated")
|
|
}
|
|
}
|