Files
iam/internal/oidc/federation_unlink_test.go
zeekay ef2f42a2ec Decide SuperAdmin on the row's own org
schema.User.SuperAdmin is the one predicate: a person whose row's owner
is the admin org. The Guard sets Principal.Sudo from it when it loads
the caller, and assume/release, device approval, unlink, the bootstrap
credential freeze and the registry push gate ask the row directly.
store.IsSuperAdmin, which also admitted a membership of the admin org,
is gone. A brand org's user added to the admin org is an ordinary
member of their own org, and a service account or program in the admin
org is never SuperAdmin.

hanzoai/authz v1.10.42 reads the same rule off a token (Claims.Sudo on
orgs[0]) and lets a membership of the admin org open nothing, so such a
member no longer reads or edits the admin org's registry row either.
The token IAM mints is unchanged: orgs still opens with the row's
owner.

Two protective gates keep the whole admin org, machines and unheld
names included: no secret key names an account there
(store.SuperAdminKey), and only a SuperAdmin writes one
(users.Authorize). Both read the owner as written, so neither reads the
store.

Every request the Guard admits for a SuperAdmin is recorded as a
superadmin audit row, PlatformWritten, with its method, address, answer
and client address; so is a SuperAdmin's unlink of someone else's
sign-in method and a device approval into another org.

TestEverySuperAdminGateAdmitsTheAdminOrgsPeopleOnly drives ten
SuperAdmin-only requests through the router as two SuperAdmins, a brand
admin holding an admin-org membership and two admin-org machines, and
reads the trail row each one leaves.

Claude-Session: https://claude.ai/code/session_01HuMDfgFssPUptYUzgZ6Cp8
2026-09-27 19:24:51 -07:00

