|
@@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test;
|
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
|
|
|
import org.springframework.mock.web.server.MockServerWebExchange;
|
|
|
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;
|
|
@@ -32,9 +33,6 @@ import org.springframework.security.oauth2.server.resource.authentication.Bearer
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
|
|
-import static org.springframework.http.HttpHeaders.AUTHORIZATION;
|
|
|
-import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED;
|
|
|
-import static org.springframework.mock.http.server.reactive.MockServerHttpRequest.post;
|
|
|
|
|
|
/**
|
|
|
* @author Rob Winch
|
|
@@ -159,6 +157,7 @@ public class ServerBearerTokenAuthenticationConverterTests {
|
|
|
|
|
|
@Test
|
|
|
public void resolveWhenValidHeaderIsPresentTogetherWithQueryParameterThenAuthenticationExceptionIsThrown() {
|
|
|
+ this.converter.setAllowUriQueryParameter(true);
|
|
|
// @formatter:off
|
|
|
this.converter.setAllowUriQueryParameter(true);
|
|
|
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/")
|
|
@@ -170,6 +169,54 @@ public class ServerBearerTokenAuthenticationConverterTests {
|
|
|
// @formatter:on
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void resolveWhenValidHeaderIsPresentTogetherWithBodyParameterThenAuthenticationExceptionIsThrown() {
|
|
|
+ this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
|
|
+ .header(HttpHeaders.AUTHORIZATION, "Bearer " + TEST_TOKEN)
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
|
+ .body("access_token=" + TEST_TOKEN);
|
|
|
+
|
|
|
+ assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> convertToToken(request))
|
|
|
+ .withMessageContaining("Found multiple bearer tokens in the request");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void resolveWhenValidHeaderIsPresentTogetherWithBodyParameterAndQueryParameterThenAuthenticationExceptionIsThrown() {
|
|
|
+ this.converter.setAllowUriQueryParameter(true);
|
|
|
+ this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
|
|
+ .header(HttpHeaders.AUTHORIZATION, "Bearer " + TEST_TOKEN)
|
|
|
+ .queryParam("access_token", TEST_TOKEN)
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
|
+ .body("access_token=" + TEST_TOKEN);
|
|
|
+
|
|
|
+ assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> convertToToken(request))
|
|
|
+ .withMessageContaining("Found multiple bearer tokens in the request");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void resolveWhenRequestContainsTwoAccessTokenQueryParametersThenAuthenticationExceptionIsThrown() {
|
|
|
+ this.converter.setAllowUriQueryParameter(true);
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.get("/")
|
|
|
+ .queryParam("access_token", "token1", "token2")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> convertToToken(request))
|
|
|
+ .withMessageContaining("Found multiple bearer tokens in the request");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void resolveWhenRequestContainsTwoAccessTokenBodyParametersThenAuthenticationExceptionIsThrown() {
|
|
|
+ this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
|
+ .body("access_token=" + TEST_TOKEN + "&access_token=" + TEST_TOKEN);
|
|
|
+
|
|
|
+ assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> convertToToken(request))
|
|
|
+ .withMessageContaining("Found multiple bearer tokens in the request");
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void resolveWhenQueryParameterIsPresentAndSupportedThenTokenIsResolved() {
|
|
|
this.converter.setAllowUriQueryParameter(true);
|
|
@@ -208,7 +255,7 @@ public class ServerBearerTokenAuthenticationConverterTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void resolveWhenQueryParameterHasMultipleAccessTokensThenOAuth2AuthenticationException() {
|
|
|
+ public void resolveWhenQueryParameterHasMultipleAccessTokensThenOAuth2AuthenticationException() {
|
|
|
this.converter.setAllowUriQueryParameter(true);
|
|
|
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/")
|
|
|
.queryParam("access_token", TEST_TOKEN, TEST_TOKEN);
|
|
@@ -223,109 +270,74 @@ public class ServerBearerTokenAuthenticationConverterTests {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void resolveWhenBodyParameterIsPresentThenTokenIsResolved() {
|
|
|
+ public void resolveWhenBodyParameterIsPresentThenTokenIsResolved() {
|
|
|
this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
- MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
|
|
|
- .body("access_token=" + TEST_TOKEN);
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
|
+ .body("access_token=" + TEST_TOKEN);
|
|
|
|
|
|
assertThat(convertToToken(request).getToken()).isEqualTo(TEST_TOKEN);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@Test
|
|
|
- void resolveWhenBodyParameterIsPresentButNotAllowedThenTokenIsNotResolved() {
|
|
|
+ public void resolveWhenBodyParameterIsPresentButNotAllowedThenTokenIsNotResolved() {
|
|
|
this.converter.setAllowFormEncodedBodyParameter(false);
|
|
|
- MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
|
|
|
- .body("access_token=" + TEST_TOKEN);
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
|
+ .body("access_token=" + TEST_TOKEN);
|
|
|
|
|
|
assertThat(convertToToken(request)).isNull();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void resolveWhenBodyParameterHasMultipleAccessTokensThenOAuth2AuthenticationException() {
|
|
|
+ public void resolveWhenBodyParameterHasMultipleAccessTokensThenOAuth2AuthenticationException() {
|
|
|
this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
- MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
|
|
|
- .body("access_token=" + TEST_TOKEN + "&access_token=" + TEST_TOKEN);
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
|
+ .body("access_token=" + TEST_TOKEN + "&access_token=" + TEST_TOKEN);
|
|
|
|
|
|
- assertThatExceptionOfType(OAuth2AuthenticationException.class)
|
|
|
- .isThrownBy(() -> convertToToken(request))
|
|
|
- .satisfies(ex -> {
|
|
|
- BearerTokenError error = (BearerTokenError) ex.getError();
|
|
|
- assertThat(error.getDescription()).isEqualTo("Found multiple bearer tokens in the request");
|
|
|
- assertThat(error.getErrorCode()).isEqualTo(BearerTokenErrorCodes.INVALID_REQUEST);
|
|
|
- assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
|
|
|
- assertThat(error.getHttpStatus()).isEqualTo(HttpStatus.BAD_REQUEST);
|
|
|
- });
|
|
|
+ assertThatExceptionOfType(OAuth2AuthenticationException.class).isThrownBy(() -> convertToToken(request))
|
|
|
+ .satisfies((ex) -> {
|
|
|
+ BearerTokenError error = (BearerTokenError) ex.getError();
|
|
|
+ assertThat(error.getDescription()).isEqualTo("Found multiple bearer tokens in the request");
|
|
|
+ assertThat(error.getErrorCode()).isEqualTo(BearerTokenErrorCodes.INVALID_REQUEST);
|
|
|
+ assertThat(error.getUri()).isEqualTo("https://tools.ietf.org/html/rfc6750#section-3.1");
|
|
|
+ assertThat(error.getHttpStatus()).isEqualTo(HttpStatus.BAD_REQUEST);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void resolveBodyContainsOtherParameterAsWellThenTokenIsResolved() {
|
|
|
+ public void resolveBodyContainsOtherParameterAsWellThenTokenIsResolved() {
|
|
|
this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
- MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
|
|
|
- .body("access_token=" + TEST_TOKEN + "&other_param=value");
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
|
+ .body("access_token=" + TEST_TOKEN + "&other_param=value");
|
|
|
|
|
|
assertThat(convertToToken(request).getToken()).isEqualTo(TEST_TOKEN);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void resolveWhenNoBodyParameterThenTokenIsNotResolved() {
|
|
|
+ public void resolveWhenNoBodyParameterThenTokenIsNotResolved() {
|
|
|
this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
- MockServerHttpRequest.BaseBuilder<?> request = post("/").contentType(APPLICATION_FORM_URLENCODED);
|
|
|
+ MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.post("/")
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
|
|
|
assertThat(convertToToken(request)).isNull();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void resolveWhenWrongBodyParameterThenTokenIsNotResolved() {
|
|
|
+ public void resolveWhenWrongBodyParameterThenTokenIsNotResolved() {
|
|
|
this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
- MockServerHttpRequest request = post("/").contentType(APPLICATION_FORM_URLENCODED)
|
|
|
- .body("other_param=value");
|
|
|
+ MockServerHttpRequest request = MockServerHttpRequest.post("/")
|
|
|
+ .contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
|
|
+ .body("other_param=value");
|
|
|
|
|
|
assertThat(convertToToken(request)).isNull();
|
|
|
}
|
|
|
|
|
|
- @Test
|
|
|
- void resolveWhenValidHeaderIsPresentTogetherWithBodyParameterThenAuthenticationExceptionIsThrown() {
|
|
|
- this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
- MockServerHttpRequest request = post("/").header(AUTHORIZATION, "Bearer " + TEST_TOKEN)
|
|
|
- .contentType(APPLICATION_FORM_URLENCODED)
|
|
|
- .body("access_token=" + TEST_TOKEN);
|
|
|
-
|
|
|
- assertThatExceptionOfType(OAuth2AuthenticationException.class)
|
|
|
- .isThrownBy(() -> convertToToken(request))
|
|
|
- .withMessageContaining("Found multiple bearer tokens in the request");
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- void resolveWhenValidQueryParameterIsPresentTogetherWithBodyParameterThenAuthenticationExceptionIsThrown() {
|
|
|
- this.converter.setAllowUriQueryParameter(true);
|
|
|
- this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
- MockServerHttpRequest request = post("/").queryParam("access_token", TEST_TOKEN)
|
|
|
- .contentType(APPLICATION_FORM_URLENCODED)
|
|
|
- .body("access_token=" + TEST_TOKEN);
|
|
|
-
|
|
|
- assertThatExceptionOfType(OAuth2AuthenticationException.class)
|
|
|
- .isThrownBy(() -> convertToToken(request))
|
|
|
- .withMessageContaining("Found multiple bearer tokens in the request");
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- void resolveWhenValidQueryParameterIsPresentTogetherWithBodyParameterAndValidHeaderThenAuthenticationExceptionIsThrown() {
|
|
|
- this.converter.setAllowUriQueryParameter(true);
|
|
|
- this.converter.setAllowFormEncodedBodyParameter(true);
|
|
|
- MockServerHttpRequest request = post("/").header(AUTHORIZATION, "Bearer " + TEST_TOKEN)
|
|
|
- .queryParam("access_token", TEST_TOKEN)
|
|
|
- .contentType(APPLICATION_FORM_URLENCODED)
|
|
|
- .body("access_token=" + TEST_TOKEN);
|
|
|
-
|
|
|
- assertThatExceptionOfType(OAuth2AuthenticationException.class)
|
|
|
- .isThrownBy(() -> convertToToken(request))
|
|
|
- .withMessageContaining("Found multiple bearer tokens in the request");
|
|
|
- }
|
|
|
-
|
|
|
// gh-16038
|
|
|
@Test
|
|
|
- void resolveWhenRequestContainsTwoAccessTokenQueryParametersAndSupportIsDisabledThenTokenIsNotResolved() {
|
|
|
+ public void resolveWhenRequestContainsTwoAccessTokenQueryParametersAndSupportIsDisabledThenTokenIsNotResolved() {
|
|
|
MockServerHttpRequest.BaseBuilder<?> request = MockServerHttpRequest.get("/")
|
|
|
.queryParam("access_token", TEST_TOKEN, TEST_TOKEN);
|
|
|
assertThat(convertToToken(request)).isNull();
|