Skip to content
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

Add DelegatingServerLogoutSuccessHandler. #14814

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,57 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.security.web.server.authentication.logout;

import java.util.ArrayList;
import java.util.List;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;
import org.springframework.util.Assert;

/**
* A {@link ServerLogoutSuccessHandler}, that iterates over multiple
* {@link ServerLogoutSuccessHandler}.
*
* @author Max Batischev
* @since 6.3
*/
public final class DelegatingServerLogoutSuccessHandler implements ServerLogoutSuccessHandler {

private final List<ServerLogoutSuccessHandler> delegates;

public DelegatingServerLogoutSuccessHandler(List<ServerLogoutSuccessHandler> delegates) {
Assert.notEmpty(delegates, "delegates cannot be null");
this.delegates = new ArrayList<>(delegates);
}

public DelegatingServerLogoutSuccessHandler(ServerLogoutSuccessHandler... delegates) {
Assert.notEmpty(delegates, "delegates cannot be null");
this.delegates = List.of(delegates);
}

@Override
public Mono<Void> onLogoutSuccess(WebFilterExchange exchange, Authentication authentication) {
return Flux.fromIterable(this.delegates)
.concatMap((delegate) -> delegate.onLogoutSuccess(exchange, authentication))
.then();
}

}
@@ -0,0 +1,83 @@
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.security.web.server.authentication.logout;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import reactor.test.publisher.PublisherProbe;

import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.WebFilterExchange;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;

/**
* @author Max Batischev
* @since 6.3
*/
@ExtendWith(MockitoExtension.class)
public class DelegatingServerLogoutSuccessHandlerTests {

@Mock
private ServerLogoutSuccessHandler delegate1;

@Mock
private ServerLogoutSuccessHandler delegate2;

private final PublisherProbe<Void> delegate1Result = PublisherProbe.empty();

private final PublisherProbe<Void> delegate2Result = PublisherProbe.empty();

@Mock
private WebFilterExchange exchange;

@Mock
private Authentication authentication;

private DelegatingServerLogoutSuccessHandler logoutSuccessHandler;

@Test
public void logoutWhenDelegate1AndDelegate2PresentThenExecuted() {
given(this.delegate1.onLogoutSuccess(any(WebFilterExchange.class), any(Authentication.class)))
.willReturn(this.delegate1Result.mono());
given(this.delegate2.onLogoutSuccess(any(WebFilterExchange.class), any(Authentication.class)))
.willReturn(this.delegate2Result.mono());
this.logoutSuccessHandler = new DelegatingServerLogoutSuccessHandler(this.delegate1, this.delegate2);

this.logoutSuccessHandler.onLogoutSuccess(this.exchange, this.authentication).block();

this.delegate1Result.assertWasSubscribed();
this.delegate2Result.assertWasSubscribed();
}

@Test
public void logoutWhenDelegate1PresentThenExecuted() {
given(this.delegate1.onLogoutSuccess(any(WebFilterExchange.class), any(Authentication.class)))
.willReturn(this.delegate1Result.mono());
this.logoutSuccessHandler = new DelegatingServerLogoutSuccessHandler(List.of(this.delegate1));

this.logoutSuccessHandler.onLogoutSuccess(this.exchange, this.authentication).block();

this.delegate1Result.assertWasSubscribed();
}

}