Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/mutation/mutants.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
220 changes: 220 additions & 0 deletions server/dpop_alias_route_grants_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
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"
"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 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"
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)
})
}
}

// 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)
})
}
}
Loading