mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
The session cookie is SameSite=Lax, which keeps it off requests from other sites but not from other hosts of the same site. Once hanzo.ai signs people in, any page on any *.hanzo.ai host could post a form to /v1/iam/login with type=device and approve an attacker's `hanzo login` code, or with type=code mint an authorization code, or edit the account. sessions.Forgeable is now the one rule for a write the cookie authorizes: Sec-Fetch-Site must be same-origin or none, and a request with a body must carry it as JSON. sessions.Writer resolves the session only past it, and every cookie write goes through it: the login SSO and device branches, callerOf/callerFrom (profile, password rotation, terms, consent, preferences, onboarding, link, unlink, passkey enrollment), and invitation accept. A refused write is 403 before the session is read. The passkey finish, which the challenge cookie authorizes, asks the same rule. A bearer from a client that is not a browser needs no fetch metadata, with or without a cookie riding along. Signing out ends sessions, so sessions.Clear asks the rule too, with one exception: a top-level navigation (Sec-Fetch-Mode navigate, Sec-Fetch-Dest document), which is how a relying party signs a person out. An image, a frame or a sibling's form ends nothing. The federation binding cookie is __Host-hanzo_fed and only that name is read; a test now pins that the bare hanzo_fed name is refused. server.RequireIssuer is the boot check for issuers: it installs IAM_ISSUER and IAM_ISSUER_MAP, then IAM_FEDERATION_ORIGIN_MAP, and returns an error for an empty or non-https IAM_ISSUER (outside IAM_DEV_HOST_RELATIVE=1), a malformed issuer map, or a sign-in map that would strand the binding. `iam serve` calls it, and a host that embeds IAM must call it before NewApp. devFallbackIssuer and the lazy env resolvers are gone: a process that never installed an issuer has none, so every issuer read is an error, discovery answers 503, no token is minted and none is accepted, where it used to answer https://hanzo.id. No issuer value changes. Co-authored-by: Hanzo Dev <[email protected]>
266 lines
10 KiB
Go
266 lines
10 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/iam/internal/sessions"
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// sessionCookieFor drives a bare (type=login) portal sign-in for hanzo/alice and
|
|
// returns the "name=value" of the session cookie it set — the credential the
|
|
// native session routes resolve the caller from.
|
|
func sessionCookieFor(t *testing.T, app *zip.App) string {
|
|
t.Helper()
|
|
form := url.Values{
|
|
"organization": {"hanzo"}, "application": {"conf"},
|
|
"username": {"alice"}, "password": {"pw"}, "type": {"login"},
|
|
}
|
|
resp, body := do(t, app, formReq("POST", PathLogin, form))
|
|
if resp.StatusCode != 200 || decode(t, body)["status"] != "ok" {
|
|
t.Fatalf("login failed: %s", body)
|
|
}
|
|
return cookieKV(resp.Header.Get("Set-Cookie"))
|
|
}
|
|
|
|
// signin exchanges an authorization code for a session and returns the caller's
|
|
// redacted account — the same envelope get-account returns, so the console's
|
|
// post<Account>('iam/signin') resolves the signed-in user in one call.
|
|
func TestSignin_CodeExchangeSetsSessionAndReturnsAccount(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
|
|
code, _, _ := loginForCode(t, app, map[string]string{
|
|
"organization": "hanzo", "application": "conf", "clientId": "conf",
|
|
"username": "alice", "password": "pw",
|
|
})
|
|
if code == "" {
|
|
t.Fatal("login (type=code) minted no code")
|
|
}
|
|
|
|
resp, body := do(t, app, formReqNoBody("POST", PathSignin+"?code="+code))
|
|
env := decode(t, body)
|
|
if resp.StatusCode != 200 || env["status"] != "ok" {
|
|
t.Fatalf("signin status=%d body=%s", resp.StatusCode, body)
|
|
}
|
|
if env["sub"] != "hanzo/alice" || env["name"] != "alice" {
|
|
t.Errorf("signin sub/name = %v/%v, want hanzo/alice / alice", env["sub"], env["name"])
|
|
}
|
|
data, _ := env["data"].(map[string]any)
|
|
if data["owner"] != "hanzo" {
|
|
t.Errorf("signin data.owner = %v, want hanzo", data["owner"])
|
|
}
|
|
if v, ok := data["passwordHash"]; ok && v != "" {
|
|
t.Errorf("signin leaked passwordHash")
|
|
}
|
|
// It establishes the durable session get-account resolves from.
|
|
cookie := resp.Header.Get("Set-Cookie")
|
|
if !strings.HasPrefix(cookie, sessions.CookieName+"=") {
|
|
t.Fatalf("signin did not set the session cookie: %q", cookie)
|
|
}
|
|
req := formReqNoBody("GET", PathAccount)
|
|
req.Header.Set("Cookie", cookieKV(cookie))
|
|
resp2, body2 := do(t, app, req)
|
|
if resp2.StatusCode != 200 || decode(t, body2)["status"] != "ok" {
|
|
t.Fatalf("get-account via the signin cookie failed: %s", body2)
|
|
}
|
|
}
|
|
|
|
// The code is single-use: a replay after redemption is refused (no second session).
|
|
func TestSignin_ReplayedCodeRejected(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
|
|
code, _, _ := loginForCode(t, app, map[string]string{
|
|
"organization": "hanzo", "application": "conf", "clientId": "conf",
|
|
"username": "alice", "password": "pw",
|
|
})
|
|
if _, body := do(t, app, formReqNoBody("POST", PathSignin+"?code="+code)); decode(t, body)["status"] != "ok" {
|
|
t.Fatalf("first signin should succeed: %s", body)
|
|
}
|
|
_, body := do(t, app, formReqNoBody("POST", PathSignin+"?code="+code))
|
|
if decode(t, body)["status"] != "error" {
|
|
t.Fatalf("replayed code must be refused, got: %s", body)
|
|
}
|
|
}
|
|
|
|
// whoami resolves the caller from the session cookie and returns the lightweight
|
|
// identity; an anonymous caller gets {status:"error"}, never a leak.
|
|
func TestWhoami_CookieResolvesIdentity(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
cookie := sessionCookieFor(t, app)
|
|
|
|
req := formReqNoBody("GET", PathWhoami)
|
|
req.Header.Set("Cookie", cookie)
|
|
resp, body := do(t, app, req)
|
|
env := decode(t, body)
|
|
if resp.StatusCode != 200 || env["status"] != "ok" || env["sub"] != "hanzo/alice" {
|
|
t.Fatalf("whoami via cookie: status=%d body=%s", resp.StatusCode, body)
|
|
}
|
|
data, _ := env["data"].(map[string]any)
|
|
if data["owner"] != "hanzo" || data["name"] != "alice" || data["id"] != "hanzo/alice" {
|
|
t.Errorf("whoami identity = %v, want owner/name/id = hanzo/alice/hanzo/alice", data)
|
|
}
|
|
|
|
// Anonymous → error, no data.
|
|
_, anon := do(t, app, formReqNoBody("GET", PathWhoami))
|
|
if e := decode(t, anon); e["status"] != "error" || e["data"] != nil {
|
|
t.Fatalf("anonymous whoami must be error with no data: %s", anon)
|
|
}
|
|
}
|
|
|
|
// update-preferences shallow-merges: a second patch adds a key without clobbering
|
|
// the first, and the merged object is returned + persisted on the caller's row.
|
|
func TestUpdatePreferences_ShallowMergeRoundTrip(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
cookie := sessionCookieFor(t, app)
|
|
|
|
post := func(patch any) map[string]any {
|
|
req := fromPage(jsonReq("POST", PathPreferences, patch))
|
|
req.Header.Set("Cookie", cookie)
|
|
resp, body := do(t, app, req)
|
|
env := decode(t, body)
|
|
if resp.StatusCode != 200 || env["status"] != "ok" {
|
|
t.Fatalf("update-preferences: status=%d body=%s", resp.StatusCode, body)
|
|
}
|
|
data, _ := env["data"].(map[string]any)
|
|
return data
|
|
}
|
|
|
|
if got := post(map[string]any{"onboarding_completed": true}); got["onboarding_completed"] != true {
|
|
t.Fatalf("first patch not reflected: %v", got)
|
|
}
|
|
got := post(map[string]any{"theme": "dark"})
|
|
if got["onboarding_completed"] != true || got["theme"] != "dark" {
|
|
t.Fatalf("second patch clobbered the first (want both keys): %v", got)
|
|
}
|
|
|
|
// Persisted on the row — a fresh read shows the merged blob under hanzo.preferences.
|
|
u, _ := store.GetUserByName(context.Background(), db, "hanzo", "alice")
|
|
if u == nil || !strings.Contains(u.Properties[preferencesKey], "onboarding_completed") ||
|
|
!strings.Contains(u.Properties[preferencesKey], "theme") {
|
|
t.Fatalf("preferences not persisted: %+v", u.Properties)
|
|
}
|
|
}
|
|
|
|
// onboard creates the named org and makes the caller its owner by membership. The
|
|
// caller's account stays where it is: hanzo/alice before and after, not admin of
|
|
// anything new by the row, owner of acme-inc on its roster.
|
|
func TestOnboard_CreatesOrgTheCallerOwns(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
cookie := sessionCookieFor(t, app)
|
|
|
|
req := fromPage(jsonReq("POST", PathOnboard, map[string]any{"name": "Acme Inc"}))
|
|
req.Header.Set("Cookie", cookie)
|
|
resp, body := do(t, app, req)
|
|
env := decode(t, body)
|
|
if resp.StatusCode != 200 || env["org"] != "acme-inc" {
|
|
t.Fatalf("onboard status=%d body=%s (want {org:acme-inc})", resp.StatusCode, body)
|
|
}
|
|
|
|
ctx := context.Background()
|
|
if org, _ := store.GetOrganizationByName(ctx, db, "acme-inc"); org == nil || org.Owner != "admin" {
|
|
t.Fatalf("onboard did not create the acme-inc org: %+v", org)
|
|
}
|
|
if u, _ := store.GetUserByName(ctx, db, "hanzo", "alice"); u == nil {
|
|
t.Fatal("alice is no longer hanzo/alice; onboarding moves nobody")
|
|
}
|
|
if moved, _ := store.GetUserByName(ctx, db, "acme-inc", "alice"); moved != nil {
|
|
t.Fatalf("alice was moved into acme-inc: %+v", moved)
|
|
}
|
|
if m, err := store.GetMembership(ctx, db, "hanzo/alice", "acme-inc"); err != nil || m == nil || m.Role != store.RoleOwner {
|
|
t.Fatalf("alice's membership of acme-inc = %+v (%v), want owner", m, err)
|
|
}
|
|
}
|
|
|
|
// The one-click personal path creates a `<username>` org.
|
|
func TestOnboard_PersonalOrg(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
cookie := sessionCookieFor(t, app)
|
|
|
|
req := fromPage(jsonReq("POST", PathOnboard, map[string]any{"personal": true}))
|
|
req.Header.Set("Cookie", cookie)
|
|
resp, body := do(t, app, req)
|
|
if env := decode(t, body); resp.StatusCode != 200 || env["org"] != "alice" {
|
|
t.Fatalf("personal onboard status=%d body=%s (want {org:alice})", resp.StatusCode, body)
|
|
}
|
|
if org, _ := store.GetOrganizationByName(context.Background(), db, "alice"); org == nil || !org.IsPersonal {
|
|
t.Fatalf("personal org not created/flagged: %+v", org)
|
|
}
|
|
}
|
|
|
|
// A reserved slug (an IAM system owner) is refused — a customer can never become admin.
|
|
func TestOnboard_ReservedRefused(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
cookie := sessionCookieFor(t, app)
|
|
|
|
req := fromPage(jsonReq("POST", PathOnboard, map[string]any{"name": "admin"}))
|
|
req.Header.Set("Cookie", cookie)
|
|
resp, body := do(t, app, req)
|
|
if resp.StatusCode == 200 || !strings.Contains(string(body), "reserved") {
|
|
t.Fatalf("reserved org must be refused: status=%d body=%s", resp.StatusCode, body)
|
|
}
|
|
}
|
|
|
|
// linked-accounts returns the caller's non-empty connector columns and nothing else.
|
|
func TestLinkedAccounts_ListsConnectorColumns(t *testing.T) {
|
|
app, db := newServer(t)
|
|
seedApp(t, db, appOpts{clientID: "conf", secret: "s3cret", redirectURIs: []string{testRedirect}})
|
|
seedRichUser(t, db)
|
|
// Link a GitHub identity to alice.
|
|
u, _ := store.GetUserByName(context.Background(), db, "hanzo", "alice")
|
|
u.GitHub = "octocat"
|
|
if err := u.UpdateCtx(context.Background()); err != nil {
|
|
t.Fatalf("link github: %v", err)
|
|
}
|
|
cookie := sessionCookieFor(t, app)
|
|
|
|
req := formReqNoBody("GET", PathLinkedAccounts)
|
|
req.Header.Set("Cookie", cookie)
|
|
resp, body := do(t, app, req)
|
|
if resp.StatusCode != 200 || !strings.Contains(string(body), "github") || !strings.Contains(string(body), "octocat") {
|
|
t.Fatalf("linked-accounts: status=%d body=%s", resp.StatusCode, body)
|
|
}
|
|
}
|
|
|
|
// linkedAccountsOf reflects only the connector columns — never Owner/Name/Email.
|
|
func TestLinkedAccountsOf_OnlyConnectors(t *testing.T) {
|
|
u := &schema.User{Owner: "hanzo", Name: "alice", Email: "[email protected]", GitHub: "octocat", Google: "g-1"}
|
|
got := linkedAccountsOf(u)
|
|
seen := map[string]string{}
|
|
for _, la := range got {
|
|
seen[la.Provider] = la.Subject
|
|
}
|
|
if seen["github"] != "octocat" || seen["google"] != "g-1" {
|
|
t.Fatalf("missing a linked connector: %v", got)
|
|
}
|
|
for _, forbidden := range []string{"owner", "name", "email"} {
|
|
if _, bad := seen[forbidden]; bad {
|
|
t.Errorf("linkedAccountsOf leaked a non-connector field %q", forbidden)
|
|
}
|
|
}
|
|
if len(got) != 2 {
|
|
t.Errorf("want exactly 2 linked accounts, got %d: %v", len(got), got)
|
|
}
|
|
}
|