org.springframework.social.oauth2.OAuth2Parameters Java Examples

The following examples show how to use org.springframework.social.oauth2.OAuth2Parameters. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OAuth2CredentialProvider.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Override
public CredentialFlowState prepare(final String connectorId, final URI baseUrl, final URI returnUrl) {
    final OAuth2CredentialFlowState.Builder flowState = new OAuth2CredentialFlowState.Builder().returnUrl(returnUrl).providerId(id);

    final OAuth2Parameters parameters = new OAuth2Parameters();
    parameters.putAll(additionalQueryParameters);

    final String callbackUrl = callbackUrlFor(baseUrl, EMPTY);
    parameters.setRedirectUri(callbackUrl);

    final String scope = connectionFactory.getScope();
    parameters.setScope(scope);

    final String stateKey = connectionFactory.generateState();
    flowState.key(stateKey);
    parameters.add("state", stateKey);

    final OAuth2Operations oauthOperations = connectionFactory.getOAuthOperations();

    final String redirectUrl = oauthOperations.buildAuthorizeUrl(parameters);
    flowState.redirectUrl(redirectUrl);

    flowState.connectorId(connectorId);

    return flowState.build();
}
 
Example #2
Source File: WechatOAuth2Template.java    From cola with MIT License 5 votes vote down vote up
/**
 * 构建获取授权码的请求。也就是引导用户跳转到微信的地址。
 */
@Override
public String buildAuthenticateUrl(OAuth2Parameters parameters) {
	String url = super.buildAuthenticateUrl(parameters);
	url = url + "&appid=" + clientId;
	return url;
}
 
Example #3
Source File: WechatMpOAuth2Template.java    From cola with MIT License 5 votes vote down vote up
/**
 * 构建获取授权码的请求。也就是引导用户跳转到微信的地址。
 */
@Override
public String buildAuthenticateUrl(OAuth2Parameters parameters) {
	String url = super.buildAuthenticateUrl(parameters);
	url = url + "&appid=" + clientId;
	return url;
}
 
Example #4
Source File: ManualTest.java    From google-plus-java-api with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
        String secret = "<secret>";
        String id = "<id>";
        OAuth2Parameters oAuthParams = new OAuth2Parameters();
        oAuthParams.setRedirectUri("<https-url>/googleplus/authenticate");
        oAuthParams.setScope("https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.moments.write");
        oAuthParams.put("access_type", Lists.newArrayList("offline"));

        GooglePlusFactory factory = new GooglePlusFactory(id, secret);

        // Uncomment parts of the code below to simulate the whole flow
        // 1. Get the redirect url
        // 2. Open the url in the browser, authenticate, and copy the authorization code from the reidrected url
        // 3. Paste the code and exchange it for tokens.
        // 4. Copy the tokens and use it for getApi(..)

//        String url = factory.getOAuthOperations().buildAuthenticateUrl(oAuthParams);
//        System.out.println(url);

//        AccessGrant grant = factory.getOAuthOperations().exchangeForAccess("<code>", oAuthParams.getRedirectUri(), null);
//        System.out.println(grant.getAccessToken());
//        System.out.println(grant.getRefreshToken());

//        Plus plus = factory.getApi("<token>");
//        Person person = plus.getPeopleOperations().get("me");
//        System.out.println(person.getName().getFamilyName());
    }
 
Example #5
Source File: WeixinOAuth2Template.java    From pre with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 构建获取授权码的请求。也就是引导用户跳转到微信的地址。
 */
@Override
public String buildAuthenticateUrl(OAuth2Parameters parameters) {
    String url = super.buildAuthenticateUrl(parameters);
    url = url + "&appid="+clientId+"&scope=snsapi_login";
    return url;
}
 
