Skip to content

Commit d14b2c4

Browse files
committed
make Java 8 compliant
1 parent bbe31b5 commit d14b2c4

File tree

2 files changed

+41
-33
lines changed

2 files changed

+41
-33
lines changed

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,15 @@ private Mono<String> token(ServerWebExchange exchange) {
8282
resolveAccessTokenFromRequest(request).map(s -> Tuples.of(s, TokenSource.QUERY_PARAMETER)),
8383
resolveAccessTokenFromBody(exchange).map(s -> Tuples.of(s, TokenSource.BODY_PARAMETER)))
8484
.collectList()
85-
.mapNotNull(tokenTuples -> switch (tokenTuples.size()) {
86-
case 0 -> null;
87-
case 1 -> getTokenIfSupported(tokenTuples.get(0), request);
88-
default -> {
89-
BearerTokenError error = invalidRequest(MULTIPLE_BEARER_TOKENS_ERROR_MSG);
90-
throw new OAuth2AuthenticationException(error);
85+
.mapNotNull(tokenTuples -> {
86+
switch (tokenTuples.size()) {
87+
case 0:
88+
return null;
89+
case 1:
90+
return getTokenIfSupported(tokenTuples.get(0), request);
91+
default:
92+
BearerTokenError error = invalidRequest(MULTIPLE_BEARER_TOKENS_ERROR_MSG);
93+
throw new OAuth2AuthenticationException(error);
9194
}
9295
});
9396
}
@@ -107,11 +110,16 @@ private static Mono<String> resolveAccessTokenFromRequest(ServerHttpRequest requ
107110
}
108111

109112
private String getTokenIfSupported(Tuple2<String, TokenSource> tokenTuple, ServerHttpRequest request) {
110-
return switch (tokenTuple.getT2()) {
111-
case HEADER -> tokenTuple.getT1();
112-
case QUERY_PARAMETER -> isParameterTokenSupportedForRequest(request) ? tokenTuple.getT1() : null;
113-
case BODY_PARAMETER -> isBodyParameterTokenSupportedForRequest(request) ? tokenTuple.getT1() : null;
114-
};
113+
switch (tokenTuple.getT2()) {
114+
case HEADER:
115+
return tokenTuple.getT1();
116+
case QUERY_PARAMETER:
117+
return isParameterTokenSupportedForRequest(request) ? tokenTuple.getT1() : null;
118+
case BODY_PARAMETER:
119+
return isBodyParameterTokenSupportedForRequest(request) ? tokenTuple.getT1() : null;
120+
default:
121+
throw new IllegalArgumentException();
122+
}
115123
}
116124

117125
/**
@@ -185,7 +193,7 @@ private Mono<String> resolveAccessTokenFromBody(ServerWebExchange exchange) {
185193
return null;
186194
}
187195
if (tokens.size() > 1) {
188-
var error = invalidRequest(MULTIPLE_BEARER_TOKENS_ERROR_MSG);
196+
BearerTokenError error = invalidRequest(MULTIPLE_BEARER_TOKENS_ERROR_MSG);
189197
throw new OAuth2AuthenticationException(error);
190198
}
191199
return formData.getFirst(ACCESS_TOKEN_NAME);

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ void resolveWhenQueryParameterHasMultipleAccessTokensThenOAuth2AuthenticationExc
222222
@Test
223223
void resolveWhenBodyParameterIsPresentThenTokenIsResolved() {
224224
this.converter.setAllowFormEncodedBodyParameter(true);
225-
var request = post("/").contentType(APPLICATION_FORM_URLENCODED)
226-
.body("access_token=" + TEST_TOKEN);
225+
MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
226+
.body("access_token=" + TEST_TOKEN);
227227

228228
assertThat(convertToToken(request).getToken()).isEqualTo(TEST_TOKEN);
229229
}
@@ -232,17 +232,17 @@ void resolveWhenBodyParameterIsPresentThenTokenIsResolved() {
232232
@Test
233233
void resolveWhenBodyParameterIsPresentButNotAllowedThenTokenIsNotResolved() {
234234
this.converter.setAllowFormEncodedBodyParameter(false);
235-
var request = post("/").contentType(APPLICATION_FORM_URLENCODED)
236-
.body("access_token=" + TEST_TOKEN);
235+
MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
236+
.body("access_token=" + TEST_TOKEN);
237237

238238
assertThat(convertToToken(request)).isNull();
239239
}
240240

241241
@Test
242242
void resolveWhenBodyParameterHasMultipleAccessTokensThenOAuth2AuthenticationException() {
243243
this.converter.setAllowFormEncodedBodyParameter(true);
244-
var request = post("/").contentType(APPLICATION_FORM_URLENCODED)
245-
.body("access_token=" + TEST_TOKEN + "&access_token=" + TEST_TOKEN);
244+
MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
245+
.body("access_token=" + TEST_TOKEN + "&access_token=" + TEST_TOKEN);
246246

247247
assertThatExceptionOfType(OAuth2AuthenticationException.class)
248248
.isThrownBy(() -> convertToToken(request))
@@ -258,35 +258,35 @@ void resolveWhenBodyParameterHasMultipleAccessTokensThenOAuth2AuthenticationExce
258258
@Test
259259
void resolveBodyContainsOtherParameterAsWellThenTokenIsResolved() {
260260
this.converter.setAllowFormEncodedBodyParameter(true);
261-
var request = post("/").contentType(APPLICATION_FORM_URLENCODED)
262-
.body("access_token=" + TEST_TOKEN + "&other_param=value");
261+
MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
262+
.body("access_token=" + TEST_TOKEN + "&other_param=value");
263263

264264
assertThat(convertToToken(request).getToken()).isEqualTo(TEST_TOKEN);
265265
}
266266

267267
@Test
268268
void resolveWhenNoBodyParameterThenTokenIsNotResolved() {
269269
this.converter.setAllowFormEncodedBodyParameter(true);
270-
var request = post("/").contentType(APPLICATION_FORM_URLENCODED);
270+
MockServerHttpRequest.BaseBuilder<?> request = post("/").contentType(APPLICATION_FORM_URLENCODED);
271271

272272
assertThat(convertToToken(request)).isNull();
273273
}
274274

275275
@Test
276276
void resolveWhenWrongBodyParameterThenTokenIsNotResolved() {
277277
this.converter.setAllowFormEncodedBodyParameter(true);
278-
var request = post("/").contentType(APPLICATION_FORM_URLENCODED)
279-
.body("other_param=value");
278+
MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
279+
.body("other_param=value");
280280

281281
assertThat(convertToToken(request)).isNull();
282282
}
283283

284284
@Test
285285
void resolveWhenValidHeaderIsPresentTogetherWithBodyParameterThenAuthenticationExceptionIsThrown() {
286286
this.converter.setAllowFormEncodedBodyParameter(true);
287-
var request = post("/").header(AUTHORIZATION, "Bearer " + TEST_TOKEN)
288-
.contentType(APPLICATION_FORM_URLENCODED)
289-
.body("access_token=" + TEST_TOKEN);
287+
MockServerHttpRequest request = post("/").header(AUTHORIZATION, "Bearer " + TEST_TOKEN)
288+
.contentType(APPLICATION_FORM_URLENCODED)
289+
.body("access_token=" + TEST_TOKEN);
290290

291291
assertThatExceptionOfType(OAuth2AuthenticationException.class)
292292
.isThrownBy(() -> convertToToken(request))
@@ -297,9 +297,9 @@ void resolveWhenValidHeaderIsPresentTogetherWithBodyParameterThenAuthenticationE
297297
void resolveWhenValidQueryParameterIsPresentTogetherWithBodyParameterThenAuthenticationExceptionIsThrown() {
298298
this.converter.setAllowUriQueryParameter(true);
299299
this.converter.setAllowFormEncodedBodyParameter(true);
300-
var request = post("/").queryParam("access_token", TEST_TOKEN)
301-
.contentType(APPLICATION_FORM_URLENCODED)
302-
.body("access_token=" + TEST_TOKEN);
300+
MockServerHttpRequest request = post("/").queryParam("access_token", TEST_TOKEN)
301+
.contentType(APPLICATION_FORM_URLENCODED)
302+
.body("access_token=" + TEST_TOKEN);
303303

304304
assertThatExceptionOfType(OAuth2AuthenticationException.class)
305305
.isThrownBy(() -> convertToToken(request))
@@ -310,10 +310,10 @@ void resolveWhenValidQueryParameterIsPresentTogetherWithBodyParameterThenAuthent
310310
void resolveWhenValidQueryParameterIsPresentTogetherWithBodyParameterAndValidHeaderThenAuthenticationExceptionIsThrown() {
311311
this.converter.setAllowUriQueryParameter(true);
312312
this.converter.setAllowFormEncodedBodyParameter(true);
313-
var request = post("/").header(AUTHORIZATION, "Bearer " + TEST_TOKEN)
314-
.queryParam("access_token", TEST_TOKEN)
315-
.contentType(APPLICATION_FORM_URLENCODED)
316-
.body("access_token=" + TEST_TOKEN);
313+
MockServerHttpRequest request = post("/").header(AUTHORIZATION, "Bearer " + TEST_TOKEN)
314+
.queryParam("access_token", TEST_TOKEN)
315+
.contentType(APPLICATION_FORM_URLENCODED)
316+
.body("access_token=" + TEST_TOKEN);
317317

318318
assertThatExceptionOfType(OAuth2AuthenticationException.class)
319319
.isThrownBy(() -> convertToToken(request))

0 commit comments

Comments
 (0)