Search in sources :

Example 6 with OAuthToken

use of org.springframework.social.oauth1.OAuthToken in project syndesis by syndesisio.

the class CredentialsTest method shouldAcquireOAuth1aCredentials.

@Test
public void shouldAcquireOAuth1aCredentials() {
    final OAuth1ConnectionFactory<?> oauth1 = mock(OAuth1ConnectionFactory.class);
    @SuppressWarnings("unchecked") final Applicator<OAuthToken> applicator = mock(Applicator.class);
    when(locator.providerWithId("providerId")).thenReturn(new OAuth1CredentialProvider<>("providerId", oauth1, applicator));
    final OAuth1Operations operations = mock(OAuth1Operations.class);
    when(oauth1.getOAuthOperations()).thenReturn(operations);
    when(operations.getVersion()).thenReturn(OAuth1Version.CORE_10_REVISION_A);
    final OAuthToken token = new OAuthToken("value", "secret");
    when(operations.fetchRequestToken("https://syndesis.io/api/v1/credentials/callback", null)).thenReturn(token);
    final ArgumentCaptor<OAuth1Parameters> parameters = ArgumentCaptor.forClass(OAuth1Parameters.class);
    when(operations.buildAuthorizeUrl(eq("value"), parameters.capture())).thenReturn("https://provider.io/oauth/authorize");
    final AcquisitionFlow acquisition = credentials.acquire("providerId", URI.create("https://syndesis.io/api/v1/"), URI.create("https://syndesis.io/ui#state"));
    final CredentialFlowState expectedFlowState = new OAuth1CredentialFlowState.Builder().providerId("providerId").redirectUrl("https://provider.io/oauth/authorize").returnUrl(URI.create("https://syndesis.io/ui#state")).token(token).build();
    final AcquisitionFlow expected = new AcquisitionFlow.Builder().type(Type.OAUTH1).redirectUrl("https://provider.io/oauth/authorize").state(expectedFlowState).build();
    assertThat(acquisition).isEqualToIgnoringGivenFields(expected, "state");
    final Optional<CredentialFlowState> maybeState = acquisition.state();
    assertThat(maybeState).isPresent();
    final CredentialFlowState state = maybeState.get();
    assertThat(state).isEqualToIgnoringGivenFields(expectedFlowState, "key");
    assertThat(state.getKey()).isNotNull();
    final OAuth1Parameters oAuth1Parameters = parameters.getValue();
    assertThat(oAuth1Parameters.getCallbackUrl()).isNull();
}
Also used : OAuthToken(org.springframework.social.oauth1.OAuthToken) OAuth1Parameters(org.springframework.social.oauth1.OAuth1Parameters) OAuth1Operations(org.springframework.social.oauth1.OAuth1Operations) Test(org.junit.Test)

Example 7 with OAuthToken

use of org.springframework.social.oauth1.OAuthToken in project syndesis by syndesisio.

the class CredentialsTest method shouldAcquireOAuth1Credentials.

@Test
public void shouldAcquireOAuth1Credentials() {
    final OAuth1ConnectionFactory<?> oauth1 = mock(OAuth1ConnectionFactory.class);
    @SuppressWarnings("unchecked") final Applicator<OAuthToken> applicator = mock(Applicator.class);
    when(locator.providerWithId("providerId")).thenReturn(new OAuth1CredentialProvider<>("providerId", oauth1, applicator));
    final OAuth1Operations operations = mock(OAuth1Operations.class);
    when(oauth1.getOAuthOperations()).thenReturn(operations);
    when(operations.getVersion()).thenReturn(OAuth1Version.CORE_10);
    final OAuthToken token = new OAuthToken("value", "secret");
    when(operations.fetchRequestToken(null, null)).thenReturn(token);
    final ArgumentCaptor<OAuth1Parameters> parameters = ArgumentCaptor.forClass(OAuth1Parameters.class);
    when(operations.buildAuthorizeUrl(eq("value"), 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 OAuth1CredentialFlowState.Builder().providerId("providerId").redirectUrl("https://provider.io/oauth/authorize").returnUrl(URI.create("/ui#state")).token(token).build();
    final AcquisitionFlow expected = new AcquisitionFlow.Builder().type(Type.OAUTH1).redirectUrl("https://provider.io/oauth/authorize").state(expectedFlowState).build();
    assertThat(acquisition).isEqualToIgnoringGivenFields(expected, "state");
    final Optional<CredentialFlowState> maybeState = acquisition.state();
    assertThat(maybeState).isPresent();
    final CredentialFlowState state = maybeState.get();
    assertThat(state).isEqualToIgnoringGivenFields(expectedFlowState, "key");
    assertThat(state.getKey()).isNotNull();
    final OAuth1Parameters oAuth1Parameters = parameters.getValue();
    assertThat(oAuth1Parameters.getCallbackUrl()).isEqualTo("https://syndesis.io/api/v1/credentials/callback");
}
Also used : OAuthToken(org.springframework.social.oauth1.OAuthToken) OAuth1Parameters(org.springframework.social.oauth1.OAuth1Parameters) OAuth1Operations(org.springframework.social.oauth1.OAuth1Operations) Test(org.junit.Test)

Example 8 with OAuthToken

use of org.springframework.social.oauth1.OAuthToken in project syndesis by syndesisio.

the class OAuth1CredentialProvider method finish.

@Override
public CredentialFlowState finish(final CredentialFlowState givenFlowState, final URI baseUrl) {
    final OAuth1CredentialFlowState flowState = flowState(givenFlowState);
    final AuthorizedRequestToken requestToken = new AuthorizedRequestToken(flowState.getToken(), flowState.getVerifier());
    final OAuthToken accessToken = connectionFactory.getOAuthOperations().exchangeForAccessToken(requestToken, null);
    return new OAuth1CredentialFlowState.Builder().createFrom(flowState).accessToken(accessToken).build();
}
Also used : OAuthToken(org.springframework.social.oauth1.OAuthToken) AuthorizedRequestToken(org.springframework.social.oauth1.AuthorizedRequestToken)

Aggregations

OAuthToken (org.springframework.social.oauth1.OAuthToken)8 Test (org.junit.Test)6 OAuth1Operations (org.springframework.social.oauth1.OAuth1Operations)4 OAuth1Parameters (org.springframework.social.oauth1.OAuth1Parameters)3 Connection (io.syndesis.common.model.connection.Connection)2 AuthorizedRequestToken (org.springframework.social.oauth1.AuthorizedRequestToken)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CredentialFlowState (io.syndesis.server.credential.CredentialFlowState)1 CredentialProvider (io.syndesis.server.credential.CredentialProvider)1 CredentialProviderLocator (io.syndesis.server.credential.CredentialProviderLocator)1 OAuth1CredentialFlowState (io.syndesis.server.credential.OAuth1CredentialFlowState)1 OAuth1CredentialProvider (io.syndesis.server.credential.OAuth1CredentialProvider)1 OAuthAppHandler (io.syndesis.server.endpoint.v1.handler.setup.OAuthAppHandler)1 Arrays (java.util.Arrays)1 List (java.util.List)1 SECONDS (java.util.concurrent.TimeUnit.SECONDS)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.entry (org.assertj.core.api.Assertions.entry)1 Awaitility.given (org.awaitility.Awaitility.given)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1