From 818a716d80dc4d322d0409bf04049d763b0e30c8 Mon Sep 17 00:00:00 2001 From: Oscar Sanderson Date: Fri, 27 Nov 2026 09:25:16 +0800 Subject: [PATCH 1/3] test(server): cover the client_credentials grant at the mTLS alias The client_credentials grant passes the request's endpoint route to the DPoP check, but no local test exercised it at the token endpoint's mTLS alias, so dropping the route would only show in a full conformance run. Add a table test for a certificate-authenticated client with DPoP sender-constraining: a proof for the alias is accepted on a request routed to the alias and refused as invalid_dpop_proof without a route; a proof for the primary URL is accepted without a route (and on the primary route) and refused on the alias route. Accepted requests get a DPoP token whose cnf.jkt is the proof's key. Co-Authored-By: Claude Opus 5.5 --- server/dpop_alias_route_grants_test.go | 156 +++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 server/dpop_alias_route_grants_test.go diff --git a/server/dpop_alias_route_grants_test.go b/server/dpop_alias_route_grants_test.go new file mode 100644 index 00000000..3f6e9b15 --- /dev/null +++ b/server/dpop_alias_route_grants_test.go @@ -0,0 +1,156 @@ +package server_test + +import ( + "context" + "crypto/ecdsa" + "crypto/x509" + "testing" + "time" + + fapi "github.com/idfoundry/fapigo" + "github.com/idfoundry/fapigo/internal/mtls" + "github.com/idfoundry/fapigo/internal/token" + "github.com/idfoundry/fapigo/server" + "github.com/idfoundry/fapigo/storage" +) + +// The tests in this file cover token-endpoint grants whose DPoP proof +// is checked against the URL the request's EndpointRoute names, +// alongside the code exchange and refresh cases in +// client_auth_mtls_test.go: the client_credentials grant. Each runs a +// client authenticated by its certificate (so it calls the token +// endpoint's mTLS alias) with DPoP-bound tokens. + +const ( + aliasRouteMTLSToken = "https://mtls.as.example/token" + aliasRoutePrimaryToken = testTokenEndpoint +) + +// aliasRouteCase is one combination of a request's endpoint route and +// the URL its DPoP proof names, and whether the request is accepted. +type aliasRouteCase struct { + route server.EndpointRoute + proofURL string + accepted bool +} + +// aliasRouteCases: a proof verifies only against the URL the route +// names, and without a route against the primary URL only. +var aliasRouteCases = map[string]aliasRouteCase{ + "alias route, alias proof": {route: server.MTLSEndpointAlias, proofURL: aliasRouteMTLSToken, accepted: true}, + "no route, alias proof": {route: 0, proofURL: aliasRouteMTLSToken, accepted: false}, + "no route, primary proof": {route: 0, proofURL: aliasRoutePrimaryToken, accepted: true}, + "alias route, primary proof": {route: server.MTLSEndpointAlias, proofURL: aliasRoutePrimaryToken, accepted: false}, + "primary route, primary proof": {route: server.PrimaryEndpoint, proofURL: aliasRoutePrimaryToken, accepted: true}, +} + +// certAuthClient registers testClientID for self_signed_tls_client_auth +// with DPoP sender-constraining (the default), adjusted by mutate. +func certAuthClient(t *testing.T, cert *x509.Certificate, mutate func(*storage.RegisteredClientConfig)) storage.RegisteredClient { + t.Helper() + cfg := storage.RegisteredClientConfig{ + ID: testClientID, + RedirectURIs: []fapi.RegisteredRedirectURI{testRedirectURI}, + ClientAuthMethod: storage.ClientAuthMethodSelfSignedTLSClientAuth, + ExpectedCertificateThumbprint: mtls.Thumbprint(cert), + AllowedScopes: []string{"openid", "accounts"}, + } + mutate(&cfg) + client, err := storage.NewRegisteredClient(cfg) + if err != nil { + t.Fatalf("NewRegisteredClient: %v", err) + } + return client +} + +func aliasRouteMTLSEndpoints(t *testing.T) server.MTLSEndpoints { + t.Helper() + mtlsToken, err := fapi.ParseEndpointURL(aliasRouteMTLSToken) + if err != nil { + t.Fatalf("ParseEndpointURL: %v", err) + } + return server.MTLSEndpoints{Token: mtlsToken} +} + +// requireDPoPBoundTo checks result is a DPoP access token whose cnf.jkt +// is key's thumbprint. +func requireDPoPBoundTo(t *testing.T, h harness, result server.TokenResult, key *ecdsa.PrivateKey) { + t.Helper() + if result.TokenType != "DPoP" { + t.Fatalf("TokenType = %q, want %q", result.TokenType, "DPoP") + } + parsed, err := token.ParseAccessToken(result.AccessToken.Reveal()) + if err != nil { + t.Fatalf("ParseAccessToken: %v", err) + } + validated, err := parsed.Validate(&h.serverKey.PublicKey, token.AccessTokenValidatePolicy{ + ExpectedIssuer: testIssuer, ExpectedAudience: testIssuer, + Algorithm: fapi.ES256, Now: h.now, MaxLifetime: 10 * time.Minute, + }) + if err != nil { + t.Fatalf("Validate access token: %v", err) + } + if want := dpopThumbprint(t, key); validated.JKT != want { + t.Fatalf("access token cnf.jkt = %q, want %q", validated.JKT, want) + } +} + +// requireAliasRouteOutcome checks one aliasRouteCase's result: tokens +// bound to key when accepted, invalid_dpop_proof when not. +func requireAliasRouteOutcome(t *testing.T, h harness, tc aliasRouteCase, result server.TokenResult, err error, key *ecdsa.PrivateKey) { + t.Helper() + if !tc.accepted { + if serverErrorCode(t, err) != server.ErrorInvalidDPoPProof { + t.Fatalf("error = %v, want invalid_dpop_proof", err) + } + return + } + if err != nil { + t.Fatalf("error = %v, want tokens", err) + } + requireDPoPBoundTo(t, h, result, key) +} + +// newHarnessWithClientCredentialsAtMTLSAlias is +// newHarnessWithClientCredentialsGrant with testClientID authenticated +// by certificate and the token endpoint's mTLS alias configured. +func newHarnessWithClientCredentialsAtMTLSAlias(t *testing.T) (harness, *x509.Certificate) { + t.Helper() + h := newHarnessWithClientCredentialsGrant(t, storage.SenderConstrainDPoP, true) + cert := selfSignedTestClientCert(t) + client := certAuthClient(t, cert, func(c *storage.RegisteredClientConfig) { + c.AllowedScopes = []string{"accounts"} + c.AllowsClientCredentialsGrant = true + }) + h.cfg.MTLSEndpoints = aliasRouteMTLSEndpoints(t) + h.clients = &fakeClientRepository{clients: map[fapi.ClientID]storage.RegisteredClient{testClientID: client}} + h.deps.Clients = h.clients + srv, err := server.New(h.cfg, h.deps) + if err != nil { + t.Fatalf("server.New: %v", err) + } + h.server = srv + return h, cert +} + +// TestRequestClientCredentialsTokenDPoPProofAtMTLSAlias: a +// client_credentials request is checked against the URL its route +// names, as a code exchange is. +func TestRequestClientCredentialsTokenDPoPProofAtMTLSAlias(t *testing.T) { + for name, tc := range aliasRouteCases { + t.Run(name, func(t *testing.T) { + h, cert := newHarnessWithClientCredentialsAtMTLSAlias(t) + dpopKey := generateKey(t) + result, err := h.server.RequestClientCredentialsToken(context.Background(), server.ClientCredentialsTokenRequest{ + HTTP: server.FormRequest{EndpointRoute: tc.route, Parameters: []server.FormParameter{ + formParam("client_id", testClientID.String()), + formParam("grant_type", "client_credentials"), + formParam("scope", "accounts"), + }}, + DPoPProofs: []string{dpopProofForURL(t, dpopKey, tc.proofURL, h.now)}, + PeerCertificate: cert, + }) + requireAliasRouteOutcome(t, h, tc, result, err, dpopKey) + }) + } +} From 8afc7e53d89739bc374fcd126f098a9427a6c2bd Mon Sep 17 00:00:00 2001 From: Oscar Sanderson Date: Fri, 27 Nov 2026 09:25:17 +0800 Subject: [PATCH 2/3] test(server): cover the CIBA token request at the mTLS alias The CIBA token request passes the request's endpoint route to the DPoP check, but no local test exercised it at the token endpoint's mTLS alias. Drive a poll-mode flow for a certificate-authenticated client to an approved request, then redeem its auth_req_id under each combination of route and proof URL: a proof for the alias is accepted only on a request routed to the alias, a proof for the primary URL only without a route or on the primary route, and the refusals are invalid_dpop_proof. Accepted requests get a DPoP token whose cnf.jkt is the proof's key. Co-Authored-By: Claude Opus 5.5 --- server/dpop_alias_route_grants_test.go | 70 ++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/server/dpop_alias_route_grants_test.go b/server/dpop_alias_route_grants_test.go index 3f6e9b15..547246d9 100644 --- a/server/dpop_alias_route_grants_test.go +++ b/server/dpop_alias_route_grants_test.go @@ -12,14 +12,15 @@ import ( "github.com/idfoundry/fapigo/internal/token" "github.com/idfoundry/fapigo/server" "github.com/idfoundry/fapigo/storage" + "github.com/idfoundry/fapigo/storage/memstore" ) // The tests in this file cover token-endpoint grants whose DPoP proof // is checked against the URL the request's EndpointRoute names, // alongside the code exchange and refresh cases in -// client_auth_mtls_test.go: the client_credentials grant. Each runs a -// client authenticated by its certificate (so it calls the token -// endpoint's mTLS alias) with DPoP-bound tokens. +// client_auth_mtls_test.go: the client_credentials grant and the CIBA +// token request. Each runs a client authenticated by its certificate +// (so it calls the token endpoint's mTLS alias) with DPoP-bound tokens. const ( aliasRouteMTLSToken = "https://mtls.as.example/token" @@ -154,3 +155,66 @@ func TestRequestClientCredentialsTokenDPoPProofAtMTLSAlias(t *testing.T) { }) } } + +// newHarnessWithBackchannelAtMTLSAlias is newHarnessWithBackchannel +// with testClientID authenticated by certificate and the token +// endpoint's mTLS alias configured. +func newHarnessWithBackchannelAtMTLSAlias(t *testing.T) (harness, *x509.Certificate) { + t.Helper() + cert := selfSignedTestClientCert(t) + client := certAuthClient(t, cert, func(c *storage.RegisteredClientConfig) { + c.BackchannelAuthenticationRequestAlgorithm = fapi.ES256 + }) + h := newHarnessWithBackchannelOptions(t, memstore.NewBackchannelAuthenticationStore(), + func(c *server.Config) { c.MTLSEndpoints = aliasRouteMTLSEndpoints(t) }, + func(d *server.Dependencies) { + d.Clients.(*fakeClientRepository).clients[testClientID] = client + }) + return h, cert +} + +// approvedCertAuthBackchannel begins a CIBA request for the +// certificate-authenticated client and approves it, returning its +// auth_req_id. +func approvedCertAuthBackchannel(t *testing.T, h harness, cert *x509.Certificate) string { + t.Helper() + action, err := h.server.BeginBackchannelAuthentication(context.Background(), server.BeginBackchannelAuthenticationRequest{ + HTTP: server.FormRequest{Parameters: []server.FormParameter{ + formParam("client_id", testClientID.String()), + formParam("request", h.backchannelRequestObject(t, standardBackchannelParams(t))), + }}, + PeerCertificate: cert, + }) + if err != nil { + t.Fatalf("BeginBackchannelAuthentication: %v", err) + } + required, ok := action.(server.BackchannelInteractionRequired) + if !ok { + t.Fatalf("action = %#v, want server.BackchannelInteractionRequired", action) + } + approveBackchannel(t, h, required.Handle) + return required.AuthReqID.String() +} + +// TestExchangeBackchannelAuthenticationDPoPProofAtMTLSAlias: a CIBA +// token request is checked against the URL its route names, as a code +// exchange is. +func TestExchangeBackchannelAuthenticationDPoPProofAtMTLSAlias(t *testing.T) { + for name, tc := range aliasRouteCases { + t.Run(name, func(t *testing.T) { + h, cert := newHarnessWithBackchannelAtMTLSAlias(t) + authReqID := approvedCertAuthBackchannel(t, h, cert) + dpopKey := generateKey(t) + result, err := h.server.ExchangeBackchannelAuthentication(context.Background(), server.BackchannelTokenExchangeRequest{ + HTTP: server.FormRequest{EndpointRoute: tc.route, Parameters: []server.FormParameter{ + formParam("client_id", testClientID.String()), + formParam("grant_type", server.CIBAGrantType), + formParam("auth_req_id", authReqID), + }}, + DPoPProofs: []string{dpopProofForURL(t, dpopKey, tc.proofURL, h.now)}, + PeerCertificate: cert, + }) + requireAliasRouteOutcome(t, h, tc, result, err, dpopKey) + }) + } +} From db4a318d55c42d01dcee6dd268811075704b7d1f Mon Sep 17 00:00:00 2001 From: Oscar Sanderson Date: Fri, 27 Nov 2026 09:25:34 +0800 Subject: [PATCH 3/3] test: add mutants for the client_credentials and CIBA token routes Drop the request's endpoint route at the client_credentials grant's and the CIBA token request's DPoP check, each killed by the new alias-route test for that grant. Co-Authored-By: Claude Opus 5.5 --- .github/mutation/mutants.json | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/.github/mutation/mutants.json b/.github/mutation/mutants.json index 794bb85f..9c5bafac 100644 --- a/.github/mutation/mutants.json +++ b/.github/mutation/mutants.json @@ -6367,5 +6367,29 @@ ], "run": "^(TestAmbiguousDPoPTargetNamesTheFix|TestVerifyDPoPTargetWithOverlappingOrigins)$", "why": "an ambiguous DPoP target is a 500 naming WithResourceOrigin and VerifyRequest.URL" + }, + { + "id": "server-client-credentials-passes-route", + "file": "server/client_credentials.go", + "old": "dpopProof, req.PeerCertificate, req.HTTP.EndpointRoute)", + "new": "dpopProof, req.PeerCertificate, 0*req.HTTP.EndpointRoute)", + "module": "root", + "packages": [ + "./server" + ], + "run": "^TestRequestClientCredentialsTokenDPoPProofAtMTLSAlias$", + "why": "a client_credentials request checks a DPoP proof against the URL its request's route names" + }, + { + "id": "server-ciba-token-passes-route", + "file": "server/backchannel_token.go", + "old": "dpopProof, req.PeerCertificate, req.HTTP.EndpointRoute)", + "new": "dpopProof, req.PeerCertificate, 0*req.HTTP.EndpointRoute)", + "module": "root", + "packages": [ + "./server" + ], + "run": "^TestExchangeBackchannelAuthenticationDPoPProofAtMTLSAlias$", + "why": "a CIBA token request checks a DPoP proof against the URL its request's route names" } ]