Files
iam/internal/principal/scope_test.go
zeekay 2bbb0db654 A brand org is no tenant of its plain members, and a key dies with its holder
A key resolves only while its holder may sign in: a suspended or deleted
account's keys answer key_holder_disabled, its org's default key included. An
org the platform runs (authz.IsPlatformOrg) is a tenant only of its owners and
admins, its own machines and a SuperAdmin, and a brand's directory of no one:
store.Acts is that rule asked of one row, and the key resolver
(key_not_tenant), the user-key mint (403, no key filed that acts for no one) and
the delegation read it. principal.Scope and ScopeRead require MemberOf, the org
listing filters by it, and the registry gives a person a token only as an admin
of a candidate org.

A delegated token (as(), naming its actor in act) founds no org and writes no
account. A suspended account resolves nobody on its session or its bearer, and
founds nothing. An act key names its holder and mints only while that holder
runs the key's org. A machine signs in as an application or with its key: it is
refused a password grant, a form sign-in and an invitation. The groups claim
carries the orgs a person is a tenant of, so a relying party that maps groups
onto access sees no directory and no brand org held as a plain member. A name a
removed org still holds draws the answer a standing one does.

authz v1.10.47.
2026-10-17 04:07:47 -07:00

45 lines
1.5 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package principal_test
import (
"context"
"testing"
policy "github.com/hanzoai/authz"
"github.com/hanzoai/iam/internal/principal"
)
// Scope is what a write is bound to. An org the caller administers by
// membership is theirs to name; an org they merely belong to is not, and neither
// is one they have nothing to do with. Belonging opens reads (ScopeRead), never
// writes.
func TestScopeNamesOnlyAnOrgTheCallerAdministers(t *testing.T) {
sam := &principal.Principal{Org: "sam", User: "sam", Admin: true,
Orgs: map[string]policy.Role{"webby": policy.Owner, "beta": policy.Member}}
ctx := principal.Bind(context.Background(), sam)
for _, c := range []struct {
asked, want string
ok bool
}{
{"", "sam", true}, // naming none is the caller's own
{"sam", "sam", true}, // its own, by name
{"webby", "webby", true}, // an org it owns by membership
{"beta", "", false}, // an org it merely belongs to
{"acme", "", false}, // an org it has nothing to do with
{"admin", "", false}, // the reserved org
} {
got, err := principal.Scope(ctx, c.asked)
if (err == nil) != c.ok || got != c.want {
t.Errorf("Scope(%q) = %q, %v; want %q, ok=%v", c.asked, got, err, c.want, c.ok)
}
}
// ScopeRead still opens what belonging opens.
if got, err := principal.ScopeRead(ctx, "beta"); err != nil || got != "beta" {
t.Errorf("ScopeRead(lux) = %q, %v; a member reads the org they belong to", got, err)
}
}