Search in sources :

Example 1 with OAuth2Parameters

use of org.springframework.social.oauth2.OAuth2Parameters in project syndesis by syndesisio.

the class OAuth2CredentialProvider method prepare.

@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();
    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();
}
Also used : OAuth2Parameters(org.springframework.social.oauth2.OAuth2Parameters) OAuth2Operations(org.springframework.social.oauth2.OAuth2Operations)

Example 2 with OAuth2Parameters

use of org.springframework.social.oauth2.OAuth2Parameters in project syndesis by syndesisio.

the class CredentialsTest method shouldAcquireOAuth2Credentials.

@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));
    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");
}
Also used : OAuth2Parameters(org.springframework.social.oauth2.OAuth2Parameters) AccessGrant(org.springframework.social.oauth2.AccessGrant) OAuth2Operations(org.springframework.social.oauth2.OAuth2Operations) Test(org.junit.Test)

Aggregations

OAuth2Operations (org.springframework.social.oauth2.OAuth2Operations)2 OAuth2Parameters (org.springframework.social.oauth2.OAuth2Parameters)2 Test (org.junit.Test)1 AccessGrant (org.springframework.social.oauth2.AccessGrant)1