KEY-WP-0005-T02-T03: cached tenant_roles claim, close workplan
All checks were successful
Build and Publish Container Image / build-and-push (push) Successful in 1m21s
All checks were successful
Build and Publish Container Image / build-and-push (push) Successful in 1m21s
New internal/adapters/tenantengine package, mirroring
internal/adapters/{lldap,privacyidea,authelia}'s shape: Client.Roles()
calls tenant-engine's cache-read endpoint. Fails open by construction --
unreachable, non-200, malformed body, or a nil *Client all return
(nil, false), never an error to specially handle. Wired into
TokenHandler.TenantEngine (nil by default, existing tests unaffected);
token.go stamps tenant_roles only when ok.
7 adapter tests plus 3 TokenHandler-level tests proving the actual
required behavior end-to-end: present when reachable, token issuance still
200 with every other core claim intact when unreachable (tenant_roles
simply absent -- the literal done-criteria), absent when not configured.
Real bug found and fixed at the source, not worked around: the first live
cross-process check (real flex-auth, real tenant-engine, this adapter)
returned tenant_not_found for a tenant that existed -- tenant-engine's read
endpoint was keyed by its internal tenant_id, but key-cape only ever has
the tenant's profile identifier. Fixed in tenant-engine
(ADHOC-2026-07-24), re-verified with the same live three-process chain --
roles=[IAM] ok=true.
Workplan closed: T01-T03 done. Explicitly still open: client_credentials /
service-token issuance -- no such flow exists in token.go at all, a
materially larger separate piece of work than either task here.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
fb888579dc
commit
44da5f5f99
5 changed files with 375 additions and 4 deletions
|
|
@ -11,6 +11,7 @@ import (
|
|||
"strings"
|
||||
"time"
|
||||
|
||||
"keycape/internal/adapters/tenantengine"
|
||||
"keycape/internal/domain"
|
||||
profileerrors "keycape/internal/errors"
|
||||
"keycape/internal/server/telemetry"
|
||||
|
|
@ -25,6 +26,9 @@ type TokenHandler struct {
|
|||
Issuer string
|
||||
TokenLifetime time.Duration
|
||||
Emitter telemetry.Emitter
|
||||
// TenantEngine sources the optional tenant_roles claim (KEY-WP-0005-T02).
|
||||
// Nil disables it entirely -- token issuance never depends on it.
|
||||
TenantEngine *tenantengine.Client
|
||||
}
|
||||
|
||||
// tokenResponse is the JSON body returned on a successful token exchange.
|
||||
|
|
@ -130,12 +134,20 @@ func (h *TokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
// Core claims required by net-kingdom/canon/standards/iam-profile_v0.3.md
|
||||
// for every production token -- not scope-gated, unlike the recommended
|
||||
// human claims above (KEY-WP-0005-T01).
|
||||
claims["tenant"] = effectiveTenant(user)
|
||||
tenant := effectiveTenant(user)
|
||||
claims["tenant"] = tenant
|
||||
claims["principal_type"] = "human"
|
||||
claims["groups"] = nonNilStrings(user.Groups)
|
||||
claims["roles"] = nonNilStrings(user.Roles)
|
||||
claims["assurance"] = assuranceClaim(sess.MFAVerified, now)
|
||||
|
||||
// Optional cached tenant_roles claim (KEY-WP-0005-T02). Fails open --
|
||||
// see internal/adapters/tenantengine's package doc for why this is the
|
||||
// one place in the whole tenant_roles design where that's correct.
|
||||
if roles, ok := h.TenantEngine.Roles(ctx, tenant); ok {
|
||||
claims["tenant_roles"] = roles
|
||||
}
|
||||
|
||||
// 7. Sign JWT with RSA-SHA256.
|
||||
kid := "key-1" // static kid for v0.1
|
||||
jwtToken, err := buildJWT(claims, kid, h.SigningKey)
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"keycape/internal/adapters/tenantengine"
|
||||
"keycape/internal/domain"
|
||||
profileerrors "keycape/internal/errors"
|
||||
"keycape/internal/server/oidc"
|
||||
|
|
@ -507,3 +508,117 @@ func TestTokenHandler_CodeDeletedAfterUse(t *testing.T) {
|
|||
t.Errorf("second use: expected 400 (code replay), got %d", w2.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// KEY-WP-0005-T02: cached tenant_roles claim
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func TestTokenHandler_TenantRoles_PresentWhenTenantEngineReachable(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write([]byte(`{"tenant_id":"tenant:coulomb","roles":["IAM","VEN"]}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
sessions := oidc.NewSessionStore()
|
||||
users := &mockUserRepo{users: map[string]*domain.User{"alice": aliceUser()}}
|
||||
h, _ := newTokenHandler(t, sessions, users)
|
||||
h.TenantEngine = tenantengine.New(server.URL, nil)
|
||||
|
||||
verifier := "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
|
||||
code := seededSession(sessions, verifier)
|
||||
|
||||
params := url.Values{
|
||||
"grant_type": []string{"authorization_code"},
|
||||
"code": []string{code},
|
||||
"client_id": []string{"test-client"},
|
||||
"redirect_uri": []string{"https://app.example.com/callback"},
|
||||
"code_verifier": []string{verifier},
|
||||
}
|
||||
req := tokenRequest(params)
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d (body: %s)", w.Code, w.Body.String())
|
||||
}
|
||||
resp := decodeTokenResponse(t, w.Body.String())
|
||||
claims := parseJWTPayload(t, resp["id_token"].(string))
|
||||
|
||||
roles, ok := claims["tenant_roles"].([]interface{})
|
||||
if !ok || len(roles) != 2 || roles[0] != "IAM" || roles[1] != "VEN" {
|
||||
t.Errorf("tenant_roles: want [IAM VEN], got %v", claims["tenant_roles"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenHandler_TenantRoles_OmittedButIssuanceSucceedsWhenUnreachable(t *testing.T) {
|
||||
sessions := oidc.NewSessionStore()
|
||||
users := &mockUserRepo{users: map[string]*domain.User{"alice": aliceUser()}}
|
||||
h, _ := newTokenHandler(t, sessions, users)
|
||||
// Unreachable: nothing listens on this port.
|
||||
h.TenantEngine = tenantengine.New("http://127.0.0.1:1", nil)
|
||||
|
||||
verifier := "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
|
||||
code := seededSession(sessions, verifier)
|
||||
|
||||
params := url.Values{
|
||||
"grant_type": []string{"authorization_code"},
|
||||
"code": []string{code},
|
||||
"client_id": []string{"test-client"},
|
||||
"redirect_uri": []string{"https://app.example.com/callback"},
|
||||
"code_verifier": []string{verifier},
|
||||
}
|
||||
req := tokenRequest(params)
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
// Token issuance itself must not fail just because the cache source is
|
||||
// down -- this is the literal point of KEY-WP-0005-T02's fail-open rule.
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200 even with tenant-engine unreachable, got %d (body: %s)", w.Code, w.Body.String())
|
||||
}
|
||||
resp := decodeTokenResponse(t, w.Body.String())
|
||||
claims := parseJWTPayload(t, resp["id_token"].(string))
|
||||
|
||||
if _, present := claims["tenant_roles"]; present {
|
||||
t.Errorf("tenant_roles must be omitted (not null, not []), got %v", claims["tenant_roles"])
|
||||
}
|
||||
// Every other core claim must still be present -- one missing optional
|
||||
// claim must not cascade into missing required ones.
|
||||
for _, c := range []string{"tenant", "principal_type", "groups", "roles", "assurance"} {
|
||||
if _, ok := claims[c]; !ok {
|
||||
t.Errorf("required claim %q missing even though only tenant_roles should be affected", c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTokenHandler_TenantRoles_OmittedWhenTenantEngineNotConfigured(t *testing.T) {
|
||||
sessions := oidc.NewSessionStore()
|
||||
users := &mockUserRepo{users: map[string]*domain.User{"alice": aliceUser()}}
|
||||
h, _ := newTokenHandler(t, sessions, users)
|
||||
// h.TenantEngine left nil -- must not panic, must simply omit the claim.
|
||||
|
||||
verifier := "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
|
||||
code := seededSession(sessions, verifier)
|
||||
|
||||
params := url.Values{
|
||||
"grant_type": []string{"authorization_code"},
|
||||
"code": []string{code},
|
||||
"client_id": []string{"test-client"},
|
||||
"redirect_uri": []string{"https://app.example.com/callback"},
|
||||
"code_verifier": []string{verifier},
|
||||
}
|
||||
req := tokenRequest(params)
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected 200, got %d (body: %s)", w.Code, w.Body.String())
|
||||
}
|
||||
resp := decodeTokenResponse(t, w.Body.String())
|
||||
claims := parseJWTPayload(t, resp["id_token"].(string))
|
||||
|
||||
if _, present := claims["tenant_roles"]; present {
|
||||
t.Errorf("tenant_roles must be omitted when TenantEngine is nil, got %v", claims["tenant_roles"])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue