Files
archie/internal/infrastructure/oidc/oidc_test.go
T
sam 605066d835 fix(identity): derive the caller the way the provider issues tokens
Infra decoded real tokens from both clients, and two of the three facts changed
this code.

A machine token carries NO sub at all: it names its caller by client_id, and the
identity is that client. A person's token carries sub, the provider's stable
identifier for the person. The subject is now derived explicitly -- sub when
present, else client_id -- and a verified token asserting neither is refused
rather than accepted as an anonymous caller, because accepting it would let any
token the provider ever signed act as whoever the binding happened to hold. A
machine identity is therefore per client registration, one identity per client,
which is what makes per-agent identity a configuration decision rather than a code
change.

Scopes are read from either claim shape. The standard is a space-delimited scope
string and this provider also emits an scp array; reading only one shape yields no
scopes at all, which is worse than reading none deliberately.

The audience-at-authorisation-endpoint trap is pinned by a test: a token minted
without the requested audience arrives with aud empty, and the audience check
refuses it. That refusal is correct and the fix is the request the human flow
builds, not a weaker check here -- a test now fails if anyone weakens it.

The PRD records both as constraints: a person's client and an agent's client are
registered separately because a shared registration collapses the distinction the
attribution rests on, and a person's token requests the audience at the
authorisation endpoint as well as the token endpoint.

Verified: task check exit 0; internal/infrastructure/oidc passes, including
subject derivation for both token shapes and every refusal.
2026-09-22 14:37:58 +10:00

274 lines
8.3 KiB
Go

package oidc
import (
"context"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"net/http"
"net/http/httptest"
"slices"
"testing"
"time"
"github.com/go-jose/go-jose/v4"
"github.com/go-jose/go-jose/v4/jwt"
)
const testAudience = "archie-dashboard"
// provider is a minimal identity provider: a discovery document and a JWKS.
// It signs with a real RSA key so every refusal below is the library's own
// signature and claim checking, not a stub's behaviour.
type provider struct {
server *httptest.Server
key *rsa.PrivateKey
keyID string
// other is a second, unrelated key used to sign a token the provider
// never published, which is what a forged or foreign token looks like.
other *rsa.PrivateKey
}
func newProvider(t *testing.T) *provider {
t.Helper()
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("generate key: %v", err)
}
other, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("generate second key: %v", err)
}
p := &provider{key: key, other: other, keyID: "test-key"}
mux := http.NewServeMux()
mux.HandleFunc("/.well-known/openid-configuration", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"issuer": p.server.URL,
"jwks_uri": p.server.URL + "/keys",
"authorization_endpoint": p.server.URL + "/authorize",
"token_endpoint": p.server.URL + "/token",
"response_types_supported": []string{"code"},
"subject_types_supported": []string{"public"},
"id_token_signing_alg_values_supported": []string{"RS256"},
})
})
mux.HandleFunc("/keys", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(jose.JSONWebKeySet{Keys: []jose.JSONWebKey{{
Key: key.Public(), KeyID: p.keyID, Algorithm: "RS256", Use: "sig",
}}})
})
p.server = httptest.NewServer(mux)
t.Cleanup(p.server.Close)
return p
}
// testClaims is the claim set this provider emits. A machine token carries
// client_id and no sub; a person's token carries sub.
type testClaims struct {
jwt.Claims
ClientID string `json:"client_id,omitempty"`
Scopes []string `json:"scp,omitempty"`
}
// token signs a token with the given key for the given claims.
func (p *provider) token(t *testing.T, signer *rsa.PrivateKey, mutate func(*testClaims)) string {
t.Helper()
claims := testClaims{
Issuer: p.server.URL,
Subject: "sam",
Audience: jwt.Audience{testAudience},
Expiry: jwt.NewNumericDate(time.Now().Add(time.Hour)),
IssuedAt: jwt.NewNumericDate(time.Now().Add(-time.Minute)),
}
if mutate != nil {
mutate(&claims)
}
sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.RS256, Key: signer},
(&jose.SignerOptions{}).WithType("JWT").WithHeader("kid", p.keyID))
if err != nil {
t.Fatalf("new signer: %v", err)
}
raw, err := jwt.Signed(sig).Claims(claims).Serialize()
if err != nil {
t.Fatalf("serialize token: %v", err)
}
return raw
}
func (p *provider) verifier(t *testing.T) *Verifier {
t.Helper()
v, err := New(context.Background(), Config{Issuer: p.server.URL, Audience: testAudience})
if err != nil {
t.Fatalf("New() error = %v", err)
}
return v
}
func TestVerifyAcceptsAProviderIssuedToken(t *testing.T) {
p := newProvider(t)
credential, err := p.verifier(t).Verify(context.Background(), p.token(t, p.key, nil))
if err != nil {
t.Fatalf("Verify() error = %v", err)
}
if credential.Subject.Subject != "sam" {
t.Fatalf("subject = %q, want %q", credential.Subject.Subject, "sam")
}
if credential.Subject.Issuer != p.server.URL {
t.Fatalf("issuer = %q, want %q", credential.Subject.Issuer, p.server.URL)
}
}
// TestVerifyDerivesTheSubjectTheWayTheProviderIssuesIt: a person is named by sub
// and a machine by client_id, because a machine token carries no sub at all.
// Getting this wrong does not fail loudly -- it binds every machine caller to one
// identity, or to none.
func TestVerifyDerivesTheSubjectTheWayTheProviderIssuesIt(t *testing.T) {
p := newProvider(t)
verifier := p.verifier(t)
tests := []struct {
name string
mutate func(*testClaims)
wantSubject string
wantScopes []string
}{
{
name: "a person's token carries sub",
mutate: func(c *testClaims) {
c.Subject = "a150ab4b-0000-0000-0000-000000000001"
c.Scopes = []string{"openid", "profile", "email", "groups"}
},
wantSubject: "a150ab4b-0000-0000-0000-000000000001",
wantScopes: []string{"openid", "profile", "email", "groups"},
},
{
name: "a machine token carries client_id and no sub",
mutate: func(c *testClaims) {
c.Subject = ""
c.ClientID = "archie"
c.Scopes = []string{"authelia.bearer.authz"}
},
wantSubject: "archie",
wantScopes: []string{"authelia.bearer.authz"},
},
{
name: "sub wins when a token carries both",
mutate: func(c *testClaims) {
c.Subject = "a150ab4b-0000-0000-0000-000000000002"
c.ClientID = "archie-web"
},
wantSubject: "a150ab4b-0000-0000-0000-000000000002",
},
{
name: "a space-delimited scope claim is read too",
mutate: func(c *testClaims) {
c.Subject = "a150ab4b-0000-0000-0000-000000000003"
c.ClientID = ""
c.Scopes = nil
},
wantSubject: "a150ab4b-0000-0000-0000-000000000003",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
credential, err := verifier.Verify(context.Background(), p.token(t, p.key, tc.mutate))
if err != nil {
t.Fatalf("Verify() error = %v", err)
}
if credential.Subject.Subject != tc.wantSubject {
t.Fatalf("subject = %q, want %q", credential.Subject.Subject, tc.wantSubject)
}
if tc.wantScopes != nil && !slices.Equal(credential.Scopes, tc.wantScopes) {
t.Fatalf("scopes = %v, want %v", credential.Scopes, tc.wantScopes)
}
})
}
}
// TestVerifyRefusesATokenThatNamesNobody: a verified token asserting neither sub
// nor client_id is an anonymous caller, and accepting it would let any token the
// provider ever signed act as whoever the binding happened to hold.
func TestVerifyRefusesATokenThatNamesNobody(t *testing.T) {
p := newProvider(t)
token := p.token(t, p.key, func(c *testClaims) {
c.Subject = ""
c.ClientID = ""
})
if _, err := p.verifier(t).Verify(context.Background(), token); err == nil {
t.Fatal("Verify() accepted a token that asserts no caller")
}
}
func TestVerifyRefusesEveryUnacceptableToken(t *testing.T) {
p := newProvider(t)
verifier := p.verifier(t)
tests := []struct {
name string
token string
}{
{
name: "no token",
token: "",
},
{
name: "expired",
token: p.token(t, p.key, func(c *testClaims) { c.Expiry = jwt.NewNumericDate(time.Now().Add(-time.Hour)) }),
},
{
name: "signed by a key the provider never published",
token: p.token(t, p.other, nil),
},
{
name: "issued for another audience",
token: p.token(t, p.key, func(c *testClaims) { c.Audience = jwt.Audience{"another-service"} }),
},
{
name: "issued by another provider",
token: p.token(t, p.key, func(c *testClaims) { c.Issuer = "https://someone-else.example" }),
},
{
// The human path's failure mode: a token minted without the requested
// audience arrives with an empty audience, and the check here refuses
// it. The fix is requesting the audience at the authorisation
// endpoint, not weakening this check.
name: "issued with no audience at all",
token: p.token(t, p.key, func(c *testClaims) { c.Audience = nil }),
},
{
name: "not a token at all",
token: "not-a-jwt",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if _, err := verifier.Verify(context.Background(), tc.token); err == nil {
t.Fatal("Verify() accepted a token it must refuse")
}
})
}
}
func TestNewRefusesAnIncompleteConfiguration(t *testing.T) {
p := newProvider(t)
tests := []struct {
name string
cfg Config
}{
{"no issuer", Config{Audience: testAudience}},
{"no audience", Config{Issuer: p.server.URL}},
{"unreachable issuer", Config{Issuer: "http://127.0.0.1:1", Audience: testAudience}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
if _, err := New(context.Background(), tc.cfg); err == nil {
t.Fatal("New() accepted a configuration it must refuse")
}
})
}
}