mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
`owner` says where an identity is anchored — its billing, its default scope. Being an operator is membership of the reserved org, held alongside an ordinary home org, so a relying party reading `owner` alone concludes that an operator anchored in a brand org has no platform authority. That makes the reserved org unreachable in practice while every check still reads correctly, which is the same conflation authz.Claims.PlatformSudo already abandoned on the token side. userinfo now emits `orgs`, the same value and shape the access token carries, so a client that reads one and a client that reads the other cannot reach two different answers about the same person. It is read from the store rather than echoed from the token, for the reason isAdmin is: a grant revoked a minute ago stops counting now rather than when the token lapses. Co-authored-by: Hanzo Dev <[email protected]>
312 lines
11 KiB
Go
312 lines
11 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"testing"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"github.com/hanzoai/orm"
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// seedRichUser creates alice with the profile fields userinfo projects.
|
|
func seedRichUser(t *testing.T, db orm.DB) {
|
|
t.Helper()
|
|
hash, err := bcrypt.GenerateFromPassword([]byte("pw"), bcrypt.MinCost)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u := orm.New[schema.User](db)
|
|
u.Owner = "hanzo"
|
|
u.Name = "alice"
|
|
u.Email = "[email protected]"
|
|
u.EmailVerified = true
|
|
u.DisplayName = "Alice Example"
|
|
u.Phone = "+15551234567"
|
|
u.Location = "San Francisco"
|
|
u.PasswordHash = string(hash)
|
|
u.PasswordType = "bcrypt"
|
|
u.SetId("hanzo/alice")
|
|
if err := u.CreateCtx(context.Background()); err != nil {
|
|
t.Fatalf("seed rich user: %v", err)
|
|
}
|
|
}
|
|
|
|
// accessTokenFor runs the confidential flow and returns the access token.
|
|
func accessTokenFor(t *testing.T, app *zip.App, scope string) string {
|
|
t.Helper()
|
|
code, _, _ := loginForCode(t, app, loginParams("conf", scope))
|
|
_, tok := exchangeCode(t, app, url.Values{
|
|
"code": {code}, "client_id": {"conf"}, "client_secret": {"s3cret"}, "redirect_uri": {testRedirect},
|
|
})
|
|
access, _ := tok["access_token"].(string)
|
|
if access == "" {
|
|
t.Fatal("no access token issued")
|
|
}
|
|
return access
|
|
}
|
|
|
|
func userinfo(t *testing.T, app *zip.App, bearer string) (int, map[string]any) {
|
|
t.Helper()
|
|
req := formReqNoBody("GET", PathUserInfo)
|
|
if bearer != "" {
|
|
req.Header.Set("Authorization", "Bearer "+bearer)
|
|
}
|
|
resp, body := do(t, app, req)
|
|
return resp.StatusCode, decode(t, body)
|
|
}
|
|
|
|
// userinfo returns exactly the claims the granted scopes authorize.
|
|
func TestUserinfo_ClaimsByScope(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
|
|
access := accessTokenFor(t, app, "openid profile email phone address")
|
|
status, info := userinfo(t, app, access)
|
|
if status != 200 {
|
|
t.Fatalf("userinfo status = %d, body %v", status, info)
|
|
}
|
|
want := map[string]any{
|
|
"sub": "hanzo/alice",
|
|
"iss": "https://hanzo.id",
|
|
"aud": "conf",
|
|
"owner": "hanzo",
|
|
"organization": "hanzo",
|
|
// `name` is the USERNAME, the same answer the access token gives: UserInfo and
|
|
// the token describe one principal, so they must not name it two ways. The
|
|
// human's name rides in displayName.
|
|
"preferred_username": "alice",
|
|
"name": "alice",
|
|
"displayName": "Alice Example",
|
|
"email": "[email protected]",
|
|
"email_verified": true,
|
|
"phone": "+15551234567",
|
|
"address": "San Francisco",
|
|
}
|
|
for k, v := range want {
|
|
if info[k] != v {
|
|
t.Errorf("userinfo[%q] = %v, want %v", k, info[k], v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// UserInfo carries the get-account security contract: isAdmin (the gateway
|
|
// admin-guard's SuperAdmin-predicate input, with owner==adminOrg) + type, from the
|
|
// loaded user record, so UserInfo is a drop-in for the retired get-account. isAdmin
|
|
// is present regardless of scope (identity, not profile).
|
|
func TestUserinfo_AdminGuardContract(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
// Promote the seeded user to an admin with a concrete type.
|
|
u, err := orm.Get[schema.User](db, "hanzo/alice")
|
|
if err != nil {
|
|
t.Fatalf("load: %v", err)
|
|
}
|
|
u.IsAdmin = true
|
|
u.Type = "normal-user"
|
|
if err := u.UpdateCtx(context.Background()); err != nil {
|
|
t.Fatalf("promote: %v", err)
|
|
}
|
|
|
|
// Even a minimal (openid-only) scope carries the identity claims.
|
|
access := accessTokenFor(t, app, "openid")
|
|
status, info := userinfo(t, app, access)
|
|
if status != 200 {
|
|
t.Fatalf("status %d: %v", status, info)
|
|
}
|
|
if info["isAdmin"] != true {
|
|
t.Errorf("userinfo[isAdmin] = %v, want true (the admin-guard SuperAdmin input)", info["isAdmin"])
|
|
}
|
|
if info["type"] != "normal-user" {
|
|
t.Errorf("userinfo[type] = %v, want normal-user", info["type"])
|
|
}
|
|
if info["owner"] != "hanzo" {
|
|
t.Errorf("userinfo[owner] = %v, want hanzo", info["owner"])
|
|
}
|
|
}
|
|
|
|
// A non-admin's userinfo carries isAdmin:false (never absent — the admin-guard
|
|
// must be able to read a definite false, not infer it from a missing key).
|
|
func TestUserinfo_NonAdminIsExplicitFalse(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db) // alice, IsAdmin=false
|
|
_, info := userinfo(t, app, accessTokenFor(t, app, "openid"))
|
|
if v, ok := info["isAdmin"]; !ok || v != false {
|
|
t.Errorf("userinfo[isAdmin] = %v (present=%v), want an explicit false", v, ok)
|
|
}
|
|
}
|
|
|
|
// A narrow scope yields only the identifiers — no profile/email leakage.
|
|
func TestUserinfo_ScopeGating(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
|
|
access := accessTokenFor(t, app, "openid")
|
|
status, info := userinfo(t, app, access)
|
|
if status != 200 {
|
|
t.Fatalf("status %d", status)
|
|
}
|
|
for _, leaked := range []string{"email", "preferred_username", "name", "phone", "address"} {
|
|
if _, ok := info[leaked]; ok {
|
|
t.Errorf("scope=openid must not expose %q (got %v)", leaked, info[leaked])
|
|
}
|
|
}
|
|
if info["sub"] != "hanzo/alice" {
|
|
t.Errorf("sub missing: %v", info["sub"])
|
|
}
|
|
}
|
|
|
|
// No/invalid bearer → 401 invalid_token with the Bearer challenge.
|
|
func TestUserinfo_Unauthorized(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
|
|
t.Run("no bearer", func(t *testing.T) {
|
|
req := formReqNoBody("GET", PathUserInfo)
|
|
resp, body := do(t, app, req)
|
|
if resp.StatusCode != 401 || decode(t, body)["error"] != "invalid_token" {
|
|
t.Fatalf("status=%d body=%s", resp.StatusCode, body)
|
|
}
|
|
if resp.Header.Get("WWW-Authenticate") == "" {
|
|
t.Error("401 must carry WWW-Authenticate")
|
|
}
|
|
})
|
|
|
|
t.Run("garbage bearer", func(t *testing.T) {
|
|
status, info := userinfo(t, app, "not.a.jwt")
|
|
if status != 401 || info["error"] != "invalid_token" {
|
|
t.Fatalf("status=%d err=%v", status, info["error"])
|
|
}
|
|
})
|
|
}
|
|
|
|
// The store keeps only token hashes — never the reusable plaintext bearer or
|
|
// refresh token — so a database dump exposes no usable credential.
|
|
func TestTokens_StoredAsHashesOnly(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "pub", redirectURIs: []string{testRedirect}, refreshHours: 24})
|
|
seedUser(t, db, "alice", "[email protected]", "pw")
|
|
|
|
tok := grantViaPKCE(t, app, "pub", "openid offline_access")
|
|
access := tok["access_token"].(string)
|
|
refresh := tok["refresh_token"].(string)
|
|
|
|
row, err := store.GetTokenByAccessTokenHash(context.Background(), db, hashToken(access))
|
|
if err != nil || row == nil {
|
|
t.Fatalf("locate token row: %v (nil=%v)", err, row == nil)
|
|
}
|
|
if row.AccessToken != "" || row.RefreshToken != "" {
|
|
t.Fatalf("plaintext tokens must not be persisted: access=%q refresh=%q", row.AccessToken, row.RefreshToken)
|
|
}
|
|
if row.AccessTokenHash != hashToken(access) || row.RefreshTokenHash != hashToken(refresh) {
|
|
t.Fatal("token hashes must be persisted for lookup")
|
|
}
|
|
}
|
|
|
|
// Deleting the token row revokes the bearer even though the JWT itself is still
|
|
// within its lifetime — userinfo looks the grant up by hash first.
|
|
func TestUserinfo_RevokedTokenRejected(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
|
|
access := accessTokenFor(t, app, "openid profile")
|
|
if status, _ := userinfo(t, app, access); status != 200 {
|
|
t.Fatalf("token should work before revocation: %d", status)
|
|
}
|
|
|
|
// Revoke: delete the stored grant.
|
|
row, err := store.GetTokenByAccessTokenHash(context.Background(), db, hashToken(access))
|
|
if err != nil || row == nil {
|
|
t.Fatalf("locate token row: %v (nil=%v)", err, row == nil)
|
|
}
|
|
if err := store.DeleteToken(context.Background(), db, row); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if status, info := userinfo(t, app, access); status != 401 || info["error"] != "invalid_token" {
|
|
t.Fatalf("revoked token: status=%d err=%v, want 401 invalid_token", status, info["error"])
|
|
}
|
|
}
|
|
|
|
// An operator is someone an existing operator put IN the reserved org, and that
|
|
// grant sits ALONGSIDE an ordinary home org — it does not replace it. So userinfo
|
|
// must carry the membership set, not just the anchor: a relying party reading
|
|
// `owner` alone sees "hanzo" here and concludes this person has no platform
|
|
// authority, which is how the reserved org becomes unreachable in practice while
|
|
// every check still looks correct.
|
|
func TestUserinfo_OperatorAnchoredInABrandOrg(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db) // alice, home org "hanzo"
|
|
if _, err := store.EnsureMembership(context.Background(), db, "hanzo/alice", "admin", "admin"); err != nil {
|
|
t.Fatalf("grant admin-org membership: %v", err)
|
|
}
|
|
|
|
// openid alone: this is identity, not profile, so no scope may withhold it.
|
|
status, info := userinfo(t, app, accessTokenFor(t, app, "openid"))
|
|
if status != 200 {
|
|
t.Fatalf("status %d: %v", status, info)
|
|
}
|
|
|
|
raw, ok := info["orgs"].([]any)
|
|
if !ok {
|
|
t.Fatalf("userinfo[orgs] = %#v, want the membership set", info["orgs"])
|
|
}
|
|
at := -1
|
|
for i, m := range raw {
|
|
if e, _ := m.(map[string]any); e != nil && e["org"] == "admin" {
|
|
at = i
|
|
if e["role"] != "admin" {
|
|
t.Errorf("admin-org role = %v, want admin", e["role"])
|
|
}
|
|
}
|
|
}
|
|
if at < 0 {
|
|
t.Fatalf("userinfo[orgs] = %#v, want an entry for the reserved org", raw)
|
|
}
|
|
|
|
// The two halves of the regression. Home is still the brand org, and the grant
|
|
// is NOT at index 0 — so reading either the anchor or the first entry misses it.
|
|
if info["owner"] != "hanzo" {
|
|
t.Errorf("userinfo[owner] = %v, want hanzo (the anchor is unchanged by the grant)", info["owner"])
|
|
}
|
|
if at == 0 {
|
|
t.Fatal("the reserved org landed at index 0; this test no longer covers the operator whose anchor is a brand org")
|
|
}
|
|
}
|
|
|
|
// A user with no grants still carries their home org, so a consumer can always
|
|
// read the membership set rather than branching on whether the key exists.
|
|
func TestUserinfo_OrgsCarriesTheHomeOrgAlone(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
|
|
_, info := userinfo(t, app, accessTokenFor(t, app, "openid"))
|
|
raw, ok := info["orgs"].([]any)
|
|
if !ok || len(raw) != 1 {
|
|
t.Fatalf("userinfo[orgs] = %#v, want exactly the home org", info["orgs"])
|
|
}
|
|
if e, _ := raw[0].(map[string]any); e == nil || e["org"] != "hanzo" {
|
|
t.Errorf("userinfo[orgs][0] = %#v, want hanzo", raw[0])
|
|
}
|
|
for _, m := range raw {
|
|
if e, _ := m.(map[string]any); e != nil && e["org"] == "admin" {
|
|
t.Fatal("a user with no grant carries the reserved org")
|
|
}
|
|
}
|
|
}
|