Example #6
Source File: CredentialsTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAcquireOAuth2CredentialsWithAdditionalQueryParameters() {
    final OAuth2ConnectionFactory<?> oauth2 = mock(OAuth2ConnectionFactory.class);
    @SuppressWarnings("unchecked")
    final Applicator<AccessGrant> applicator = mock(Applicator.class);
    final Map<String, String> queryParameters = new HashMap<>();
    queryParameters.put("q1", "v1");
    queryParameters.put("q2", "v2");
    when(locator.providerWithId("providerId"))
        .thenReturn(new OAuth2CredentialProvider<>("providerId", oauth2, applicator, queryParameters));

    when(oauth2.getScope()).thenReturn("scope");
    when(oauth2.generateState()).thenReturn("state-token");
    final OAuth2Operations operations = mock(OAuth2Operations.class);
    when(oauth2.getOAuthOperations()).thenReturn(operations);
    final ArgumentCaptor<OAuth2Parameters> parameters = ArgumentCaptor.forClass(OAuth2Parameters.class);
    when(operations.buildAuthorizeUrl(parameters.capture())).thenReturn("https://provider.io/oauth/authorize");

    final AcquisitionFlow acquisition = credentials.acquire("providerId", URI.create("https://syndesis.io/api/v1/"),
        URI.create("/ui#state"));

    final CredentialFlowState expectedFlowState = new OAuth2CredentialFlowState.Builder().key("state-token")
        .providerId("providerId").redirectUrl("https://provider.io/oauth/authorize")
        .returnUrl(URI.create("/ui#state")).build();

    final AcquisitionFlow expected = new AcquisitionFlow.Builder().type(Type.OAUTH2)
        .redirectUrl("https://provider.io/oauth/authorize").state(expectedFlowState).build();
    assertThat(acquisition).isEqualTo(expected);

    final OAuth2Parameters capturedParameters = parameters.getValue();
    assertThat(capturedParameters.getRedirectUri()).isEqualTo("https://syndesis.io/api/v1/credentials/callback");
    assertThat(capturedParameters.getScope()).isEqualTo("scope");
    assertThat(capturedParameters.getState()).isEqualTo("state-token");
    assertThat(capturedParameters.get("q1")).containsOnly("v1");
    assertThat(capturedParameters.get("q2")).containsOnly("v2");
}
 
Example #7
Source File: CredentialsTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAcquireOAuth2Credentials() {
    final OAuth2ConnectionFactory<?> oauth2 = mock(OAuth2ConnectionFactory.class);
    @SuppressWarnings("unchecked")
    final Applicator<AccessGrant> applicator = mock(Applicator.class);
    when(locator.providerWithId("providerId"))
    .thenReturn(new OAuth2CredentialProvider<>("providerId", oauth2, applicator, Collections.emptyMap()));

    when(oauth2.getScope()).thenReturn("scope");
    when(oauth2.generateState()).thenReturn("state-token");
    final OAuth2Operations operations = mock(OAuth2Operations.class);
    when(oauth2.getOAuthOperations()).thenReturn(operations);
    final ArgumentCaptor<OAuth2Parameters> parameters = ArgumentCaptor.forClass(OAuth2Parameters.class);
    when(operations.buildAuthorizeUrl(parameters.capture())).thenReturn("https://provider.io/oauth/authorize");

    final AcquisitionFlow acquisition = credentials.acquire("providerId", URI.create("https://syndesis.io/api/v1/"),
        URI.create("/ui#state"));

    final CredentialFlowState expectedFlowState = new OAuth2CredentialFlowState.Builder().key("state-token")
        .providerId("providerId").redirectUrl("https://provider.io/oauth/authorize")
        .returnUrl(URI.create("/ui#state")).build();

    final AcquisitionFlow expected = new AcquisitionFlow.Builder().type(Type.OAUTH2)
        .redirectUrl("https://provider.io/oauth/authorize").state(expectedFlowState).build();
    assertThat(acquisition).isEqualTo(expected);

    final OAuth2Parameters capturedParameters = parameters.getValue();
    assertThat(capturedParameters.getRedirectUri()).isEqualTo("https://syndesis.io/api/v1/credentials/callback");
    assertThat(capturedParameters.getScope()).isEqualTo("scope");
    assertThat(capturedParameters.getState()).isEqualTo("state-token");
}
 
