Files
zeekay 6195916f03 Only a person writes how they sign in, an org's founder alone is made its owner, and a deleted org's name is given once
An org's admin runs members' profiles, roles and machine accounts, and
no longer writes a person's password, email, phone, factors, passkeys,
or the tokens and keys that speak as them: users.Credential admits the
person, a SuperAdmin or an application's allowlist. Nothing an admin
files for a member signs in as them once they own the org, or in another
org they belong to.

Onboarding writes the owner row only for the org's founder, its creator
or the recorded founder resuming, never for an admin re-driving its own
org, and records founders by subject id. An organization create names
its founder by subject id alone and runs in one transaction.

Deleting an org files a tombstone on the trail in the delete's
transaction, and Leftovers refuses the name forever after. Rekey refuses
a move that would take an org's last owner and keeps the higher role
where the new id already holds one. A membership names a live account
under its own id; a revoke is decided on the org-wide row and removes
every scope. An as() token acts in the key's org alone, and a target
holding an admin or owner role anywhere is refused. Role writes by
onboarding, Rekey and ForgetUser are on the trail.
2026-09-29 12:24:42 -07:00

86 lines
3.0 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package webauthn_test
// A passkey authenticates its User, so REGISTERING one for a person is acting for
// that account. The write authorizes that subject the same way the list authorizes
// reading it — otherwise a caller files a credential of its own under
// User=admin/root and the public signin ceremony, which offers a person's
// credentials by User, hands it to the attacker's authenticator. These drive the
// REAL router (newRig seeds admin/cert-hanzo), so the Guard attaches a principal
// and the write's gate runs.
import (
"context"
"io"
"net/http/httptest"
"strings"
"testing"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/internal/testhttp"
"github.com/hanzoai/iam/pkg/schema"
)
// write posts one credential registration as sub and returns the status.
func (r *rig) write(t *testing.T, sub, body string) int {
t.Helper()
req := httptest.NewRequest("POST", keys, strings.NewReader(body))
req.Host = "hanzo.id"
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+r.bearer(t, sub))
resp, err := testhttp.Do(r.app, req)
if err != nil {
t.Fatalf("POST: %v", err)
}
_, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close()
return resp.StatusCode
}
func (r *rig) credsFor(t *testing.T, user string) int {
t.Helper()
rows, err := orm.TypedQuery[schema.WebauthnCredential](r.db).Filter("User=", user).GetAll(context.Background())
if err != nil {
t.Fatalf("query %s: %v", user, err)
}
return len(rows)
}
// The passkey-plant is refused, and nothing lands under admin/root — so the signin
// ceremony that offers a person's credentials by User has none of the attacker's to
// offer.
func TestAdd_refusesAPasskeyPlantedForAReservedUser(t *testing.T) {
r := newRig(t)
before := r.credsFor(t, "admin/root")
if st := r.write(t, "hanzo/boss",
`{"owner":"hanzo","name":"plant","user":"admin/root","credentialId":"AAAA","publicKey":"AAAA","userVerified":true}`); st != 403 {
t.Fatalf("planting a passkey for admin/root: status=%d, want 403", st)
}
if after := r.credsFor(t, "admin/root"); after != before {
t.Fatalf("a credential landed under admin/root: %d -> %d", before, after)
}
}
// The same gate refuses a plant aimed at ANOTHER tenant's user.
func TestAdd_refusesAPasskeyPlantedForAnotherTenant(t *testing.T) {
r := newRig(t)
if st := r.write(t, "hanzo/boss",
`{"owner":"hanzo","name":"plant","user":"orgb/carol","credentialId":"AAAA","publicKey":"AAAA"}`); st != 403 {
t.Fatalf("planting a passkey for orgb/carol: status=%d, want 403", st)
}
}
// A passkey signs in as its person, so an org-admin files none for its own member
// either: a person enrolls their own through the WebAuthn ceremony.
func TestAdd_refusesAPasskeyForAnOwnMember(t *testing.T) {
r := newRig(t)
if st := r.write(t, "hanzo/boss",
`{"owner":"hanzo","name":"alice-newkey","user":"hanzo/alice","credentialId":"AAAA","publicKey":"AAAA"}`); st != 403 {
t.Fatalf("an admin registering a passkey for a member: status=%d, want 403", st)
}
}