|
|
|
@ -16,6 +16,11 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.springframework.security.oauth2.client.endpoint; |
|
|
|
package org.springframework.security.oauth2.client.endpoint; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
|
|
|
|
import java.net.URLEncoder; |
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
|
|
|
|
import java.util.Base64; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
@ -24,6 +29,7 @@ import org.springframework.http.HttpMethod; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.http.RequestEntity; |
|
|
|
import org.springframework.http.RequestEntity; |
|
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistration; |
|
|
|
import org.springframework.security.oauth2.client.registration.ClientRegistration; |
|
|
|
|
|
|
|
import org.springframework.security.oauth2.client.registration.TestClientRegistrations; |
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType; |
|
|
|
import org.springframework.security.oauth2.core.AuthorizationGrantType; |
|
|
|
import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
|
|
|
import org.springframework.security.oauth2.core.ClientAuthenticationMethod; |
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
|
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames; |
|
|
|
@ -76,4 +82,37 @@ public class OAuth2ClientCredentialsGrantRequestEntityConverterTests { |
|
|
|
assertThat(formParameters.getFirst(OAuth2ParameterNames.SCOPE)).isEqualTo("read write"); |
|
|
|
assertThat(formParameters.getFirst(OAuth2ParameterNames.SCOPE)).isEqualTo("read write"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// gh-9610
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void convertWhenSpecialCharactersThenConvertsWithEncodedClientCredentials() |
|
|
|
|
|
|
|
throws UnsupportedEncodingException { |
|
|
|
|
|
|
|
String clientCredentialWithAnsiKeyboardSpecialCharacters = "~!@#$%^&*()_+{}|:\"<>?`-=[]\\;',./ "; |
|
|
|
|
|
|
|
// @formatter:off
|
|
|
|
|
|
|
|
ClientRegistration clientRegistration = TestClientRegistrations.clientCredentials() |
|
|
|
|
|
|
|
.clientId(clientCredentialWithAnsiKeyboardSpecialCharacters) |
|
|
|
|
|
|
|
.clientSecret(clientCredentialWithAnsiKeyboardSpecialCharacters) |
|
|
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
// @formatter:on
|
|
|
|
|
|
|
|
OAuth2ClientCredentialsGrantRequest clientCredentialsGrantRequest = new OAuth2ClientCredentialsGrantRequest( |
|
|
|
|
|
|
|
clientRegistration); |
|
|
|
|
|
|
|
RequestEntity<?> requestEntity = this.converter.convert(clientCredentialsGrantRequest); |
|
|
|
|
|
|
|
assertThat(requestEntity.getMethod()).isEqualTo(HttpMethod.POST); |
|
|
|
|
|
|
|
assertThat(requestEntity.getUrl().toASCIIString()) |
|
|
|
|
|
|
|
.isEqualTo(clientRegistration.getProviderDetails().getTokenUri()); |
|
|
|
|
|
|
|
HttpHeaders headers = requestEntity.getHeaders(); |
|
|
|
|
|
|
|
assertThat(headers.getAccept()).contains(MediaType.APPLICATION_JSON_UTF8); |
|
|
|
|
|
|
|
assertThat(headers.getContentType()) |
|
|
|
|
|
|
|
.isEqualTo(MediaType.valueOf(MediaType.APPLICATION_FORM_URLENCODED_VALUE + ";charset=UTF-8")); |
|
|
|
|
|
|
|
String urlEncodedClientCredential = URLEncoder.encode(clientCredentialWithAnsiKeyboardSpecialCharacters, |
|
|
|
|
|
|
|
StandardCharsets.UTF_8.toString()); |
|
|
|
|
|
|
|
String clientCredentials = Base64.getEncoder().encodeToString( |
|
|
|
|
|
|
|
(urlEncodedClientCredential + ":" + urlEncodedClientCredential).getBytes(StandardCharsets.UTF_8)); |
|
|
|
|
|
|
|
assertThat(headers.getFirst(HttpHeaders.AUTHORIZATION)).isEqualTo("Basic " + clientCredentials); |
|
|
|
|
|
|
|
MultiValueMap<String, String> formParameters = (MultiValueMap<String, String>) requestEntity.getBody(); |
|
|
|
|
|
|
|
assertThat(formParameters.getFirst(OAuth2ParameterNames.GRANT_TYPE)) |
|
|
|
|
|
|
|
.isEqualTo(AuthorizationGrantType.CLIENT_CREDENTIALS.getValue()); |
|
|
|
|
|
|
|
assertThat(formParameters.getFirst(OAuth2ParameterNames.SCOPE)).contains(clientRegistration.getScopes()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|