Skip to content

Commit 7b0d83d

Browse files
committed
Add DelegatingServerAuthenticationConverter
Closes spring-projectsgh-14644
1 parent c639d0a commit 7b0d83d

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-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,109 @@
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 org.springframework.http.HttpHeaders;
23+
import org.springframework.http.server.reactive.ServerHttpRequest;
24+
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
25+
import org.springframework.mock.web.server.MockServerWebExchange;
26+
import org.springframework.security.authentication.AbstractAuthenticationToken;
27+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
28+
import org.springframework.security.core.Authentication;
29+
import org.springframework.security.core.authority.AuthorityUtils;
30+
import org.springframework.util.ObjectUtils;
31+
import org.springframework.web.server.ServerWebExchange;
32+
import reactor.core.publisher.Mono;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* @author DingHao
38+
* @since 6.3
39+
*/
40+
@ExtendWith(MockitoExtension.class)
41+
public class DelegatingServerAuthenticationConverterTest {
42+
43+
DelegatingServerAuthenticationConverter converter = new DelegatingServerAuthenticationConverter(
44+
new ApkServerAuthenticationConverter(), new ServerHttpBasicAuthenticationConverter());
45+
46+
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/");
47+
48+
@Test
49+
public void applyServerHttpBasicAuthenticationConverter() {
50+
Mono<Authentication> result = this.converter.convert(MockServerWebExchange
51+
.from(this.request.header(HttpHeaders.AUTHORIZATION, "Basic dXNlcjpwYXNzd29yZA==").build()));
52+
UsernamePasswordAuthenticationToken authentication = result.cast(UsernamePasswordAuthenticationToken.class)
53+
.block();
54+
assertThat(authentication.getPrincipal()).isEqualTo("user");
55+
assertThat(authentication.getCredentials()).isEqualTo("password");
56+
}
57+
58+
@Test
59+
public void applyApkServerAuthenticationConverter() {
60+
String apk = "123e4567e89b12d3a456426655440000";
61+
Mono<Authentication> result = this.converter
62+
.convert(MockServerWebExchange.from(this.request.header("apk", apk).build()));
63+
ApkTokenAuthenticationToken authentication = result.cast(ApkTokenAuthenticationToken.class).block();
64+
assertThat(authentication.getApk()).isEqualTo(apk);
65+
}
66+
67+
private static String getApk(ServerHttpRequest request) {
68+
String apk = request.getHeaders().getFirst("APK");
69+
if (ObjectUtils.isEmpty(apk)) {
70+
apk = request.getQueryParams().getFirst("apk");
71+
}
72+
return apk;
73+
}
74+
75+
static class ApkServerAuthenticationConverter implements ServerAuthenticationConverter {
76+
77+
@Override
78+
public Mono<Authentication> convert(ServerWebExchange exchange) {
79+
return Mono.justOrEmpty(getApk(exchange.getRequest())).map(ApkTokenAuthenticationToken::new);
80+
}
81+
82+
}
83+
84+
static class ApkTokenAuthenticationToken extends AbstractAuthenticationToken {
85+
86+
private final String apk;
87+
88+
public ApkTokenAuthenticationToken(String apk) {
89+
super(AuthorityUtils.NO_AUTHORITIES);
90+
this.apk = apk;
91+
}
92+
93+
public String getApk() {
94+
return this.apk;
95+
}
96+
97+
@Override
98+
public Object getCredentials() {
99+
return this.getApk();
100+
}
101+
102+
@Override
103+
public Object getPrincipal() {
104+
return this.getApk();
105+
}
106+
107+
}
108+
109+
}

0 commit comments

Comments
 (0)