Files
iam/internal/oidc/provision_refresh_test.go
zeekay e34828c260 oidc: founding an org at onboarding never moves the founder
POST /v1/iam/onboard and POST /v1/iam/admin/provision moved the caller into
the org they founded, re-keying them to <org>/<name>. Sign-in resolves an
address in the application's org, so the founder was then found by nobody: the
sign-in screen said the address had no account and offered a second one, and
hanzo.id answered "the username or password is incorrect". Founding the same
name at cloud's door and then here also moved the person and left their first
spelling on the roster as a second owner, holding the org's key.

Both endpoints now found an org beside the account: the org row with its
Founder stamp, the caller's owner membership, and <org>/default held by the
caller. The account, how it signs in and its sessions are unchanged, and the
org rides the next token's orgs claim. A replay converges on the same org and
key. A founder resumes an org only while nobody else owns it, so one who
handed it over and was removed does not take it back, and an account founds
at most store.MaxFoundedOrgs, as the membership surface holds it. An account
of the reserved admin org founds nothing. A signup at a founding application
still moves the account it just made into its personal org (Charter), the one
converge that sets claim.move.
2026-10-16 13:50:55 -07:00

67 lines
2.2 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"encoding/base64"
"encoding/json"
"net/http/httptest"
"strings"
"testing"
)
// A founder keeps the account they signed in with, so the refresh token they hold
// keeps renewing, and the token it mints carries the org they founded beside their
// home org. A founder whose session died here was told their org was not created
// when it was.
func TestProvision_FounderKeepsRenewing(t *testing.T) {
t.Setenv("IAM_SERVICE_TOKEN", "svc-secret-value")
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")
rt, _ := tok["refresh_token"].(string)
if rt == "" {
t.Fatalf("grant issued no refresh token: %v", tok)
}
req := httptest.NewRequest("POST", PathProvision, strings.NewReader(`{"owner":"hanzo","name":"alice","orgSlug":"acme"}`))
req.Host = "hanzo.id"
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer svc-secret-value")
if resp, body := do(t, app, req); resp.StatusCode != 200 {
t.Fatalf("provision: %d %s", resp.StatusCode, body)
}
status, out := refresh(t, app, "pub", rt, nil)
if status != 200 {
t.Fatalf("refresh after founding an org: %d %v, want 200", status, out)
}
at, _ := out["access_token"].(string)
parts := strings.Split(at, ".")
if len(parts) != 3 {
t.Fatalf("access token is not a JWT: %q", at)
}
raw, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
t.Fatal(err)
}
var claims struct {
Name string `json:"name"`
Orgs []struct {
Org string `json:"org"`
Role string `json:"role"`
} `json:"orgs"`
}
if err := json.Unmarshal(raw, &claims); err != nil {
t.Fatal(err)
}
// The home org leads `orgs`, and the founder owns acme.
if claims.Name != "alice" || len(claims.Orgs) != 2 || claims.Orgs[0].Org != "hanzo" ||
claims.Orgs[1].Org != "acme" || claims.Orgs[1].Role != "owner" {
t.Fatalf("renewed token names %s in %+v, want alice at home in hanzo, owner of acme", claims.Name, claims.Orgs)
}
}