Skip to content

Simplify OIDC Back-Channel Logout DSL (Closes gh-15817) #16698

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;


/**
* A {@link HttpSecurity} is similar to Spring Security's XML <http> element in the
* namespace configuration. It allows configuring web based security for specific http
Expand Down Expand Up @@ -2870,6 +2871,32 @@ public HttpSecurity oidcLogout(Customizer<OidcLogoutConfigurer<HttpSecurity>> oi
return HttpSecurity.this;
}

/**
* Configures OpenID Connect (OIDC) Back-Channel Logout support.
*
* <p>This method enables the configuration of OIDC Back-Channel Logout by applying
* the provided {@link Customizer} to an instance of {@link OidcLogoutConfigurer}. It
* initializes the back-channel logout support with default settings, making it easier
* to integrate with other logout configurations.
*
* <p>For example, to enable OIDC Back-Channel Logout with default settings:
* <pre>
* http.oidcBackChannelLogout(Customizer.withDefaults());
* </pre>
*
* @param oidcBackChannelLogoutCustomizer the customizer to configure OIDC Back-Channel Logout options
* @return the {@code HttpSecurity} instance for further customizations
* @throws Exception if an error occurs during configuration
* @since 6.5
*/
public HttpSecurity oidcBackChannelLogout(Customizer<OidcLogoutConfigurer<HttpSecurity>> oidcBackChannelLogoutCustomizer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add JavaDoc so folks can read how to use the method. Please make sure the JavaDoc includes @since 6.5.

throws Exception {
oidcBackChannelLogoutCustomizer.customize(
getOrApply(new OidcLogoutConfigurer<>()).backChannel(Customizer.withDefaults())
);
return this;
}

/**
* Configures OAuth 2.0 Client support.
* @return the {@link OAuth2ClientConfigurer} for further customizations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@

package org.springframework.security.config.annotation.web.configurers.oauth2.client;

import java.util.function.Consumer;
import java.util.function.Function;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
Expand All @@ -40,6 +36,9 @@
import org.springframework.security.web.csrf.CsrfFilter;
import org.springframework.util.Assert;

import java.util.function.Consumer;
import java.util.function.Function;

/**
* An {@link AbstractHttpConfigurer} for OIDC Logout flows
*
Expand Down Expand Up @@ -102,7 +101,10 @@ public OidcLogoutConfigurer<B> oidcSessionRegistry(OidcSessionRegistry oidcSessi
/**
* Configure OIDC Back-Channel Logout using the provided {@link Consumer}
* @return the {@link OidcLogoutConfigurer} for further configuration
* @deprecated For removal in a future release. Use
* {@link HttpSecurity#oidcBackChannelLogout(Customizer)} instead.
*/
@Deprecated(since = "6.5", forRemoval = true)
public OidcLogoutConfigurer<B> backChannel(Customizer<BackChannelLogoutConfigurer> backChannelLogoutConfigurer) {
if (this.backChannel == null) {
this.backChannel = new BackChannelLogoutConfigurer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,24 @@ void logoutWhenProviderIssuerMissingThenThrowIllegalArgumentException() throws E
.param("logout_token", logoutToken)));
}

@Test
void oidcBackChannelLogoutWhenDefaultsThenRemotelyInvalidatesSessions() throws Exception {
this.spring.register(WebServerConfig.class, OidcProviderConfig.class, WithOidcBackChannelDslConfig.class)
.autowire();
String registrationId = this.clientRegistration.getRegistrationId();
MockHttpSession session = login();
String logoutToken = this.mvc.perform(get("/token/logout").session(session))
.andExpect(status().isOk())
.andReturn()
.getResponse()
.getContentAsString();
this.mvc.perform(post(this.web.url("/logout/connect/back-channel/" + registrationId).toString())
.param("logout_token", logoutToken))
.andExpect(status().isOk());
this.mvc.perform(get("/token/logout").session(session))
.andExpect(status().isUnauthorized());
}

private MockHttpSession login() throws Exception {
MockMvcDispatcher dispatcher = (MockMvcDispatcher) this.web.getDispatcher();
this.mvc.perform(get("/token/logout")).andExpect(status().isUnauthorized());
Expand Down Expand Up @@ -739,6 +757,23 @@ void shutdown() throws IOException {

}

@Configuration
@EnableWebSecurity
@Import(RegistrationConfig.class)
static class WithOidcBackChannelDslConfig {

@Bean
@Order(1)
SecurityFilterChain filters(HttpSecurity http) throws Exception {
http
.authorizeHttpRequests((authorize) -> authorize.anyRequest().authenticated())
.oauth2Login(Customizer.withDefaults())
.oidcBackChannelLogout(Customizer.withDefaults());
return http.build();
}

}

private static class MockMvcDispatcher extends Dispatcher {

private final Map<String, MockHttpSession> session = new ConcurrentHashMap<>();
Expand Down