Skip to content

Commit 443adb1

Browse files
committed
Add DelegatingServerAuthenticationConverter
Closes gh-14644
1 parent c639d0a commit 443adb1

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
21+
import reactor.core.publisher.Flux;
22+
import reactor.core.publisher.Mono;
23+
24+
import org.springframework.security.core.Authentication;
25+
import org.springframework.util.Assert;
26+
import org.springframework.web.server.ServerWebExchange;
27+
28+
/**
29+
* A {@link ServerAuthenticationConverter} that delegates to other
30+
* {@link ServerAuthenticationConverter} instances.
31+
*
32+
* @author DingHao
33+
* @since 6.3
34+
*/
35+
public class DelegatingServerAuthenticationConverter implements ServerAuthenticationConverter {
36+
37+
private final List<ServerAuthenticationConverter> delegates;
38+
39+
public DelegatingServerAuthenticationConverter(ServerAuthenticationConverter... converters) {
40+
this(List.of(converters));
41+
}
42+
43+
public DelegatingServerAuthenticationConverter(List<ServerAuthenticationConverter> converters) {
44+
Assert.notEmpty(converters, "converters cannot be null");
45+
this.delegates = converters;
46+
}
47+
48+
@Override
49+
public Mono<Authentication> convert(ServerWebExchange exchange) {
50+
return Flux.fromIterable(this.delegates).concatMap(converter -> converter.convert(exchange)).next();
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
24+
import org.springframework.http.HttpHeaders;
25+
import org.springframework.http.server.reactive.ServerHttpRequest;
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.authority.AuthorityUtils;
32+
import org.springframework.util.ObjectUtils;
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+
public static class ApkServerAuthenticationConverter implements ServerAuthenticationConverter {
69+
70+
private static String getApk(ServerHttpRequest request) {
71+
String apk = request.getHeaders().getFirst("APK");
72+
if (ObjectUtils.isEmpty(apk)) {
73+
apk = request.getQueryParams().getFirst("apk");
74+
}
75+
return apk;
76+
}
77+
78+
@Override
79+
public Mono<Authentication> convert(ServerWebExchange exchange) {
80+
return Mono.justOrEmpty(getApk(exchange.getRequest())).map(ApkTokenAuthenticationToken::new);
81+
}
82+
83+
}
84+
85+
public static class ApkTokenAuthenticationToken extends AbstractAuthenticationToken {
86+
87+
private final String apk;
88+
89+
public ApkTokenAuthenticationToken(String apk) {
90+
super(AuthorityUtils.NO_AUTHORITIES);
91+
this.apk = apk;
92+
}
93+
94+
public String getApk() {
95+
return this.apk;
96+
}
97+
98+
@Override
99+
public Object getCredentials() {
100+
return this.getApk();
101+
}
102+
103+
@Override
104+
public Object getPrincipal() {
105+
return this.getApk();
106+
}
107+
108+
}
109+
110+
}

0 commit comments

Comments
 (0)