Skip to content

Commit e6ac975

Browse files
elefeintjzheaux
authored andcommitted
Extract bearer token from arbitrary header.
1 parent cf0c5f9 commit e6ac975

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2019 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 javax.servlet.http.HttpServletRequest;
20+
import org.springframework.util.Assert;
21+
22+
/**
23+
* Generic resolver extracting pre-authenticated JWT identity from a custom header.
24+
*
25+
* @author Elena Felder
26+
* @since 5.2
27+
*/
28+
public class HeaderBearerTokenResolver implements BearerTokenResolver {
29+
30+
private String header;
31+
32+
public HeaderBearerTokenResolver(String header) {
33+
Assert.hasText(header, "header cannot be empty");
34+
this.header = header;
35+
}
36+
37+
@Override
38+
public String resolve(HttpServletRequest request) {
39+
return request.getHeader(this.header);
40+
}
41+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2002-2019 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.Test;
20+
21+
import org.springframework.mock.web.MockHttpServletRequest;
22+
23+
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatCode;
25+
26+
/**
27+
* Tests for {@link HeaderBearerTokenResolver}
28+
*
29+
* @author Elena Felder
30+
*/
31+
public class HeaderBearerTokenResolverTests {
32+
33+
private static final String TEST_TOKEN = "test-token";
34+
35+
private static final String CORRECT_HEADER = "jwt-assertion";
36+
37+
private HeaderBearerTokenResolver resolver = new HeaderBearerTokenResolver(CORRECT_HEADER);
38+
39+
@Test
40+
public void constructorWhenHeaderNullThenThrowIllegalArgumentException() {
41+
assertThatCode(() -> { new HeaderBearerTokenResolver(null); })
42+
.isInstanceOf(IllegalArgumentException.class)
43+
.hasMessage("header cannot be empty");
44+
}
45+
46+
@Test
47+
public void constructorWhenHeaderEmptyThenThrowIllegalArgumentException() {
48+
assertThatCode(() -> { new HeaderBearerTokenResolver(""); })
49+
.isInstanceOf(IllegalArgumentException.class)
50+
.hasMessage("header cannot be empty");
51+
}
52+
53+
@Test
54+
public void resolveWhenTokenPresentThenTokenIsResolved() {
55+
MockHttpServletRequest request = new MockHttpServletRequest();
56+
request.addHeader(CORRECT_HEADER, TEST_TOKEN);
57+
58+
assertThat(this.resolver.resolve(request)).isEqualTo(TEST_TOKEN);
59+
}
60+
61+
@Test
62+
public void resolveWhenTokenNotPresentThenTokenIsNotResolved() {
63+
MockHttpServletRequest request = new MockHttpServletRequest();
64+
65+
assertThat(this.resolver.resolve(request)).isNull();
66+
}
67+
}

0 commit comments

Comments
 (0)