Skip to content

Commit d104490

Browse files
qavidrwinch
authored andcommitted
Resolve Bearer token after subscribing to publisher
Bearer token was resolved immediately after calling method convert. In situations when malformed token was provided or authorization header and access token query param were present in request exception was thrown instead of signalling error. After this change Bearer token is resolved on subscription and invalid states are handled by signaling error to subscriber. Closes gh-8865
1 parent c2612a2 commit d104490

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

oauth2/oauth2-resource-server/src/main/java/org/springframework/security/oauth2/server/resource/web/server/ServerBearerTokenAuthenticationConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class ServerBearerTokenAuthenticationConverter
5252
private boolean allowUriQueryParameter = false;
5353

5454
public Mono<Authentication> convert(ServerWebExchange exchange) {
55-
return Mono.justOrEmpty(token(exchange.getRequest()))
55+
return Mono.fromCallable(() -> token(exchange.getRequest()))
5656
.map(token -> {
5757
if (token.isEmpty()) {
5858
BearerTokenError error = invalidTokenError();

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/server/resource/web/server/ServerBearerTokenAuthenticationConverterTests.java

+11
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ public void resolveWhenHeaderWithInvalidCharactersIsPresentThenAuthenticationExc
131131
.hasMessageContaining(("Bearer token is malformed"));
132132
}
133133

134+
// gh-8865
135+
@Test
136+
public void resolveWhenHeaderWithInvalidCharactersIsPresentAndNotSubscribedThenNoneExceptionIsThrown() {
137+
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest
138+
.get("/")
139+
.header(HttpHeaders.AUTHORIZATION, "Bearer an\"invalid\"token");
140+
141+
assertThatCode(() -> this.converter.convert(MockServerWebExchange.from(request)))
142+
.doesNotThrowAnyException();
143+
}
144+
134145
@Test
135146
public void resolveWhenValidHeaderIsPresentTogetherWithQueryParameterThenAuthenticationExceptionIsThrown() {
136147
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest

0 commit comments

Comments
 (0)