Files
iam/internal/oidc/orgs_claim_test.go
zeekayandClaude Opus 5.5 4d6831cab9 A client_credentials token is minted only for an app that declares the grant
The grant answered for any application holding a secret. A secret says a
client is confidential, not that it is a machine: a server-side web app
keeps one for its code exchange, and that secret alone minted a token whose
principal is the application. The application must now list
client_credentials, the same declaration the device and workload grants
already require (appGrants). An undeclared app is refused 400
unauthorized_client (RFC 6749 §5.2), after client authentication, so a
caller that cannot authenticate learns nothing about the grant list.

Tests: an app with only authorization_code, and one with no grant list, are
refused unauthorized_client and write no token row, over post and Basic; a
wrong secret on such an app is still invalid_client; the same registration
with client_credentials declared mints. Machine fixtures now declare the
grant, as provision.yaml's `service` type does.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
2026-09-24 01:19:54 -07:00

164 lines
6.2 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
// The `orgs` claim (GAP C): a user token must carry the membership set — home org
// first, then every explicit team org — so a resource server can authorize an
// org-switch (X-Org-Id ∈ orgs) with no round-trip. Without it a multi-org identity
// silently collapses to home-org-only at the resource server. These tests drive the
// REAL mint path (authorization_code → issueTokens → the Signer) so the whole thread
// store→signer→claim is proven, not the Signer in isolation. A machine token
// (client_credentials, no user) carries only the memberships its application was
// granted — never an implied one.
import (
"context"
"net/url"
"testing"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// orgsOf indexes a decoded token's `orgs` claim by org slug → role, and reports the
// emission order, so a test asserts both the set/roles and that home comes first
// with no duplicate.
func orgsOf(t *testing.T, refs []schema.OrgRef) (byOrg map[string]string, order []string) {
t.Helper()
byOrg = map[string]string{}
for _, r := range refs {
if _, dup := byOrg[r.Org]; dup {
t.Fatalf("orgs claim emitted %q twice: %+v", r.Org, refs)
}
byOrg[r.Org] = r.Role
order = append(order, r.Org)
}
return byOrg, order
}
// A user with a home org AND explicit team memberships gets both in the access
// token AND the id_token: home first (role from HomeRole, not an explicit row),
// then the team org with its coarse role, deduped.
func TestOrgsClaim_UserCarriesHomeAndTeams(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw") // regular user hanzo/alice → home role member
ctx := context.Background()
// A redundant explicit home-org row (owner role) must NOT override the home
// entry; a real team membership must appear.
if _, err := store.EnsureMembership(ctx, db, "hanzo/alice", "hanzo", store.RoleOwner); err != nil {
t.Fatal(err)
}
if _, err := store.EnsureMembership(ctx, db, "hanzo/alice", "team-x", store.RoleAdmin); err != nil {
t.Fatal(err)
}
code, resp, body := loginForCode(t, app, loginParams("conf", "openid profile"))
if code == "" {
t.Fatalf("login did not mint a code: status=%d body=%s", resp.StatusCode, body)
}
_, tok := exchangeCode(t, app, url.Values{
"code": {code}, "client_id": {"conf"}, "client_secret": {"s3cret"}, "redirect_uri": {testRedirect},
})
for _, tt := range []struct {
name, token string
}{
{"access_token", tok["access_token"].(string)},
{"id_token", tok["id_token"].(string)},
} {
claims, err := verifyToken(ctx, db, tt.token)
if err != nil {
t.Fatalf("verify %s: %v", tt.name, err)
}
byOrg, order := orgsOf(t, claims.Orgs)
if len(order) == 0 || order[0] != "hanzo" {
t.Fatalf("%s orgs order = %v, want home org 'hanzo' first", tt.name, order)
}
if byOrg["hanzo"] != store.RoleMember {
t.Fatalf("%s home role = %q, want member (home wins over the explicit owner row)", tt.name, byOrg["hanzo"])
}
if byOrg["team-x"] != store.RoleAdmin {
t.Fatalf("%s team-x role = %q, want admin", tt.name, byOrg["team-x"])
}
if len(order) != 2 {
t.Fatalf("%s orgs = %+v, want exactly {hanzo, team-x}", tt.name, claims.Orgs)
}
}
}
// A user with no explicit membership still carries its home org — the invariant
// that a single-org identity does not lose its tenancy at cutover.
func TestOrgsClaim_HomeOnlyUser(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
seedUser(t, db, "alice", "[email protected]", "pw")
code, _, _ := loginForCode(t, app, loginParams("conf", "openid"))
_, tok := exchangeCode(t, app, url.Values{
"code": {code}, "client_id": {"conf"}, "client_secret": {"s3cret"}, "redirect_uri": {testRedirect},
})
claims, err := verifyToken(context.Background(), db, tok["access_token"].(string))
if err != nil {
t.Fatal(err)
}
if len(claims.Orgs) != 1 || claims.Orgs[0].Org != "hanzo" || claims.Orgs[0].Role != store.RoleMember {
t.Fatalf("home-only orgs = %+v, want [{hanzo member}]", claims.Orgs)
}
}
// A machine token whose application was granted no membership omits the `orgs`
// claim entirely — never an empty or app-org value.
func TestOrgsClaim_ClientCredentialsHasNone(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "svc", secret: "svc-secret", redirectURIs: []string{testRedirect}, grants: machineGrants})
resp, tok := postToken(t, app, url.Values{
"grant_type": {"client_credentials"}, "client_id": {"svc"}, "client_secret": {"svc-secret"}, "scope": {"read"},
})
if resp.StatusCode != 200 {
t.Fatalf("client_credentials status = %d, body = %v", resp.StatusCode, tok)
}
claims, err := verifyToken(context.Background(), db, tok["access_token"].(string))
if err != nil {
t.Fatal(err)
}
if len(claims.Orgs) != 0 {
t.Fatalf("machine token carried an orgs claim: %+v", claims.Orgs)
}
}
// A machine token carries exactly the memberships its application was granted,
// and never a reserved system org, however it was granted.
func TestOrgsClaim_ClientCredentialsCarriesGrants(t *testing.T) {
app, db := newServer(t)
seedApp(t, db, appOpts{clientID: "svc", secret: "svc-secret", redirectURIs: []string{testRedirect}, grants: machineGrants})
ctx := context.Background()
if _, err := store.EnsureMembership(ctx, db, "admin/svc", "acme", store.RoleMember); err != nil {
t.Fatal(err)
}
if _, err := store.EnsureMembership(ctx, db, "admin/svc", "admin", store.RoleAdmin); err != nil {
t.Fatal(err)
}
resp, tok := postToken(t, app, url.Values{
"grant_type": {"client_credentials"}, "client_id": {"svc"}, "client_secret": {"svc-secret"},
})
if resp.StatusCode != 200 {
t.Fatalf("client_credentials status = %d, body = %v", resp.StatusCode, tok)
}
claims, err := verifyToken(ctx, db, tok["access_token"].(string))
if err != nil {
t.Fatal(err)
}
if len(claims.Orgs) != 1 || claims.Orgs[0].Org != "acme" || claims.Orgs[0].Role != store.RoleMember {
t.Fatalf("orgs = %+v, want [{acme member}]", claims.Orgs)
}
if claims.Type != schema.Program {
t.Fatalf("type = %q, want %q", claims.Type, schema.Program)
}
}