Skip to content

Commit da4bd22

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 fd669f7 commit da4bd22

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class ServerBearerTokenAuthenticationConverter
5050
private boolean allowUriQueryParameter = false;
5151

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

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

Lines changed: 11 additions & 0 deletions
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)