Skip to content

Commit 8c78a5b

Browse files
committed
Document Authorization Request Validation
Closes gh-858
1 parent 74fe63a commit 8c78a5b

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

docs/src/docs/asciidoc/protocol-endpoints.adoc

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,76 @@ public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity h
5050
* `*AuthenticationSuccessHandler*` -- An internal implementation that handles an "`authenticated`" `OAuth2AuthorizationCodeRequestAuthenticationToken` and returns the `OAuth2AuthorizationResponse`.
5151
* `*AuthenticationFailureHandler*` -- An internal implementation that uses the `OAuth2Error` associated with the `OAuth2AuthorizationCodeRequestAuthenticationException` and returns the `OAuth2Error` response.
5252

53+
[[oauth2-authorization-endpoint-customizing-authorization-request-validation]]
54+
=== Customizing Authorization Request Validation
55+
56+
`OAuth2AuthorizationCodeRequestAuthenticationValidator` is the default validator used for validating specific OAuth2 authorization request parameters used in the Authorization Code Grant.
57+
The default implementation validates the `redirect_uri` and `scope` parameters.
58+
If validation fails, an `OAuth2AuthorizationCodeRequestAuthenticationException` is thrown.
59+
60+
`OAuth2AuthorizationCodeRequestAuthenticationProvider` provides the ability to override the default authorization request validation by supplying a custom authentication validator of type `Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext>` to `setAuthenticationValidator()`.
61+
62+
[TIP]
63+
`OAuth2AuthorizationCodeRequestAuthenticationContext` holds the `OAuth2AuthorizationCodeRequestAuthenticationToken`, which contains the OAuth2 authorization request parameters.
64+
65+
[IMPORTANT]
66+
If validation fails, the authentication validator *MUST* throw `OAuth2AuthorizationCodeRequestAuthenticationException`.
67+
68+
A common use case during the development life cycle phase is to allow for `localhost` in the `redirect_uri` parameter.
69+
70+
The following example shows how to configure `OAuth2AuthorizationCodeRequestAuthenticationProvider` with a custom authentication validator that allows for `localhost` in the `redirect_uri` parameter:
71+
72+
[source,java]
73+
----
74+
@Bean
75+
public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http) throws Exception {
76+
OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
77+
new OAuth2AuthorizationServerConfigurer();
78+
http.apply(authorizationServerConfigurer);
79+
80+
authorizationServerConfigurer
81+
.authorizationEndpoint(authorizationEndpoint ->
82+
authorizationEndpoint
83+
.authenticationProviders(configureAuthenticationValidator())
84+
);
85+
86+
return http.build();
87+
}
88+
89+
private Consumer<List<AuthenticationProvider>> configureAuthenticationValidator() {
90+
return (authenticationProviders) ->
91+
authenticationProviders.forEach((authenticationProvider) -> {
92+
if (authenticationProvider instanceof OAuth2AuthorizationCodeRequestAuthenticationProvider) {
93+
Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> authenticationValidator =
94+
// Override default redirect_uri validator
95+
new CustomRedirectUriValidator()
96+
// Reuse default scope validator
97+
.andThen(OAuth2AuthorizationCodeRequestAuthenticationValidator.DEFAULT_SCOPE_VALIDATOR);
98+
99+
((OAuth2AuthorizationCodeRequestAuthenticationProvider) authenticationProvider)
100+
.setAuthenticationValidator(authenticationValidator);
101+
}
102+
});
103+
}
104+
105+
static class CustomRedirectUriValidator implements Consumer<OAuth2AuthorizationCodeRequestAuthenticationContext> {
106+
107+
@Override
108+
public void accept(OAuth2AuthorizationCodeRequestAuthenticationContext authenticationContext) {
109+
OAuth2AuthorizationCodeRequestAuthenticationToken authorizationCodeRequestAuthentication =
110+
authenticationContext.getAuthentication();
111+
RegisteredClient registeredClient = authenticationContext.getRegisteredClient();
112+
String requestedRedirectUri = authorizationCodeRequestAuthentication.getRedirectUri();
113+
114+
// Use exact string matching when comparing client redirect URIs against pre-registered URIs
115+
if (!registeredClient.getRedirectUris().contains(requestedRedirectUri)) {
116+
OAuth2Error error = new OAuth2Error(OAuth2ErrorCodes.INVALID_REQUEST);
117+
throw new OAuth2AuthorizationCodeRequestAuthenticationException(error, null);
118+
}
119+
}
120+
}
121+
----
122+
53123
[[oauth2-token-endpoint]]
54124
== OAuth2 Token Endpoint
55125

0 commit comments

Comments
 (0)