Skip to content

Commit 8885707

Browse files
kse-musicjzheaux
authored andcommitted
Add DelegatingServerAuthenticationConverter
Closes gh-14644
1 parent 69527f9 commit 8885707

File tree

2 files changed

+209
-0
lines changed

2 files changed

+209
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.server.authentication;
18+
19+
import java.util.List;
20+
import java.util.function.Function;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
import reactor.core.publisher.Flux;
25+
import reactor.core.publisher.Mono;
26+
27+
import org.springframework.security.core.Authentication;
28+
import org.springframework.util.Assert;
29+
import org.springframework.web.server.ServerWebExchange;
30+
31+
/**
32+
* A {@link ServerAuthenticationConverter} that delegates to other
33+
* {@link ServerAuthenticationConverter} instances.
34+
*
35+
* @author DingHao
36+
* @since 6.3
37+
*/
38+
public final class DelegatingServerAuthenticationConverter implements ServerAuthenticationConverter {
39+
40+
private final List<ServerAuthenticationConverter> delegates;
41+
42+
private boolean continueOnError = false;
43+
44+
private final Log logger = LogFactory.getLog(getClass());
45+
46+
public DelegatingServerAuthenticationConverter(ServerAuthenticationConverter... converters) {
47+
this(List.of(converters));
48+
}
49+
50+
public DelegatingServerAuthenticationConverter(List<ServerAuthenticationConverter> converters) {
51+
Assert.notEmpty(converters, "converters cannot be null");
52+
this.delegates = converters;
53+
}
54+
55+
@Override
56+
public Mono<Authentication> convert(ServerWebExchange exchange) {
57+
Flux<ServerAuthenticationConverter> result = Flux.fromIterable(this.delegates);
58+
Function<ServerAuthenticationConverter, Mono<Authentication>> logging = (
59+
converter) -> converter.convert(exchange).doOnError(this.logger::debug);
60+
return ((this.continueOnError) ? result.concatMapDelayError(logging) : result.concatMap(logging)).next();
61+
}
62+
63+
/**
64+
* Continue iterating when a delegate errors, defaults to {@code true}
65+
* @param continueOnError whether to continue when a delegate errors
66+
* @since 6.3
67+
*/
68+
public void setContinueOnError(boolean continueOnError) {
69+
this.continueOnError = continueOnError;
70+
}
71+
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.web.server.authentication;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.mockito.junit.jupiter.MockitoExtension;
22+
import reactor.core.publisher.Mono;
23+
import reactor.test.StepVerifier;
24+
25+
import org.springframework.http.HttpHeaders;
26+
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
27+
import org.springframework.mock.web.server.MockServerWebExchange;
28+
import org.springframework.security.authentication.AbstractAuthenticationToken;
29+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
30+
import org.springframework.security.core.Authentication;
31+
import org.springframework.security.core.AuthenticationException;
32+
import org.springframework.security.core.authority.AuthorityUtils;
33+
import org.springframework.web.server.ServerWebExchange;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* @author DingHao
39+
* @since 6.3
40+
*/
41+
@ExtendWith(MockitoExtension.class)
42+
public class DelegatingServerAuthenticationConverterTests {
43+
44+
DelegatingServerAuthenticationConverter converter = new DelegatingServerAuthenticationConverter(
45+
new ApkServerAuthenticationConverter(), new ServerHttpBasicAuthenticationConverter());
46+
47+
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/");
48+
49+
@Test
50+
public void applyServerHttpBasicAuthenticationConverter() {
51+
Mono<Authentication> result = this.converter.convert(MockServerWebExchange
52+
.from(this.request.header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA==").build()));
53+
UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class)
54+
.block();
55+
assertThat(authentication.getPrincipal()).isEqualTo("user");
56+
assertThat(authentication.getCredentials()).isEqualTo("password");
57+
}
58+
59+
@Test
60+
public void applyApkServerAuthenticationConverter() {
61+
String apk = "123e4567e89b12d3a456426655440000";
62+
Mono<Authentication> result = this.converter
63+
.convert(MockServerWebExchange.from(this.request.header("APK", apk).build()));
64+
ApkTokenAuthenticationToken authentication = result.cast(ApkTokenAuthenticationToken.class).block();
65+
assertThat(authentication.getApk()).isEqualTo(apk);
66+
}
67+
68+
@Test
69+
public void applyServerHttpBasicAuthenticationConverterWhenFirstAuthenticationConverterException() {
70+
this.converter.setContinueOnError(true);
71+
Mono<Authentication> result = this.converter.convert(MockServerWebExchange.from(this.request.header("APK", "")
72+
.header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA==")
73+
.build()));
74+
UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class)
75+
.block();
76+
assertThat(authentication.getPrincipal()).isEqualTo("user");
77+
assertThat(authentication.getCredentials()).isEqualTo("password");
78+
}
79+
80+
@Test
81+
public void applyApkServerAuthenticationConverterThenDelegate2NotInvokedAndError() {
82+
Mono<Authentication> result = this.converter.convert(MockServerWebExchange.from(this.request.header("APK", "")
83+
.header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA==")
84+
.build()));
85+
StepVerifier.create(result.cast(ApkTokenAuthenticationToken.class))
86+
.expectError(ApkAuthenticationException.class)
87+
.verify();
88+
}
89+
90+
public static class ApkServerAuthenticationConverter implements ServerAuthenticationConverter {
91+
92+
@Override
93+
public Mono<Authentication> convert(ServerWebExchange exchange) {
94+
return Mono.fromCallable(() -> exchange.getRequest().getHeaders().getFirst("APK")).map((apk) -> {
95+
if (apk.isEmpty()) {
96+
throw new ApkAuthenticationException("apk invalid");
97+
}
98+
return new ApkTokenAuthenticationToken(apk);
99+
});
100+
}
101+
102+
}
103+
104+
public static class ApkTokenAuthenticationToken extends AbstractAuthenticationToken {
105+
106+
private final String apk;
107+
108+
public ApkTokenAuthenticationToken(String apk) {
109+
super(AuthorityUtils.NO_AUTHORITIES);
110+
this.apk = apk;
111+
}
112+
113+
public String getApk() {
114+
return this.apk;
115+
}
116+
117+
@Override
118+
public Object getCredentials() {
119+
return this.getApk();
120+
}
121+
122+
@Override
123+
public Object getPrincipal() {
124+
return this.getApk();
125+
}
126+
127+
}
128+
129+
public static class ApkAuthenticationException extends AuthenticationException {
130+
131+
public ApkAuthenticationException(String msg) {
132+
super(msg);
133+
}
134+
135+
}
136+
137+
}

0 commit comments

Comments
 (0)