Skip to content

Commit 4f2cf89

Browse files
ruabtmhruabtmh
ruabtmh
authored and
ruabtmh
committed
Added DelegatingBearerTokenResolver
Closes gh-14644
1 parent e771267 commit 4f2cf89

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.oauth2.server.resource.web;
18+
19+
import java.util.List;
20+
import java.util.Objects;
21+
22+
import jakarta.servlet.http.HttpServletRequest;
23+
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* A {@link BearerTokenResolver}, that iterates over multiple {@link BearerTokenResolver}.
28+
*
29+
* @author Max Batischev
30+
* @since 6.3
31+
*/
32+
public class DelegatingBearerTokenResolver implements BearerTokenResolver {
33+
34+
private final List<BearerTokenResolver> delegates;
35+
36+
public DelegatingBearerTokenResolver(List<BearerTokenResolver> delegates) {
37+
Assert.notEmpty(delegates, "delegates cannot be null");
38+
this.delegates = delegates;
39+
}
40+
41+
public DelegatingBearerTokenResolver(BearerTokenResolver... delegates) {
42+
this.delegates = List.of(delegates);
43+
}
44+
45+
@Override
46+
public String resolve(HttpServletRequest request) {
47+
return (this.delegates).stream().map((d) -> d.resolve(request)).filter(Objects::nonNull).findAny().orElse(null);
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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.oauth2.server.resource.web;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.http.HttpHeaders;
22+
import org.springframework.mock.web.MockHttpServletRequest;
23+
24+
import static org.assertj.core.api.Assertions.assertThat;
25+
26+
/**
27+
* Tests for {@link DelegatingBearerTokenResolver}.
28+
*
29+
* @author Max Batischev
30+
*/
31+
public class DelegatingBearerTokenResolverTests {
32+
33+
private static final String X_AUTH_TOKEN_HEADER = "X-Auth-Token";
34+
35+
private static final String TEST_BEARER_TOKEN = "test-bearer-token";
36+
37+
private static final String TEST_X_AUTH_TOKEN = "test-x-auth-token";
38+
39+
private static final String X_AUTHORIZATION_HEADER = "x-authorization";
40+
41+
private static final String X_AUTHORIZATION_TOKEN = "test-x-authorization-token";
42+
43+
@Test
44+
public void resolveWhenBearerAuthorizationHeaderIsPresentThenBearerTokenIsResolved() {
45+
MockHttpServletRequest request = new MockHttpServletRequest();
46+
request.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + TEST_BEARER_TOKEN);
47+
48+
DelegatingBearerTokenResolver bearerTokenResolver = resolverWithTwoDelegates();
49+
50+
assertThat(bearerTokenResolver.resolve(request)).isEqualTo(TEST_BEARER_TOKEN);
51+
}
52+
53+
@Test
54+
public void resolveWhenXAuthTokenHeaderIsPresentThenXAuthTokenIsResolved() {
55+
MockHttpServletRequest request = new MockHttpServletRequest();
56+
request.addHeader(X_AUTH_TOKEN_HEADER, TEST_X_AUTH_TOKEN);
57+
58+
DelegatingBearerTokenResolver bearerTokenResolver = resolverWithTwoDelegates();
59+
60+
assertThat(bearerTokenResolver.resolve(request)).isEqualTo(TEST_X_AUTH_TOKEN);
61+
}
62+
63+
@Test
64+
public void resolveWhenXAuthorizationHeaderIsPresentThenTokenIsNotResolved() {
65+
MockHttpServletRequest request = new MockHttpServletRequest();
66+
request.addHeader(X_AUTHORIZATION_HEADER, X_AUTHORIZATION_TOKEN);
67+
68+
DelegatingBearerTokenResolver bearerTokenResolver = resolverWithTwoDelegates();
69+
70+
assertThat(bearerTokenResolver.resolve(request)).isNull();
71+
}
72+
73+
private DelegatingBearerTokenResolver resolverWithTwoDelegates() {
74+
return new DelegatingBearerTokenResolver(new DefaultBearerTokenResolver(),
75+
new HeaderBearerTokenResolver(X_AUTH_TOKEN_HEADER));
76+
}
77+
78+
}

0 commit comments

Comments
 (0)