Files
iam/internal/oidc/signup_org_test.go
zooqueenandHanzo Dev 86bd53c8ef Sign-up gives the founder their org's default key; a person's API key is their own
Provision files no service account. It ensures one key, <slug>/default
labelled "Default", held by the founder <slug>/<name> and filed in the org,
minted through keys.Issue: a pk- half, the secret kept as a digest and
revealed once, a replay naming the same key. The onboard and admin provision
responses return that key; founding sign-ups through Charter hold it.

keys.Issue is the one mint of a named, held secret key; MintAccountKey is
Issue at <owner>/<account>-key. store.OrgCredential and its special cases in
OrgAccounts and ForgetOrg are gone: every account in an org blocks its
deletion alike, and ForgetOrg removes the rows that name the org. The org
slug bound is the console's MAX_ORG_SLUG (60) again.

POST /v1/iam/keys from a person (not a confidential client, not a SuperAdmin)
binds a key that names nobody to the caller, in the org it is filed in, and
refuses any other holder (403); a member by membership may hold their own key
in that org. A person's update cannot change who a key names. A SuperAdmin
and a confidential client name the holder as before, within holdable.


Co-authored-by: Hanzo Dev <[email protected]>
2026-10-01 14:27:26 -07:00

444 lines
18 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"strings"
"testing"
policy "github.com/hanzoai/authz"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/internal/users"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// A signup at a founding application lands in an org of its OWN.
//
// The application's org is where an account is REGISTERED — its username is
// unique there, and its sign-in screen names it. It is not the tenant the person
// works in, and an application open to strangers must not put them in the tenant
// it happens to belong to, which is where the credentials and the spend are.
func TestSignup_FoundsItsOwnOrg(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
status, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": "[email protected]",
})
if status != 200 || env["status"] != "ok" {
t.Fatalf("signup failed: status=%d env=%v", status, env)
}
data, _ := env["data"].(map[string]any)
if data["owner"] == "hanzo" {
t.Fatal("the account was filed in the application's own org")
}
if data["owner"] != "stranger" {
t.Fatalf("owner = %v, want the personal org \"stranger\"", data["owner"])
}
// The org exists, is marked personal, and the account is really in it.
org, err := store.GetOrganizationByName(tctx(), db, "stranger")
if err != nil || org == nil {
t.Fatalf("personal org not founded: %v", err)
}
if !org.IsPersonal {
t.Error("the org is not marked personal")
}
if u, _ := store.GetUserByName(tctx(), db, "hanzo", "stranger"); u != nil {
t.Fatal("the account is still in the application's own org")
}
if u, _ := store.GetUserByName(tctx(), db, "stranger", "stranger"); u == nil {
t.Fatal("the account is not in its own org")
}
// Sole member. The roster is the (user × org) relation, and it holds one row.
rows, err := store.MembershipsByOrg(tctx(), db, "stranger")
if err != nil {
t.Fatalf("roster: %v", err)
}
if len(rows) != 1 || rows[0].User != "stranger/stranger" {
t.Fatalf("roster = %v, want exactly stranger/stranger", rows)
}
// And nothing carried the old tenant over.
for _, m := range mustMemberships(t, db, "stranger/stranger") {
if m.Org == "hanzo" {
t.Fatal("the account kept a membership in the application's own org")
}
}
}
// A founding signup (Charter) gives the person the org's one API key, held by
// them in the org it founded, with no account standing between them and it.
func TestSignup_FounderHoldsTheDefaultKey(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
status, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": "[email protected]",
})
if status != 200 || env["status"] != "ok" {
t.Fatalf("signup failed: status=%d env=%v", status, env)
}
founderKey(t, db, "stranger", "stranger/stranger")
}
// Two people whose addresses yield the same handle both get an org of their own.
// The slug is derived, so the second one cannot simply take the first one's.
func TestSignup_SecondPersonGetsTheirOwnOrgToo(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
for _, addr := range []string{"[email protected]", "[email protected]"} {
status, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": addr,
})
if status != 200 || env["status"] != "ok" {
t.Fatalf("signup %s failed: %v", addr, env)
}
}
first, _ := store.GetUserByEmail(tctx(), db, "dave", "[email protected]")
second, _ := store.GetUserByEmail(tctx(), db, "dave2", "[email protected]")
if first == nil || second == nil {
t.Fatalf("want one account in each of dave and dave2; got %v / %v", first, second)
}
}
// An address stays taken after the account that holds it moves into its own org.
//
// The uniqueness check reads the application's org, and founding empties that org
// of the very accounts it should be checking against — so without the second
// reach the address is free again, a second person registers it, and then NEITHER
// of them can sign in: two accounts of one application carrying one address name
// nobody, which is the refusal that keeps a person from being authenticated as
// somebody else.
func TestSignup_AddressStaysTakenAfterFounding(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
const addr = "[email protected]"
if _, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": addr,
}); env["status"] != "ok" {
t.Fatalf("first signup failed: %v", env)
}
_, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": addr,
})
if env["status"] != "error" {
t.Fatalf("the address was registered twice: %v", env)
}
if msg, _ := env["msg"].(string); msg != "email already exists" {
t.Fatalf("refusal = %q, want the uniqueness refusal", msg)
}
// The first account still signs in, which is what the refusal protects.
_, body := do(t, app, jsonReq("POST", PathLogin, map[string]string{
"organization": "hanzo", "username": addr, "password": "correct horse battery staple",
"application": "hanzo-cloud", "clientId": "hanzo-cloud",
"redirectUri": testRedirect, "scope": "openid", "type": "code",
}))
if m := decode(t, body); m["status"] != "ok" {
t.Fatalf("the account that holds the address cannot sign in: %v", m)
}
}
// A username too short to be an org slug still gets one. minOrgSlug is 2, so the
// derivation has to walk rather than hand provision a name it will refuse.
func TestSignup_ShortNameStillGetsAnOrg(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
status, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"username": "z", "password": "correct horse battery staple", "email": "[email protected]",
})
if status != 200 || env["status"] != "ok" {
t.Fatalf("signup failed: %v", env)
}
data, _ := env["data"].(map[string]any)
if data["owner"] == "hanzo" || data["owner"] == "z" {
t.Fatalf("owner = %v, want a slug of at least %d characters", data["owner"], minOrgSlug)
}
}
// A long address still gets an org, and its slug stays within the bound every
// org slug obeys.
func TestSignup_LongNameStillGetsAnOrg(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
long := strings.Repeat("a", 60)
for i, addr := range []string{long + "@example.com", long + "@other.example"} {
status, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": addr,
})
if status != 200 || env["status"] != "ok" {
t.Fatalf("signup %d failed: %v", i, env)
}
data, _ := env["data"].(map[string]any)
slug, _ := data["owner"].(string)
if slug == "" || slug == "hanzo" || len(slug) > maxOrgSlug {
t.Fatalf("slug %q: want an org of its own within the %d-character bound", slug, maxOrgSlug)
}
}
}
// A reserved system owner can never be founded, however the account is named. A
// user under "admin" is a SuperAdmin, so the derivation must walk past it.
func TestSignup_DerivedSlugNeverReservedOrg(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
status, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"username": "admin", "password": "correct horse battery staple", "email": "[email protected]",
})
if status != 200 || env["status"] != "ok" {
t.Fatalf("signup failed: %v", env)
}
data, _ := env["data"].(map[string]any)
if policy.IsReservedOrg(data["owner"].(string)) {
t.Fatalf("owner = %v, a reserved system org", data["owner"])
}
if u, _ := store.GetUserByName(tctx(), db, "admin", "admin"); u != nil {
t.Fatal("an account was created under the reserved admin org")
}
}
// An application that does not found orgs is untouched: its accounts stay in its
// own org. The change is per-application, declared, and narrow.
func TestSignup_AppThatDoesNotFoundIsUnchanged(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true})
seedOrg(t, db, "hanzo")
_, env := signupReq(t, app, map[string]string{
"application": "conf", "organization": "hanzo",
"username": "member", "password": "correct horse battery staple", "email": "[email protected]",
})
data, _ := env["data"].(map[string]any)
if data["owner"] != "hanzo" {
t.Fatalf("owner = %v, want hanzo — this app declares no founding", data["owner"])
}
if org, _ := store.GetOrganizationByName(tctx(), db, "member"); org != nil {
t.Fatal("an org was founded for an application that does not found orgs")
}
}
// A person who has their own org can still JOIN a team org. hanzo, lux and zoo
// are team orgs like any other — nothing about them is special, only membership
// varies — so restricting where a signup LANDS must not restrict who may join.
func TestSignup_TeamOrgStaysJoinable(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
for _, team := range []string{"hanzo", "lux", "zoo"} {
seedOrg(t, db, team)
}
_, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": "[email protected]",
})
if env["status"] != "ok" {
t.Fatalf("signup failed: %v", env)
}
me := "joiner/joiner"
for _, team := range []string{"hanzo", "lux", "zoo"} {
if _, err := store.EnsureMembership(tctx(), db, me, team, store.RoleMember); err != nil {
t.Fatalf("join %s: %v", team, err)
}
}
// The signed membership set carries the personal org as home plus every team.
user, _ := store.GetUserByName(tctx(), db, "joiner", "joiner")
if user == nil {
t.Fatal("the account is missing")
}
got := map[string]bool{}
for _, ref := range store.MemberOrgRefs(tctx(), db, user) {
got[ref.Org] = true
}
for _, want := range []string{"joiner", "hanzo", "lux", "zoo"} {
if !got[want] {
t.Errorf("membership set is missing %q: %v", want, got)
}
}
}
// The account can SIGN IN afterwards. The login screen names the application's
// org — it has no way to know a person's own — so an account founded into its own
// org is unreachable unless login resolves the accounts the application created.
// Without this the fix above is an outage: everybody who signs up is locked out
// the moment they come back.
func TestLogin_ResolvesAnAccountInItsOwnOrg(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
const pw = "correct horse battery staple"
_, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": pw, "email": "[email protected]",
})
if env["status"] != "ok" {
t.Fatalf("signup failed: %v", env)
}
// Exactly what the portal posts: the APPLICATION's org, and the address.
_, body := do(t, app, jsonReq("POST", PathLogin, map[string]string{
"organization": "hanzo", "username": "[email protected]", "password": pw,
"application": "hanzo-cloud", "clientId": "hanzo-cloud",
"redirectUri": testRedirect, "scope": "openid", "type": "code",
}))
m := decode(t, body)
if m["status"] != "ok" {
t.Fatalf("the account it just created cannot sign in: %v", m)
}
if code, _ := m["data"].(string); code == "" {
t.Fatal("no authorization code was minted")
}
}
// Every application of the org reaches the accounts the org registered. hanzo.ai,
// the console and the CLI are each their own client of org hanzo; a person who
// registered at one of them signs in at every other. Keyed by the one application,
// the account was unreachable anywhere but where it started — and a login screen
// can only ever name the org, never which of its entrances somebody first used.
func TestLogin_ReachesWhatASiblingApplicationRegistered(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedApp(t, db, appOpts{clientID: "hanzo-ai", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
const pw = "correct horse battery staple"
if _, env := signupReq(t, app, map[string]string{
"application": "hanzo-ai", "organization": "hanzo",
"password": pw, "email": "[email protected]",
}); env["status"] != "ok" {
t.Fatalf("signup failed: %v", env)
}
_, body := do(t, app, jsonReq("POST", PathLogin, map[string]string{
"organization": "hanzo", "username": "[email protected]", "password": pw,
"application": "hanzo-cloud", "clientId": "hanzo-cloud",
"redirectUri": testRedirect, "scope": "openid", "type": "code",
}))
m := decode(t, body)
if m["status"] != "ok" {
t.Fatalf("an account registered at hanzo.ai cannot sign in at the console: %v", m)
}
if code, _ := m["data"].(string); code == "" {
t.Fatal("no authorization code was minted")
}
}
// The reach stops at the org. Another org's registrations stay unreachable, so the
// tenant refusal login already makes is unchanged.
func TestLogin_DoesNotReachAnotherOrgsRegistrations(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedAppFull(t, db, fullApp{clientID: "lux-cloud", secret: "s3cret", org: "lux", signup: true, orgChoice: "create", redirects: []string{testRedirect}})
seedOrg(t, db, "hanzo")
seedOrg(t, db, "lux")
const pw = "correct horse battery staple"
if _, env := signupReq(t, app, map[string]string{
"application": "lux-cloud", "organization": "lux",
"password": pw, "email": "[email protected]",
}); env["status"] != "ok" {
t.Fatalf("signup failed: %v", env)
}
_, body := do(t, app, jsonReq("POST", PathLogin, map[string]string{
"organization": "hanzo", "username": "[email protected]", "password": pw,
"application": "hanzo-cloud", "clientId": "hanzo-cloud",
"redirectUri": testRedirect, "scope": "openid", "type": "code",
}))
if m := decode(t, body); m["status"] != "error" {
t.Fatalf("an account another org registered must not sign in here: %v", m)
}
}
// One address is one registration across the org's applications. Keyed by the one
// application, a person who registered at hanzo.ai and later signed up at the
// console got a second account and a second org — and then neither signed in,
// because one address on two of the org's registrations names nobody.
func TestSignup_AddressTakenAcrossTheOrgsApplications(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedApp(t, db, appOpts{clientID: "hanzo-ai", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
const addr = "[email protected]"
if _, env := signupReq(t, app, map[string]string{
"application": "hanzo-ai", "organization": "hanzo",
"password": "correct horse battery staple", "email": addr,
}); env["status"] != "ok" {
t.Fatalf("first signup failed: %v", env)
}
_, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": addr,
})
if msg, _ := env["msg"].(string); env["status"] != "error" || msg != "email already exists" {
t.Fatalf("the address was registered twice in one org: %v", env)
}
if org, _ := store.GetOrganizationByName(tctx(), db, "once2"); org != nil {
t.Fatal("a second org was founded for the same address")
}
}
// mustMemberships reads a user's membership rows or fails the test.
func mustMemberships(t *testing.T, db orm.DB, user string) []*schema.Membership {
t.Helper()
rows, err := store.MembershipsByUser(tctx(), db, user)
if err != nil {
t.Fatalf("memberships: %v", err)
}
return rows
}
// An org admin cannot plant an address as an account an application registered.
//
// The create an org admin reaches is users.Create, and its body is the whole user
// row. If that row could name its registering application, anyone who founded an
// org of their own could file a stranger's address under hanzo-cloud and the
// stranger's own signup would be refused as taken — or, with no password on the
// plant, their first social sign-in would be linked onto it.
func TestSignup_PlantedRegistrationDoesNotTakeTheAddress(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "hanzo-cloud", secret: "s3cret", redirectURIs: []string{testRedirect}, signup: true, orgChoice: "create"})
seedOrg(t, db, "hanzo")
seedOrg(t, db, "evil")
if _, err := users.New(db).Create(tctx(), &users.CreateInput{
User: schema.User{Owner: "evil", Name: "plant", Email: "[email protected]", SignupApplication: "hanzo-cloud"},
}); err != nil {
t.Fatalf("plant: %v", err)
}
_, env := signupReq(t, app, map[string]string{
"application": "hanzo-cloud", "organization": "hanzo",
"password": "correct horse battery staple", "email": "[email protected]",
})
if env["status"] != "ok" {
t.Fatalf("a planted row took the address from the person who holds it: %v", env)
}
}