Search in sources :

Example 1 with OAuth2Operations

use of org.springframework.social.oauth2.OAuth2Operations 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 OAuth2Operations

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

the class CredentialsTest method shouldFinishOAuth2Acquisition.

@Test
public void shouldFinishOAuth2Acquisition() {
    final OAuth2ConnectionFactory<?> oauth2 = mock(OAuth2ConnectionFactory.class);
    final OAuth2Applicator applicator = new OAuth2Applicator(properties);
    applicator.setAccessTokenProperty("accessTokenProperty");
    applicator.setClientIdProperty("clientIdProperty");
    applicator.setClientSecretProperty("clientSecretProperty");
    applicator.setRefreshTokenProperty("refreshTokenProperty");
    when(locator.providerWithId("providerId")).thenReturn(new OAuth2CredentialProvider<>("providerId", oauth2, applicator));
    final OAuth2Operations operations = mock(OAuth2Operations.class);
    when(oauth2.getOAuthOperations()).thenReturn(operations);
    final AccessGrant accessGrant = new AccessGrant("accessToken", "scope", "refreshToken", 1L);
    when(operations.exchangeForAccess("code", "https://syndesis.io/api/v1/credentials/callback", null)).thenReturn(accessGrant);
    final CredentialFlowState flowState = new OAuth2CredentialFlowState.Builder().providerId("providerId").returnUrl(URI.create("/ui#state")).code("code").state("state").build();
    final CredentialFlowState finalFlowState = credentials.finishAcquisition(flowState, URI.create("https://syndesis.io/api/v1/"));
    assertThat(finalFlowState).isEqualTo(new OAuth2CredentialFlowState.Builder().createFrom(flowState).accessGrant(accessGrant).build());
}
Also used : AccessGrant(org.springframework.social.oauth2.AccessGrant) OAuth2Operations(org.springframework.social.oauth2.OAuth2Operations) Test(org.junit.Test)

Example 3 with OAuth2Operations

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

the class TestCredentialProviderFactory method create.

@Override
public CredentialProvider create(final SocialProperties properties) {
    @SuppressWarnings("unchecked") final OAuth2ConnectionFactory<Object> connectionFactory = mock(OAuth2ConnectionFactory.class);
    when(connectionFactory.generateState()).thenReturn("test-state");
    properties.setAppId("appId");
    properties.setAppSecret("appSecret");
    final OAuth2Applicator applicator = new OAuth2Applicator(properties);
    applicator.setAccessTokenProperty("accessToken");
    applicator.setClientIdProperty("clientId");
    applicator.setClientSecretProperty("clientSecret");
    applicator.setRefreshTokenProperty("refreshToken");
    final CredentialProvider credentialProvider = new OAuth2CredentialProvider<>("test-provider", connectionFactory, applicator);
    @SuppressWarnings({ "unchecked", "rawtypes" }) final Class<MultiValueMap<String, String>> additionalParametersType = (Class) MultiValueMap.class;
    final OAuth2Operations operations = spy(new OAuth2Template("testClientId", "testClientSecret", "https://test/oauth2/authorize", "https://test/oauth2/token"));
    doReturn(new AccessGrant("token")).when(operations).exchangeForAccess(Matchers.anyString(), Matchers.anyString(), Matchers.any(additionalParametersType));
    when(connectionFactory.getOAuthOperations()).thenReturn(operations);
    return credentialProvider;
}
Also used : OAuth2Applicator(io.syndesis.server.credential.OAuth2Applicator) OAuth2CredentialProvider(io.syndesis.server.credential.OAuth2CredentialProvider) OAuth2Template(org.springframework.social.oauth2.OAuth2Template) AccessGrant(org.springframework.social.oauth2.AccessGrant) OAuth2CredentialProvider(io.syndesis.server.credential.OAuth2CredentialProvider) CredentialProvider(io.syndesis.server.credential.CredentialProvider) OAuth2Operations(org.springframework.social.oauth2.OAuth2Operations) MultiValueMap(org.springframework.util.MultiValueMap)

Example 4 with OAuth2Operations

use of org.springframework.social.oauth2.OAuth2Operations 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)4 AccessGrant (org.springframework.social.oauth2.AccessGrant)3 Test (org.junit.Test)2 OAuth2Parameters (org.springframework.social.oauth2.OAuth2Parameters)2 CredentialProvider (io.syndesis.server.credential.CredentialProvider)1 OAuth2Applicator (io.syndesis.server.credential.OAuth2Applicator)1 OAuth2CredentialProvider (io.syndesis.server.credential.OAuth2CredentialProvider)1 OAuth2Template (org.springframework.social.oauth2.OAuth2Template)1 MultiValueMap (org.springframework.util.MultiValueMap)1