Example #8
Source File: WechatOAuth2Template.java    From cola with MIT License 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(OAuth2Parameters parameters) {
	return buildAuthenticateUrl(parameters);
}
 
Example #9
Source File: YahooOAuth2Template.java    From cloudstreetmarket.com with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(GrantType grantType, OAuth2Parameters parameters) {
	if (redirectUri != null) parameters.setRedirectUri(redirectUri);
	return super.buildAuthorizeUrl(grantType, parameters);
}
 
Example #10
Source File: YahooOAuth2Template.java    From cloudstreetmarket.com with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String buildAuthenticateUrl(GrantType grantType, OAuth2Parameters parameters) {
	if (redirectUri != null) parameters.setRedirectUri(redirectUri);
	return super.buildAuthenticateUrl(grantType, parameters);
}
 
Example #11
Source File: WeiXinOAuth2Template.java    From FEBS-Security with Apache License 2.0 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(OAuth2Parameters parameters) {
    return buildAuthenticateUrl(parameters);
}
 
Example #12
Source File: WeiXinOAuth2Template.java    From FEBS-Security with Apache License 2.0 4 votes vote down vote up
@Override
public String buildAuthenticateUrl(OAuth2Parameters parameters) {
    String url = super.buildAuthenticateUrl(parameters);
    url = url + "&appid=" + clientId + "&scope=snsapi_login";
    return url;
}
 
Example #13
Source File: WechatOAuth2Template.java    From spring-social-wechat with Apache License 2.0 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(GrantType grantType, OAuth2Parameters parameters) {
	return replaceParamKey(super.buildAuthorizeUrl(grantType, parameters));
}
 
Example #14
Source File: WechatOAuth2Template.java    From spring-social-wechat with Apache License 2.0 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(OAuth2Parameters parameters) {
	return replaceParamKey(super.buildAuthorizeUrl(parameters));
}
 
Example #15
Source File: WeixinOAuth2Template.java    From pre with GNU General Public License v3.0 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(OAuth2Parameters parameters) {
    return buildAuthenticateUrl(parameters);
}
 
Example #16
Source File: AlipayOAuth2Template.java    From cola with MIT License 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(GrantType grantType, OAuth2Parameters parameters) {
	String url = super.buildAuthorizeUrl(grantType, parameters);
	url = url.replace("client_id=", "app_id=");
	return url;
}
 
Example #17
Source File: AlipayOAuth2Template.java    From cola with MIT License 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(OAuth2Parameters parameters) {
	String url = super.buildAuthorizeUrl(parameters);
	url = url.replace("client_id=", "app_id=");
	return url;
}
 
Example #18
Source File: WechatMpOAuth2Template.java    From cola with MIT License 4 votes vote down vote up
@Override
public String buildAuthorizeUrl(OAuth2Parameters parameters) {
	return buildAuthenticateUrl(parameters);
}
 
Example #19
Source File: WeixinOAuth2Template.java    From paascloud-master with Apache License 2.0 3 votes vote down vote up
/**
 * 构建获取授权码的请求。也就是引导用户跳转到微信的地址。
 *
 * @param parameters the parameters
 *
 * @return the string
 */
@Override
public String buildAuthenticateUrl(OAuth2Parameters parameters) {
	String url = super.buildAuthenticateUrl(parameters);
	url = url + "&appid=" + clientId + "&scope=snsapi_login";
	return url;
}
 
Example #20
Source File: WeixinOAuth2Template.java    From paascloud-master with Apache License 2.0 2 votes vote down vote up
/**
 * Build authorize url string.
 *
 * @param parameters the parameters
 *
 * @return the string
 */
@Override
public String buildAuthorizeUrl(OAuth2Parameters parameters) {
	return buildAuthenticateUrl(parameters);
}