mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-21 03:13:26 +00:00
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.
89 lines
3.5 KiB
Go
89 lines
3.5 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
package authz
|
|
|
|
import (
|
|
"testing"
|
|
|
|
policy "github.com/hanzoai/authz"
|
|
|
|
"github.com/hanzoai/iam/internal/principal"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// ONE PERSON, MANY ORGS.
|
|
//
|
|
// A human's account lives in one IAM tenant; the organizations they work in are
|
|
// a SET. The policy used to answer "may you read this org?" with "is this org
|
|
// your account's owner?", so an org's own member — its own ADMIN — could not
|
|
// read the org they belong to. Every console that renders a tenant reads that
|
|
// row for its name and logo, so a second org was invisible: the picker fell back
|
|
// to the signed-in person, and the org mark showed a personal monogram.
|
|
func TestOrgReadFollowsMembershipNotTheAccountOwner(t *testing.T) {
|
|
// A person whose account lives in `hanzo`, who also belongs to `maxpower`.
|
|
dave := &principal.Principal{
|
|
Org: "beta", User: "davelorenzini",
|
|
Orgs: map[string]policy.Role{"maxpower": store.RoleAdmin},
|
|
}
|
|
// A person in `hanzo` with no other membership.
|
|
stranger := &principal.Principal{Org: "beta", User: "nobody"}
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
p *principal.Principal
|
|
method string
|
|
org string
|
|
want bool
|
|
}{
|
|
{"member reads the org they belong to", dave, "GET", "maxpower", true},
|
|
{"member reads their home org", dave, "GET", "beta", true},
|
|
{"org admin edits the org they administer", dave, "POST", "maxpower", true},
|
|
{"a non-member cannot read that org", stranger, "GET", "maxpower", false},
|
|
{"a non-member cannot edit that org", stranger, "POST", "maxpower", false},
|
|
{"home-org membership alone does not grant editing", stranger, "POST", "beta", false},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := authorize(tc.p, tc.method, "organizations", "admin", tc.org); got != tc.want {
|
|
t.Errorf("authorize(%s %s) = %v, want %v", tc.method, tc.org, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Belonging is permission to SEE, never to edit. A plain member of an org may
|
|
// read it and nothing more — otherwise inviting someone to a workspace would
|
|
// hand them its settings.
|
|
func TestPlainMemberReadsButCannotWrite(t *testing.T) {
|
|
guest := &principal.Principal{
|
|
Org: "hanzo", User: "guest",
|
|
Orgs: map[string]policy.Role{"maxpower": store.RoleMember},
|
|
}
|
|
if !authorize(guest, "GET", "organizations", "admin", "maxpower") {
|
|
t.Error("a member must be able to read the org they belong to")
|
|
}
|
|
if authorize(guest, "POST", "organizations", "admin", "maxpower") {
|
|
t.Error("a plain member must NOT be able to write the org they belong to")
|
|
}
|
|
}
|
|
|
|
// An owner or admin membership makes its holder that org's admin, over the rows
|
|
// that org owns, exactly as an admin whose account lives there. It reaches no
|
|
// other owner, and a plain membership reaches no row at all: belonging says which
|
|
// orgs you act in, not that you may reach their users or signing material.
|
|
func TestMembershipAdministersOnlyTheOrgItAdmins(t *testing.T) {
|
|
dave := &principal.Principal{
|
|
Org: "hanzo", User: "davelorenzini",
|
|
Orgs: map[string]policy.Role{"maxpower": store.RoleAdmin, "lux": store.RoleMember},
|
|
}
|
|
for _, entity := range []string{"users", "certs", "applications", "providers"} {
|
|
if !authorize(dave, "GET", entity, "maxpower", "anything") {
|
|
t.Errorf("an admin of maxpower cannot read its %s", entity)
|
|
}
|
|
for _, owner := range []string{"lux", "acme", "admin"} {
|
|
if authorize(dave, "GET", entity, owner, "anything") || authorize(dave, "POST", entity, owner, "anything") {
|
|
t.Errorf("membership reached %s owned by %s", entity, owner)
|
|
}
|
|
}
|
|
}
|
|
}
|