Skip to content
This repository was archived by the owner on Apr 5, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.social.support.ClientHttpRequestFactorySelector;
import org.springframework.social.support.FormMapHttpMessageConverter;
import org.springframework.social.support.LoggingErrorHandler;
import org.springframework.social.support.URIBuilder;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
Expand Down Expand Up @@ -85,10 +86,9 @@ public OAuth2Template(String clientId, String clientSecret, String authorizeUrl,
Assert.notNull(accessTokenUrl, "The accessTokenUrl property cannot be null");
this.clientId = clientId;
this.clientSecret = clientSecret;
String clientInfo = "?client_id=" + formEncode(clientId);
this.authorizeUrl = authorizeUrl + clientInfo;
this.authorizeUrl = URIBuilder.fromUri(authorizeUrl).queryParam("client_id", clientId).build().toString();
if (authenticateUrl != null) {
this.authenticateUrl = authenticateUrl + clientInfo;
this.authenticateUrl = URIBuilder.fromUri(authenticateUrl).queryParam("client_id", clientId).build().toString();
} else {
this.authenticateUrl = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ public class OAuth2TemplateTest {

private static final String AUTHORIZE_URL = "http://www.someprovider.com/oauth/authorize";

private static final String AUTHORIZE_URL_WITH_QUERY = "http://www.someprovider.com/oauth/authorize?foo=bar";

private static final String AUTHENTICATE_URL_WITH_QUERY = "http://www.someprovider.com/oauth/authenticate?foo=bar";

private static final String ACCESS_TOKEN_URL = "http://www.someprovider.com/oauth/accessToken";

private OAuth2Template oAuth2Template;
private OAuth2Template oAuth2TemplateParamBased;
private OAuth2Template oAuth2TemplateWithQuery;

@Before
public void setup() {
oAuth2Template = new OAuth2Template("client_id", "client_secret", AUTHORIZE_URL, null, ACCESS_TOKEN_URL);
oAuth2TemplateParamBased = new OAuth2Template("client_id", "client_secret", AUTHORIZE_URL, null, ACCESS_TOKEN_URL);
oAuth2TemplateParamBased.setUseParametersForClientAuthentication(true);
oAuth2TemplateWithQuery = new OAuth2Template("client_id", "client_secret", AUTHORIZE_URL_WITH_QUERY, AUTHENTICATE_URL_WITH_QUERY, ACCESS_TOKEN_URL);
}

@Test
Expand Down Expand Up @@ -90,6 +96,24 @@ public void buildAuthorizeUrl_additionalParameters() {
assertEquals(expected, actual);
}

@Test
public void buildAuthorizeUrl_withQuery() {
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.setRedirectUri("http://www.someclient.com/connect/foo");
String expected = AUTHORIZE_URL_WITH_QUERY + "&client_id=client_id&response_type=code&redirect_uri=http%3A%2F%2Fwww.someclient.com%2Fconnect%2Ffoo";
String actual = oAuth2TemplateWithQuery.buildAuthorizeUrl(parameters);
assertEquals(expected, actual);
}

@Test
public void buildAuthenticateUrl_withQuery() {
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.setRedirectUri("http://www.someclient.com/connect/foo");
String expected = AUTHENTICATE_URL_WITH_QUERY + "&client_id=client_id&response_type=code&redirect_uri=http%3A%2F%2Fwww.someclient.com%2Fconnect%2Ffoo";
String actual = oAuth2TemplateWithQuery.buildAuthenticateUrl(parameters);
assertEquals(expected, actual);
}

@Test
public void exchangeForAccess_jsonResponse() {
AccessGrant accessGrant = getAccessGrant("accessToken.json");
Expand Down