Skip to content

Commit 1d0e6cb

Browse files
committed
Document usage for OAuth2ClientCredentialsAuthenticationProvider.setAuthenticationValidator()
Issue gh-1377
1 parent 463b2c4 commit 1d0e6cb

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

docs/modules/ROOT/pages/protocol-endpoints.adoc

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,71 @@ The supported https://datatracker.ietf.org/doc/html/rfc6749#section-1.3[authoriz
266266
* `*AuthenticationSuccessHandler*` -- An internal implementation that handles an `OAuth2AccessTokenAuthenticationToken` and returns the `OAuth2AccessTokenResponse`.
267267
* `*AuthenticationFailureHandler*` -- An `OAuth2ErrorAuthenticationFailureHandler`.
268268

269+
[[oauth2-token-endpoint-customizing-client-credentials-grant-request-validation]]
270+
=== Customizing Client Credentials Grant Request Validation
271+
272+
`OAuth2ClientCredentialsAuthenticationValidator` is the default validator used for validating specific OAuth2 Client Credentials Grant request parameters.
273+
The default implementation validates the `scope` parameter.
274+
If validation fails, an `OAuth2AuthenticationException` is thrown.
275+
276+
`OAuth2ClientCredentialsAuthenticationProvider` provides the ability to override the default request validation by supplying a custom authentication validator of type `Consumer<OAuth2ClientCredentialsAuthenticationContext>` to `setAuthenticationValidator()`.
277+
278+
[TIP]
279+
`OAuth2ClientCredentialsAuthenticationContext` holds the `OAuth2ClientCredentialsAuthenticationToken`, which contains the OAuth2 Client Credentials Grant request parameters.
280+
281+
[IMPORTANT]
282+
If validation fails, the authentication validator *MUST* throw `OAuth2AuthenticationException`.
283+
284+
The following example shows how to configure `OAuth2ClientCredentialsAuthenticationProvider` with a custom authentication validator that overrides the default `scope` validation:
285+
286+
[source,java]
287+
----
288+
@Bean
289+
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
290+
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
291+
new OAuth2AuthorizationServerConfigurer();
292+
http.apply(authorizationServerConfigurer);
293+
294+
authorizationServerConfigurer
295+
.tokenEndpoint(tokenEndpoint ->
296+
tokenEndpoint
297+
.authenticationProviders(configureAuthenticationValidator())
298+
);
299+
300+
return http.build();
301+
}
302+
303+
private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
304+
return (authenticationProviders) ->
305+
authenticationProviders.forEach((authenticationProvider) -> {
306+
if (authenticationProvider instanceof OAuth2ClientCredentialsAuthenticationProvider) {
307+
Consumer<OAuth2ClientCredentialsAuthenticationContext> authenticationValidator =
308+
new CustomScopeValidator();
309+
310+
// Override default scope validation
311+
((OAuth2ClientCredentialsAuthenticationProvider) authenticationProvider)
312+
.setAuthenticationValidator(authenticationValidator);
313+
}
314+
});
315+
}
316+
317+
static class CustomScopeValidator implements Consumer<OAuth2ClientCredentialsAuthenticationContext> {
318+
319+
@Override
320+
public void accept(OAuth2ClientCredentialsAuthenticationContext authenticationContext) {
321+
OAuth2ClientCredentialsAuthenticationToken clientCredentialsAuthentication =
322+
authenticationContext.getAuthentication();
323+
324+
Set<String> requestedScopes = clientCredentialsAuthentication.getScopes();
325+
RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
326+
Set<String> allowedScopes = registeredClient.getScopes();
327+
328+
// TODO Implement scope validation
329+
330+
}
331+
}
332+
----
333+
269334
[[oauth2-token-introspection-endpoint]]
270335
== OAuth2 Token Introspection Endpoint
271336

0 commit comments

Comments
 (0)