Skip to content

Commit 5556b82

Browse files
DarrenForsythejzheaux
authored andcommitted
Check for multiple access tokens per rfc 6750
Check for multiple access tokens on the ServerHttpRequest rather than get get first. If multiples are found throw a OAuth2AuthenticationException. Closes spring-projectsgh-5708
1 parent 770c57e commit 5556b82

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.security.oauth2.server.resource.web.server;
1818

19+
import java.util.List;
1920
import java.util.regex.Matcher;
2021
import java.util.regex.Pattern;
2122

@@ -30,6 +31,7 @@
3031
import org.springframework.security.oauth2.server.resource.BearerTokenError;
3132
import org.springframework.security.oauth2.server.resource.BearerTokenErrors;
3233
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
34+
import org.springframework.util.CollectionUtils;
3335
import org.springframework.util.StringUtils;
3436
import org.springframework.web.server.ServerWebExchange;
3537

@@ -65,7 +67,8 @@ public Mono<Authentication> convert(ServerWebExchange exchange) {
6567

6668
private String token(ServerHttpRequest request) {
6769
String authorizationHeaderToken = resolveFromAuthorizationHeader(request.getHeaders());
68-
String parameterToken = request.getQueryParams().getFirst("access_token");
70+
String parameterToken = resolveAccessTokenFromRequest(request);
71+
6972
if (authorizationHeaderToken != null) {
7073
if (parameterToken != null) {
7174
BearerTokenError error = BearerTokenErrors
@@ -80,6 +83,20 @@ private String token(ServerHttpRequest request) {
8083
return null;
8184
}
8285

86+
private static String resolveAccessTokenFromRequest(ServerHttpRequest request) {
87+
List<String> parameterTokens = request.getQueryParams().get("access_token");
88+
if (CollectionUtils.isEmpty(parameterTokens)) {
89+
return null;
90+
}
91+
if (parameterTokens.size() == 1) {
92+
return parameterTokens.get(0);
93+
}
94+
95+
BearerTokenError error = BearerTokenErrors.invalidRequest("Found multiple bearer tokens in the request");
96+
throw new OAuth2AuthenticationException(error);
97+
98+
}
99+
83100
/**
84101
* Set if transport of access token using URI query parameter is supported. Defaults
85102
* to {@code false}.

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -203,6 +203,20 @@ public void resolveWhenQueryParameterIsPresentAndNotSupportedThenTokenIsNotResol
203203
assertThat(convertToToken(request)).isNull();
204204
}
205205

206+
@Test
207+
void resolveWhenQueryParameterHasMultipleAccessTokensThenOAuth2AuthenticationException() {
208+
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/").queryParam("access_token",
209+
TEST_TOKEN, TEST_TOKEN);
210+
assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> convertToToken(request))
211+
.satisfies((ex) -> {
212+
BearerTokenError error = (BearerTokenError) ex.getError();
213+
assertThat(error.getErrorCode()).isEqualTo(BearerTokenErrorCodes.INVALID_REQUEST);
214+
assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
215+
assertThat(error.getHttpStatus()).isEqualTo(HttpStatus.BAD_REQUEST);
216+
});
217+
218+
}
219+
206220
private BearerTokenAuthenticationToken convertToToken(MockServerHttpRequest.BaseBuilder<?> request) {
207221
return convertToToken(request.build());
208222
}

0 commit comments

Comments
 (0)