Skip to content

Commit

Permalink
Pick end_session_endpoint from OpenID conf first, then spring-addons …
Browse files Browse the repository at this point in the history
…logout one
  • Loading branch information
ch4mpy committed Jun 14, 2023
1 parent eda1904 commit 14b0d77
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 6 deletions.
12 changes: 9 additions & 3 deletions samples/tutorials/reactive-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,18 @@ static class DelegatingOidcClientInitiatedServerLogoutSuccessHandler implements
String postLogoutRedirectUri) {
delegates = StreamSupport.stream(clientRegistrationRepository.spliterator(), false)
.collect(Collectors.toMap(ClientRegistration::getRegistrationId, clientRegistration -> {
final var registrationProperties = properties.getRegistration().get(clientRegistration.getRegistrationId());
if (registrationProperties == null) {
final var endSessionEnpoint = (String) (clientRegistration.getProviderDetails().getConfigurationMetadata().get("end_session_endpoint"));
if (StringUtils.hasText(endSessionEnpoint)) {
final var handler = new OidcClientInitiatedServerLogoutSuccessHandler(clientRegistrationRepository);
handler.setPostLogoutRedirectUri(postLogoutRedirectUri);
return handler;
}
final var registrationProperties = properties.getRegistration().get(clientRegistration.getRegistrationId());
if (registrationProperties == null) {
throw new MisconfigurationException(
"OAuth2 client registration \"%s\" has no end_session_endpoint in OpenID configuration nor spring-addons logout properties"
.formatted(clientRegistration.getRegistrationId()));
}
return new AlmostOidcClientInitiatedServerLogoutSuccessHandler(registrationProperties, clientRegistration, postLogoutRedirectUri);
}));
}
Expand All @@ -262,7 +268,7 @@ static class DelegatingOidcClientInitiatedServerLogoutSuccessHandler implements

}
```
This handler switches between Spring's `OidcClientInitiatedServerLogoutSuccessHandler` and our `AlmostOidcClientInitiatedServerLogoutSuccessHandler` depending on the configuration properties.
This handler switches between Spring's `OidcClientInitiatedServerLogoutSuccessHandler` (used if the `.well-known/openid-configuration` exposes an `end_session_endpont`) and our `AlmostOidcClientInitiatedServerLogoutSuccessHandler` (if the logout configuration properties are present, or throws an exception).

Last we need to update the security filter-chain to use the new `DelegatingOidcClientInitiatedServerLogoutSuccessHandler`:
```java
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.c4soft.springaddons.tutorials;

import static org.springframework.security.config.Customizer.withDefaults;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
Expand Down Expand Up @@ -45,12 +47,14 @@
import org.springframework.security.web.server.authentication.logout.RedirectServerLogoutSuccessHandler;
import org.springframework.security.web.server.authentication.logout.ServerLogoutSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import com.c4soft.springaddons.tutorials.WebSecurityConfig.AuthoritiesMappingProperties.MisconfigurationException;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;

Expand All @@ -69,7 +73,7 @@ SecurityWebFilterChain clientSecurityFilterChain(
InMemoryReactiveClientRegistrationRepository clientRegistrationRepository,
LogoutProperties logoutProperties) {
http.addFilterBefore(loginPageWebFilter(), SecurityWebFiltersOrder.LOGIN_PAGE_GENERATING);
http.oauth2Login();
http.oauth2Login(withDefaults());
http.logout(logout -> {
logout.logoutSuccessHandler(
new DelegatingOidcClientInitiatedServerLogoutSuccessHandler(clientRegistrationRepository, logoutProperties, "{baseUrl}"));
Expand Down Expand Up @@ -189,12 +193,18 @@ public DelegatingOidcClientInitiatedServerLogoutSuccessHandler(
String postLogoutRedirectUri) {
delegates = StreamSupport.stream(clientRegistrationRepository.spliterator(), false)
.collect(Collectors.toMap(ClientRegistration::getRegistrationId, clientRegistration -> {
final var registrationProperties = properties.getRegistration().get(clientRegistration.getRegistrationId());
if (registrationProperties == null) {
final var endSessionEnpoint = (String) (clientRegistration.getProviderDetails().getConfigurationMetadata().get("end_session_endpoint"));
if (StringUtils.hasText(endSessionEnpoint)) {
final var handler = new OidcClientInitiatedServerLogoutSuccessHandler(clientRegistrationRepository);
handler.setPostLogoutRedirectUri(postLogoutRedirectUri);
return handler;
}
final var registrationProperties = properties.getRegistration().get(clientRegistration.getRegistrationId());
if (registrationProperties == null) {
throw new MisconfigurationException(
"OAuth2 client registration \"%s\" has no end_session_endpoint in OpenID configuration nor spring-addons logout properties"
.formatted(clientRegistration.getRegistrationId()));
}
return new AlmostOidcClientInitiatedServerLogoutSuccessHandler(registrationProperties, clientRegistration, postLogoutRedirectUri);
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see ParameterizedBearerAuth
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see ParameterizedJwtAuth
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see ParameterizedOAuth2Login
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see ParameterizedOidcLogin
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see ParameterizedOpenId
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see BearerAuthenticationSource
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see JwtAuthenticationSource
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see OAuth2LoginAuthenticationSource
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see OidcLoginAuthenticationSource
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
* @author Jerome Wacongne ch4mp@c4-soft.com
* @see OpenIdAuthenticationSource
* @since 6.1.12
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.PARAMETER })
@Retention(RetentionPolicy.RUNTIME)
Expand Down

0 comments on commit 14b0d77

Please sign in to comment.