44
44
import org .springframework .security .oauth2 .server .authorization .authentication .OAuth2RefreshTokenAuthenticationProvider ;
45
45
import org .springframework .security .oauth2 .server .authorization .authentication .OAuth2TokenIntrospectionAuthenticationProvider ;
46
46
import org .springframework .security .oauth2 .server .authorization .authentication .OAuth2TokenRevocationAuthenticationProvider ;
47
+ import org .springframework .security .oauth2 .server .authorization .authentication .OidcClientRegistrationAuthenticationProvider ;
47
48
import org .springframework .security .oauth2 .server .authorization .client .RegisteredClientRepository ;
48
49
import org .springframework .security .oauth2 .server .authorization .config .ProviderSettings ;
50
+ import org .springframework .security .oauth2 .server .authorization .oidc .web .OidcClientRegistrationEndpointFilter ;
49
51
import org .springframework .security .oauth2 .server .authorization .oidc .web .OidcProviderConfigurationEndpointFilter ;
50
52
import org .springframework .security .oauth2 .server .authorization .web .NimbusJwkSetEndpointFilter ;
51
53
import org .springframework .security .oauth2 .server .authorization .web .OAuth2AuthorizationEndpointFilter ;
69
71
* @author Joe Grandja
70
72
* @author Daniel Garnier-Moiroux
71
73
* @author Gerardo Roza
74
+ * @author Ovidiu Popa
72
75
* @since 0.0.1
73
76
* @see AbstractHttpConfigurer
74
77
* @see RegisteredClientRepository
81
84
* @see OidcProviderConfigurationEndpointFilter
82
85
* @see OAuth2AuthorizationServerMetadataEndpointFilter
83
86
* @see OAuth2ClientAuthenticationFilter
87
+ * @see OidcClientRegistrationEndpointFilter
84
88
*/
85
89
public final class OAuth2AuthorizationServerConfigurer <B extends HttpSecurityBuilder <B >>
86
90
extends AbstractHttpConfigurer <OAuth2AuthorizationServerConfigurer <B >, B > {
@@ -92,14 +96,16 @@ public final class OAuth2AuthorizationServerConfigurer<B extends HttpSecurityBui
92
96
private RequestMatcher jwkSetEndpointMatcher ;
93
97
private RequestMatcher oidcProviderConfigurationEndpointMatcher ;
94
98
private RequestMatcher authorizationServerMetadataEndpointMatcher ;
99
+ private RequestMatcher oidcClientRegistrationEndpointMatcher ;
95
100
private final RequestMatcher endpointsMatcher = (request ) ->
96
101
this .authorizationEndpointMatcher .matches (request ) ||
97
102
this .tokenEndpointMatcher .matches (request ) ||
98
103
this .tokenIntrospectionEndpointMatcher .matches (request ) ||
99
104
this .tokenRevocationEndpointMatcher .matches (request ) ||
100
105
this .jwkSetEndpointMatcher .matches (request ) ||
101
106
this .oidcProviderConfigurationEndpointMatcher .matches (request ) ||
102
- this .authorizationServerMetadataEndpointMatcher .matches (request );
107
+ this .authorizationServerMetadataEndpointMatcher .matches (request ) ||
108
+ this .oidcClientRegistrationEndpointMatcher .matches (request );
103
109
104
110
/**
105
111
* Sets the repository of registered clients.
@@ -146,6 +152,17 @@ public RequestMatcher getEndpointsMatcher() {
146
152
return this .endpointsMatcher ;
147
153
}
148
154
155
+ /**
156
+ * Returns {@code true} if the OIDC Client Registration endpoint is enabled.
157
+ * The default is {@code false}.
158
+ *
159
+ * @return {@code true} if the OIDC Client Registration endpoint is enabled, {@code false} otherwise
160
+ */
161
+ public boolean isOidcClientRegistrationEnabled () {
162
+ ProviderSettings providerSettings = getProviderSettings (this .getBuilder ());
163
+ return providerSettings .isOidClientRegistrationEndpointEnabled ();
164
+ }
165
+
149
166
@ Override
150
167
public void init (B builder ) {
151
168
ProviderSettings providerSettings = getProviderSettings (builder );
@@ -199,6 +216,11 @@ public void init(B builder) {
199
216
getAuthorizationService (builder ));
200
217
builder .authenticationProvider (postProcess (tokenRevocationAuthenticationProvider ));
201
218
219
+ OidcClientRegistrationAuthenticationProvider clientRegistrationAuthenticationProvider =
220
+ new OidcClientRegistrationAuthenticationProvider (
221
+ getAuthorizationService (builder ));
222
+ builder .authenticationProvider (postProcess (clientRegistrationAuthenticationProvider ));
223
+
202
224
ExceptionHandlingConfigurer <B > exceptionHandling = builder .getConfigurer (ExceptionHandlingConfigurer .class );
203
225
if (exceptionHandling != null ) {
204
226
exceptionHandling .defaultAuthenticationEntryPointFor (
@@ -224,6 +246,9 @@ public void configure(B builder) {
224
246
builder .addFilterBefore (postProcess (authorizationServerMetadataEndpointFilter ), AbstractPreAuthenticatedProcessingFilter .class );
225
247
}
226
248
249
+ RegisteredClientRepository registeredClientRepository = getRegisteredClientRepository (builder );
250
+ OAuth2AuthorizationService authorizationService = getAuthorizationService (builder );
251
+
227
252
JWKSource <SecurityContext > jwkSource = getJwkSource (builder );
228
253
NimbusJwkSetEndpointFilter jwkSetEndpointFilter = new NimbusJwkSetEndpointFilter (
229
254
jwkSource ,
@@ -243,8 +268,8 @@ public void configure(B builder) {
243
268
244
269
OAuth2AuthorizationEndpointFilter authorizationEndpointFilter =
245
270
new OAuth2AuthorizationEndpointFilter (
246
- getRegisteredClientRepository ( builder ) ,
247
- getAuthorizationService ( builder ) ,
271
+ registeredClientRepository ,
272
+ authorizationService ,
248
273
providerSettings .authorizationEndpoint ());
249
274
builder .addFilterBefore (postProcess (authorizationEndpointFilter ), AbstractPreAuthenticatedProcessingFilter .class );
250
275
@@ -265,6 +290,15 @@ public void configure(B builder) {
265
290
authenticationManager ,
266
291
providerSettings .tokenRevocationEndpoint ());
267
292
builder .addFilterAfter (postProcess (tokenRevocationEndpointFilter ), OAuth2TokenIntrospectionEndpointFilter .class );
293
+
294
+ if (providerSettings .isOidClientRegistrationEndpointEnabled ()) {
295
+ OidcClientRegistrationEndpointFilter oidcClientRegistrationEndpointFilter =
296
+ new OidcClientRegistrationEndpointFilter (
297
+ registeredClientRepository ,
298
+ authenticationManager ,
299
+ providerSettings .oidcClientRegistrationEndpoint ());
300
+ builder .addFilterAfter (postProcess (oidcClientRegistrationEndpointFilter ), OAuth2TokenRevocationEndpointFilter .class );
301
+ }
268
302
}
269
303
270
304
private void initEndpointMatchers (ProviderSettings providerSettings ) {
@@ -287,6 +321,9 @@ private void initEndpointMatchers(ProviderSettings providerSettings) {
287
321
OidcProviderConfigurationEndpointFilter .DEFAULT_OIDC_PROVIDER_CONFIGURATION_ENDPOINT_URI , HttpMethod .GET .name ());
288
322
this .authorizationServerMetadataEndpointMatcher = new AntPathRequestMatcher (
289
323
OAuth2AuthorizationServerMetadataEndpointFilter .DEFAULT_OAUTH2_AUTHORIZATION_SERVER_METADATA_ENDPOINT_URI , HttpMethod .GET .name ());
324
+ this .oidcClientRegistrationEndpointMatcher = new AntPathRequestMatcher (
325
+ providerSettings .oidcClientRegistrationEndpoint (),
326
+ HttpMethod .POST .name ());
290
327
}
291
328
292
329
private static void validateProviderSettings (ProviderSettings providerSettings ) {
0 commit comments