315 lines
13 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"context"
"net/url"
"testing"
policy "github.com/hanzoai/authz"
"github.com/hanzoai/orm"
"github.com/zap-proto/zip"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// Unlink self-authenticates (session cookie / bearer) on the public OIDC surface,
// so the harness only needs the one registered group — the same one the confidential
// flow mints the bearer from.
func newUnlinkServer(t *testing.T) (*zip.App, orm.DB) {
t.Helper()
db := openTestDB(t)
app := zip.New(zip.Config{AppName: "iam-unlink-test", DisableStartupMessage: true})
Route(app.Group(""), db) // public: authorize/login/token AND the self-authenticating unlink
return app, db
}
// linkGitHub declares a GitHub provider on the "conf" app (CanUnlink toggled) and
// stamps a GitHub subject onto the user, whose SignupApplication is that app.
func linkGitHub(t *testing.T, db orm.DB, user, subject string, canUnlink bool) {
t.Helper()
pv := orm.New[schema.Provider](db)
pv.Owner, pv.Name, pv.Category, pv.Type = "admin", "prov-github-unlink", "OAuth", "GitHub"
pv.SetId("admin/prov-github-unlink")
// Idempotent across sub-tests sharing a db is not needed (each test opens its own).
if err := pv.CreateCtx(context.Background()); err != nil {
t.Fatalf("seed provider: %v", err)
}
a, err := orm.Get[schema.Application](db, "admin/conf")
if err != nil {
t.Fatalf("load conf app: %v", err)
}
a.Providers = append(a.Providers, &schema.ProviderItem{Owner: "admin", Name: "prov-github-unlink", CanSignIn: true, CanUnlink: canUnlink})
if err := a.UpdateCtx(context.Background()); err != nil {
t.Fatalf("link provider: %v", err)
}
u := userRow(t, db, user)
u.GitHub = subject
u.SignupApplication = "conf"
if err := u.UpdateCtx(context.Background()); err != nil {
t.Fatalf("stamp connector: %v", err)
}
}
func doUnlink(t *testing.T, app *zip.App, bearer, providerType, owner, name string) (int, map[string]any) {
t.Helper()
req := jsonReq("POST", PathUnlink, map[string]any{
"providerType": providerType,
"user": map[string]string{"owner": owner, "name": name},
})
req.Header.Set("Authorization", "Bearer "+bearer)
resp, body := do(t, app, req)
return resp.StatusCode, decode(t, body)
}
// The account holder unlinks its own GitHub link when the app permits it; the
// connector column is cleared.
func TestUnlink_SelfClearsLinkWhenPermitted(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
linkGitHub(t, db, "alice", "gh-alice", true)
access := accessTokenFor(t, app, "openid") // bearer for hanzo/alice
if _, m := doUnlink(t, app, access, "GitHub", "hanzo", "alice"); m["status"] != "ok" {
t.Fatalf("self-unlink failed: %v", m["msg"])
}
if got := userRow(t, db, "alice").GitHub; got != "" {
t.Fatalf("self-unlink did not clear the connector, got %q", got)
}
}
// A holder cannot unlink ANOTHER account (not self, not super), and the target's
// link survives.
func TestUnlink_CrossUserRefused(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
seedUser(t, db, "bob", "[email protected]", "pw")
linkGitHub(t, db, "bob", "gh-bob", true)
access := accessTokenFor(t, app, "openid") // bearer for hanzo/alice
status, m := doUnlink(t, app, access, "GitHub", "hanzo", "bob")
if m["status"] != "error" {
t.Fatalf("cross-user unlink must be refused, got %v (status %d)", m, status)
}
if got := userRow(t, db, "bob").GitHub; got != "gh-bob" {
t.Fatalf("a non-owner removed bob's link: %q", got)
}
}
// When the application forbids unlinking (CanUnlink=false), a self-unlink is
// refused — an org that mandates federated sign-in keeps its users linked.
func TestUnlink_SelfRefusedWhenAppForbids(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
linkGitHub(t, db, "alice", "gh-alice", false)
access := accessTokenFor(t, app, "openid")
if _, m := doUnlink(t, app, access, "GitHub", "hanzo", "alice"); m["status"] != "error" {
t.Fatalf("self-unlink must be refused when the app forbids it, got %v", m)
}
if got := userRow(t, db, "alice").GitHub; got != "gh-alice" {
t.Fatalf("a forbidden unlink still cleared the connector: %q", got)
}
}
// An unauthenticated request is refused (the SDK envelope carries status:error on
// a 200, the casibase contract) and clears nothing.
func TestUnlink_RequiresAuthentication(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
linkGitHub(t, db, "alice", "gh-alice", true)
req := jsonReq("POST", PathUnlink, map[string]any{"providerType": "GitHub", "user": map[string]string{"owner": "hanzo", "name": "alice"}})
_, body := do(t, app, req) // no bearer, no session cookie
if m := decode(t, body); m["status"] != "error" {
t.Fatalf("unlink without authentication must be refused, got %v", m)
}
if got := userRow(t, db, "alice").GitHub; got != "gh-alice" {
t.Fatal("an unauthenticated request cleared the connector")
}
}
// --- the account's last credential, and the application a name resolves to ---
// clearPassword strips the account's password digest, leaving it as federated
// provisioning does (users.Create writes no digest for a row created without a
// password). The bearer is minted BEFORE this runs — how the person authenticated
// is a separate question from what the account can still be signed in as, which is
// what the unlink check reads.
func clearPassword(t *testing.T, db orm.DB, name string) {
t.Helper()
u := userRow(t, db, name)
u.PasswordHash, u.PasswordType = "", ""
if err := u.UpdateCtx(context.Background()); err != nil {
t.Fatalf("clear password: %v", err)
}
}
// A federated account with nothing else to sign in with keeps its link: unlinking
// it would destroy the account, with no password, no passkey and no wallet left.
func TestUnlink_RefusedWhenItIsTheOnlyCredential(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
linkGitHub(t, db, "alice", "gh-alice", true)
access := accessTokenFor(t, app, "openid")
clearPassword(t, db, "alice")
_, m := doUnlink(t, app, access, "GitHub", "hanzo", "alice")
if m["status"] != "error" {
t.Fatalf("unlinking the only credential must be refused, got %v", m)
}
if got := userRow(t, db, "alice").GitHub; got != "gh-alice" {
t.Fatalf("the account's only credential was removed: %q", got)
}
}
// A second linked provider IS another way in, so the first one may go.
func TestUnlink_PermittedWhenAnotherLinkRemains(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
linkGitHub(t, db, "alice", "gh-alice", true)
access := accessTokenFor(t, app, "openid")
clearPassword(t, db, "alice")
u := userRow(t, db, "alice")
u.Google = "goog-alice"
if err := u.UpdateCtx(context.Background()); err != nil {
t.Fatalf("stamp second connector: %v", err)
}
if _, m := doUnlink(t, app, access, "GitHub", "hanzo", "alice"); m["status"] != "ok" {
t.Fatalf("unlink with another link remaining must be permitted, got %v", m["msg"])
}
if got := userRow(t, db, "alice").GitHub; got != "" {
t.Fatalf("the link survived a permitted unlink: %q", got)
}
}
// A bound wallet is a credential too — the account can still sign in, so the link
// may go. Without this arm the check would refuse an unlink that is perfectly safe.
func TestUnlink_WalletIsACredential(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
linkGitHub(t, db, "alice", "gh-alice", true)
access := accessTokenFor(t, app, "openid")
clearPassword(t, db, "alice")
w := orm.New[schema.Wallet](db)
w.Owner, w.User, w.Chain, w.Address = "hanzo", "alice", "evm", "0xabc"
w.SetId("hanzo/alice/evm/0xabc")
if err := w.CreateCtx(context.Background()); err != nil {
t.Fatalf("bind wallet: %v", err)
}
if _, m := doUnlink(t, app, access, "GitHub", "hanzo", "alice"); m["status"] != "ok" {
t.Fatalf("unlink with a bound wallet must be permitted, got %v", m["msg"])
}
}
// A SuperAdmin is not bound by the last-credential refusal — it is the platform's
// recovery path, the same carve-out the CanUnlink flag already has.
func TestUnlink_SuperAdminMayRemoveTheOnlyCredential(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
seedUserInOrg(t, db, "admin", "z", "[email protected]", "pw") // a person in the reserved org
linkGitHub(t, db, "alice", "gh-alice", true)
clearPassword(t, db, "alice")
// A reserved-org person does not sign in through a tenant's application, so
// the bearer is signed directly under the trusted cert (directSubjectToken).
super := directSubjectToken(t, db, "cert-conf", "admin", "z")
if _, m := doUnlink(t, app, super, "GitHub", "hanzo", "alice"); m["status"] != "ok" {
t.Fatalf("a SuperAdmin must be able to force the unlink, got %v", m["msg"])
}
// Unpicking someone else's sign-in method is a platform act, so it is on the
// SuperAdmin trail, naming who did it and whose method went.
rows, err := orm.TypedQuery[schema.AuditLog](db).Filter("Action=", schema.ActionSuperAdmin).GetAll(context.Background())
if err != nil || len(rows) != 1 {
t.Fatalf("SuperAdmin trail = %d rows (%v), want 1", len(rows), err)
}
if r := rows[0]; r.User != "admin/z" || r.Organization != "hanzo" || r.Object != "hanzo/alice github" || r.RequestUri != PathUnlink {
t.Fatalf("trail row user=%q org=%q object=%q uri=%q, want admin/z unlinking hanzo/alice's github at %s",
r.User, r.Organization, r.Object, r.RequestUri, PathUnlink)
}
}
// An admin-org membership held from a brand org is not the recovery path. The
// account lives in hanzo, so it is not a SuperAdmin, and unlinking someone
// else's method is refused exactly as TestUnlink_CrossUserRefused refuses it
// without the membership.
func TestUnlink_AnAdminMembershipIsNotTheRecoveryPath(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
seedUser(t, db, "op", "[email protected]", "pw") // home org hanzo, NOT the reserved org
if _, err := store.EnsureMembership(tctx(), db, "hanzo/op", policy.AdminOrg, store.RoleAdmin); err != nil {
t.Fatalf("grant the reserved-org membership: %v", err)
}
linkGitHub(t, db, "alice", "gh-alice", true)
clearPassword(t, db, "alice")
code, _, _ := loginForCode(t, app, map[string]string{
"organization": "hanzo", "username": "op", "password": "pw",
"clientId": "conf", "redirectUri": testRedirect, "scope": "openid",
})
_, tok := exchangeCode(t, app, url.Values{
"code": {code}, "client_id": {"conf"}, "client_secret": {"s3cret"}, "redirect_uri": {testRedirect},
})
member, _ := tok["access_token"].(string)
if member == "" {
t.Fatalf("hanzo/op must be able to sign in: %v", tok)
}
if _, m := doUnlink(t, app, member, "GitHub", "hanzo", "alice"); m["status"] != "error" {
t.Fatalf("an admin-org membership forced another person's unlink: %v", m)
}
if u := userRow(t, db, "alice"); u.GitHub != "gh-alice" {
t.Fatalf("alice's GitHub link = %q after a refusal", u.GitHub)
}
}
// The signup application is resolved by NAME, so a TENANT-registered one is found
// and its CanUnlink flag is honoured. Pinning the lookup to the "admin" registry
// missed the row and fail-closed every unlink for such an account.
func TestUnlink_TenantOwnedSignupApplicationResolves(t *testing.T) {
app, db := newUnlinkServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
linkGitHub(t, db, "alice", "gh-alice", true)
// The same provider item, on an application the TENANT owns.
tenant := orm.New[schema.Application](db)
tenant.Owner, tenant.Name, tenant.Organization = "hanzo", "storefront", "hanzo"
tenant.Providers = []*schema.ProviderItem{{Owner: "admin", Name: "prov-github-unlink", CanSignIn: true, CanUnlink: true}}
tenant.SetId("hanzo/storefront")
if err := tenant.CreateCtx(context.Background()); err != nil {
t.Fatalf("seed tenant app: %v", err)
}
u := userRow(t, db, "alice")
u.SignupApplication = "storefront"
if err := u.UpdateCtx(context.Background()); err != nil {
t.Fatalf("repoint signup application: %v", err)
}
access := accessTokenFor(t, app, "openid")
if _, m := doUnlink(t, app, access, "GitHub", "hanzo", "alice"); m["status"] != "ok" {
t.Fatalf("a tenant-owned signup application must resolve, got %v", m["msg"])
}
if got := userRow(t, db, "alice").GitHub; got != "" {
t.Fatalf("the link survived a permitted unlink: %q", got)
}
}