Sender-constrained tokens
| This documentation was generated with the assistance of AI. Please report any inaccuracies. |
A bearer token works for whoever holds it. A sender-constrained token is bound to a key the client proves it holds, so a
stolen token alone is useless. Two bindings are implemented: DPoP, where the client signs a fresh proof JWT for every
request and the token carries the thumbprint of the proof key (cnf.jkt), and certificate-bound tokens, where the client
authenticates with mutual TLS and the token carries the thumbprint of its certificate (cnf.x5t#S256). The authorization
server side is Spring Authorization Server’s; service-client hand-wires the client side of DPoP because Spring Security
has none; the resource server checks both bindings.
-
Actors:
service-client,auth-server,resource-server. -
Clients involved: a confidential client of
service-client(see Client credentials). -
Preconditions:
-
DPoP: nothing on the server or the client registration;
service-client.auth.dpop=true(SERVICE_CLIENT_DPOP) onservice-client. -
Certificate-bound: the client registered with
tls_client_authorself_signed_tls_client_authandcertificateBoundAccessTokens: true; the authorization server’s TLS connector (authserver.x509.enabled=true,authserver.x509.port); the resource server served over TLS withserver.ssl.client-auth: wantand a bundle.
-
-
Endpoints:
POST /oauth2/token,GET /api/documents, the metadata endpoints/.well-known/oauth-authorization-serverand/.well-known/openid-configuration(formtls_endpoint_aliases).
Steps
DPoP
-
With
service-client.auth.dpop=true,OAuth2ClientConfigurationdeclares aDPoPProofsbean over Nimbus'DefaultDPoPProofFactorywith a P-256 key generated in memory at startup (signing algorithmES256). Nothing is written to disk. -
TokenEndpointClientsadds aDPoPheader to every token request: a proof forPOSTand the token URI (DPoPProofs.proof(method, uri)drops the query and fragment, ashturequires). -
Spring Authorization Server validates the proof and issues
token_type=DPoPwithcnf.jkt, the JWK SHA-256 thumbprint of the proof key. -
For each resource call,
ResourceServerInterceptorsinstallsDPoPClientHttpRequestInterceptor, which gets the token from the authorized client manager and, when its type isDPoP, sendsAuthorization: DPoP <token>and a new proof for the request’s method and URL withath(the hash of the token). A token the server issued asBeareris sent asBearer. -
The resource server’s JWT chain enables
dPoP(…): it checks the proof’s signature, that its key matchescnf.jkt,htm,htu,ath, its age and itsjtiagainst a replay cache. A DPoP-bound token sent asBeareror without a proof is answered401.
@Override
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body,
final ClientHttpRequestExecution execution) throws IOException {
final var authorizeRequest = OAuth2AuthorizeRequest.withClientRegistrationId(registrationId)
.principal(principal)
.build();
final var authorizedClient = manager.authorize(authorizeRequest);
if (authorizedClient == null) {
throw new ClientAuthorizationException(new OAuth2Error(OAuth2ErrorCodes.ACCESS_DENIED,
"The service client has no access token", null), registrationId);
}
final var token = authorizedClient.getAccessToken();
if (OAuth2AccessToken.TokenType.DPOP.equals(token.getTokenType())) {
request.getHeaders().set(HttpHeaders.AUTHORIZATION, DPOP_SCHEME + token.getTokenValue());
request.getHeaders().set(DPOP_HEADER,
proofs.proof(request.getMethod().name(), request.getURI(), token.getTokenValue()));
} else {
request.getHeaders().setBearerAuth(token.getTokenValue());
}
return execution.execute(request, body);
}
Certificate-bound tokens
-
The client opens a TLS connection to the authorization server’s TLS port and presents its certificate (the SSL bundle named by
service-client.auth.ssl-bundle,SERVICE_CLIENT_SSL_BUNDLE). -
It authenticates with
tls_client_authorself_signed_tls_client_auth(ClientCertificateVerifier, see Client authentication). -
Because the client has
certificateBoundAccessTokens, the framework writescnf.x5t#S256, the base64url SHA-256 thumbprint of the certificate, into the access token. -
The client calls the resource server over TLS with the same certificate. The validator that Spring Boot’s JWT decoder includes compares the thumbprint with the certificate of the connection; without it, or with another certificate, the answer is
401. -
When
authserver.x509.portis set,MtlsEndpointAliasesaddsmtls_endpoint_aliasesto both metadata documents (throughClientAuthProtocolChainCustomizer): the token, revocation, introspection, pushed authorization request and device authorization endpoints ashttpsURLs on that port, so a client knows where to send mutual TLS requests.
// DPoP (RFC 9449): "Authorization: DPoP <token>" plus a proof; the framework already refuses a
// token whose cnf holds jkt when it arrives as Bearer. A cnf.x5t#S256 token is checked against the
// TLS client certificate by the validator of the decoder Boot builds (no JwtDecoder bean here).
.dPoP(Customizer.withDefaults())
Tokens and claims
-
DPoP: the token response has
token_type=DPoP; the access token hascnf: {"jkt": "<JWK SHA-256 thumbprint>"}. -
Certificate-bound: the access token has
cnf: {"x5t#S256": "<certificate SHA-256 thumbprint>"};token_typestaysBearer. -
All other claims are those of the grant (for client credentials:
sub= client id,aud,scope).
How it is tested
-
auth-server/boot:ClientAuthenticationIT(token_whenADpopProofIsSent_issuesADpopTokenBoundToTheProofKey,token_whenCertificateBoundAccessTokensAreEnabled_carriesTheCertificateThumbprint). -
resource-server/boot:BoundTokenITaccepts a certificate-bound token only with its certificate, and a DPoP-bound token only with a valid proof from the bound key (refused asBearer, without a proof or with a proof of another key). -
service-client/infrastructure/resourceserver:ResourceServerAdapterIT(listDocuments_whenDPoPIsOn_sendsProofsAndTheDPoPScheme); unit testsDPoPProofsTestandDPoPClientHttpRequestInterceptorTest. -
e2e-tests:ClientCredentialsEndToEndIT(clientCredentials_whenTheToolUsesDpop_theTokenIsBoundToItsKeyandclientCredentials_whenTheClientPresentsACertificateOfATrustedAuthority_theTokenIsBoundToTheCertificate). -
auth-server/api/web: unit testMtlsEndpointAliasesTest.