Search in sources :

Example 1 with OAuthToken

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

the class OAuth1CredentialProvider method prepare.

@Override
public CredentialFlowState prepare(final String connectorId, final URI baseUrl, final URI returnUrl) {
    final OAuth1CredentialFlowState.Builder flowState = new OAuth1CredentialFlowState.Builder().returnUrl(returnUrl).providerId(id);
    final OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations();
    final OAuth1Parameters parameters = new OAuth1Parameters();
    final String stateKey = UUID.randomUUID().toString();
    flowState.key(stateKey);
    final OAuthToken oAuthToken;
    final OAuth1Version oAuthVersion = oauthOperations.getVersion();
    if (oAuthVersion == OAuth1Version.CORE_10) {
        parameters.setCallbackUrl(callbackUrlFor(baseUrl, EMPTY));
        oAuthToken = oauthOperations.fetchRequestToken(null, null);
    } else if (oAuthVersion == OAuth1Version.CORE_10_REVISION_A) {
        oAuthToken = oauthOperations.fetchRequestToken(callbackUrlFor(baseUrl, EMPTY), null);
    } else {
        throw new IllegalStateException("Unsupported OAuth 1 version: " + oAuthVersion);
    }
    flowState.token(oAuthToken);
    final String redirectUrl = oauthOperations.buildAuthorizeUrl(oAuthToken.getValue(), parameters);
    flowState.redirectUrl(redirectUrl);
    flowState.connectorId(connectorId);
    return flowState.build();
}
Also used : OAuthToken(org.springframework.social.oauth1.OAuthToken) OAuth1Parameters(org.springframework.social.oauth1.OAuth1Parameters) OAuth1Version(org.springframework.social.oauth1.OAuth1Version) OAuth1Operations(org.springframework.social.oauth1.OAuth1Operations)

Example 2 with OAuthToken

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

the class CredentialsTest method shouldFinishOAuth1Acquisition.

@Test
public void shouldFinishOAuth1Acquisition() {
    final OAuthToken token = new OAuthToken("value", "secret");
    final OAuth1ConnectionFactory<?> oauth1 = mock(OAuth1ConnectionFactory.class);
    final OAuth1Applicator applicator = new OAuth1Applicator(properties);
    when(locator.providerWithId("providerId")).thenReturn(new OAuth1CredentialProvider<>("providerId", oauth1, applicator));
    final OAuth1Operations operations = mock(OAuth1Operations.class);
    when(oauth1.getOAuthOperations()).thenReturn(operations);
    final ArgumentCaptor<AuthorizedRequestToken> requestToken = ArgumentCaptor.forClass(AuthorizedRequestToken.class);
    final OAuthToken accessToken = new OAuthToken("tokenValue", "tokenSecret");
    @SuppressWarnings({ "unchecked", "rawtypes" }) final Class<MultiValueMap<String, String>> multimapType = (Class) MultiValueMap.class;
    when(operations.exchangeForAccessToken(requestToken.capture(), isNull(multimapType))).thenReturn(accessToken);
    applicator.setAccessTokenSecretProperty("accessTokenSecretProperty");
    applicator.setAccessTokenValueProperty("accessTokenValueProperty");
    applicator.setConsumerKeyProperty("consumerKeyProperty");
    applicator.setConsumerSecretProperty("consumerSecretProperty");
    final CredentialFlowState flowState = new OAuth1CredentialFlowState.Builder().providerId("providerId").token(token).returnUrl(URI.create("/ui#state")).verifier("verifier").build();
    final CredentialFlowState finalFlowState = credentials.finishAcquisition(flowState, URI.create("https://www.example.com"));
    final AuthorizedRequestToken capturedRequestToken = requestToken.getValue();
    assertThat(capturedRequestToken.getValue()).isEqualTo("value");
    assertThat(capturedRequestToken.getSecret()).isEqualTo("secret");
    assertThat(capturedRequestToken.getVerifier()).isEqualTo("verifier");
    assertThat(finalFlowState).isEqualTo(new OAuth1CredentialFlowState.Builder().createFrom(flowState).accessToken(accessToken).build());
}
Also used : OAuthToken(org.springframework.social.oauth1.OAuthToken) AuthorizedRequestToken(org.springframework.social.oauth1.AuthorizedRequestToken) OAuth1Operations(org.springframework.social.oauth1.OAuth1Operations) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.Test)

Example 3 with OAuthToken

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

the class OAuth1ApplicatorTest method shouldApplyTokens.

@Test
public void shouldApplyTokens() {
    final SocialProperties properties = new SocialProperties() {
    };
    properties.setAppId("appId");
    properties.setAppSecret("appSecret");
    final OAuth1Applicator applicator = new OAuth1Applicator(properties);
    applicator.setAccessTokenSecretProperty("accessTokenSecretProperty");
    applicator.setAccessTokenValueProperty("accessTokenValueProperty");
    applicator.setConsumerKeyProperty("consumerKeyProperty");
    applicator.setConsumerSecretProperty("consumerSecretProperty");
    final Connection connection = new Connection.Builder().build();
    final Connection result = applicator.applyTo(connection, new OAuthToken("tokenValue", "tokenSecret"));
    final Connection expected = new Connection.Builder().putConfiguredProperty("accessTokenSecretProperty", "tokenSecret").putConfiguredProperty("accessTokenValueProperty", "tokenValue").putConfiguredProperty("consumerKeyProperty", "appId").putConfiguredProperty("consumerSecretProperty", "appSecret").build();
    assertThat(result).isEqualToIgnoringGivenFields(expected, "lastUpdated");
    assertThat(result.getLastUpdated()).isPresent();
}
Also used : OAuthToken(org.springframework.social.oauth1.OAuthToken) Connection(io.syndesis.common.model.connection.Connection) SocialProperties(org.springframework.boot.autoconfigure.social.SocialProperties) Test(org.junit.Test)

Example 4 with OAuthToken

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

the class SetupITCase method updateOauthApp.

@Test
public void updateOauthApp() {
    // Validate initial state assumptions.
    getOauthApps();
    OAuthAppHandler.OAuthApp twitter = new OAuthAppHandler.OAuthApp();
    twitter.clientId = "test-id";
    twitter.clientSecret = "test-secret";
    http(HttpMethod.PUT, "/api/v1/setup/oauth-apps/twitter", twitter, null, tokenRule.validToken(), HttpStatus.NO_CONTENT);
    ResponseEntity<OAuthAppHandler.OAuthApp[]> result = get("/api/v1/setup/oauth-apps", OAuthAppHandler.OAuthApp[].class);
    List<OAuthAppHandler.OAuthApp> apps = Arrays.asList(result.getBody());
    assertThat(apps.size()).isEqualTo(2);
    twitter = apps.stream().filter(x -> "twitter".equals(x.id)).findFirst().get();
    assertThat(twitter.id).isEqualTo("twitter");
    assertThat(twitter.name).isEqualTo("Twitter");
    assertThat(twitter.icon).isEqualTo("fa-twitter");
    assertThat(twitter.clientId).isEqualTo("test-id");
    assertThat(twitter.clientSecret).isEqualTo("test-secret");
    // Now that we have configured the app, we should be able to create the
    // connection factory.
    // The connection factory is setup async so we might need to wait a little bit
    // for it to register.
    given().ignoreExceptions().await().atMost(10, SECONDS).pollInterval(1, SECONDS).until(() -> {
        final CredentialProvider twitterCredentialProvider = locator.providerWithId("twitter");
        // preparing is something we could not do with a `null` ConnectionFactory
        assertThat(twitterCredentialProvider).isNotNull().isInstanceOfSatisfying(OAuth1CredentialProvider.class, p -> {
            final Connection connection = new Connection.Builder().build();
            final CredentialFlowState flowState = new OAuth1CredentialFlowState.Builder().accessToken(new OAuthToken("value", "secret")).connectorId("connectorId").build();
            final Connection appliedTo = p.applyTo(connection, flowState);
            // test that the updated values are used
            assertThat(appliedTo.getConfiguredProperties()).contains(entry("consumerKey", "test-id"), entry("consumerSecret", "test-secret"));
        });
        return true;
    });
}
Also used : Arrays(java.util.Arrays) CredentialFlowState(io.syndesis.server.credential.CredentialFlowState) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpMethod(org.springframework.http.HttpMethod) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) OAuthToken(org.springframework.social.oauth1.OAuthToken) Assertions.entry(org.assertj.core.api.Assertions.entry) Awaitility.given(org.awaitility.Awaitility.given) OAuth1CredentialFlowState(io.syndesis.server.credential.OAuth1CredentialFlowState) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Connection(io.syndesis.common.model.connection.Connection) CredentialProvider(io.syndesis.server.credential.CredentialProvider) CredentialProviderLocator(io.syndesis.server.credential.CredentialProviderLocator) ResponseEntity(org.springframework.http.ResponseEntity) OAuth1CredentialProvider(io.syndesis.server.credential.OAuth1CredentialProvider) OAuthAppHandler(io.syndesis.server.endpoint.v1.handler.setup.OAuthAppHandler) SECONDS(java.util.concurrent.TimeUnit.SECONDS) CredentialFlowState(io.syndesis.server.credential.CredentialFlowState) OAuth1CredentialFlowState(io.syndesis.server.credential.OAuth1CredentialFlowState) Connection(io.syndesis.common.model.connection.Connection) OAuth1CredentialFlowState(io.syndesis.server.credential.OAuth1CredentialFlowState) OAuthAppHandler(io.syndesis.server.endpoint.v1.handler.setup.OAuthAppHandler) OAuthToken(org.springframework.social.oauth1.OAuthToken) CredentialProvider(io.syndesis.server.credential.CredentialProvider) OAuth1CredentialProvider(io.syndesis.server.credential.OAuth1CredentialProvider) Test(org.junit.Test)

Example 5 with OAuthToken

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

the class CredentialModuleTest method shouldDeserializeFromJson.

@Test
public void shouldDeserializeFromJson() throws IOException {
    final String json = "{\"type\":\"OAUTH1\",\"accessToken\":{\"value\":\"access-token-value\",\"secret\":\"access-token-secret\"},\"token\":{\"value\":\"token-value\",\"secret\":\"token-secret\"},\"verifier\":\"verifier\",\"key\":\"key\",\"providerId\":\"twitter\",\"returnUrl\":\"https://localhost:4200/connections/create/configure-fields?state=create-connection&connectorId=twitter\"}";
    final ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new CredentialModule());
    final OAuth1CredentialFlowState flowState = mapper.readerFor(CredentialFlowState.class).readValue(json);
    final OAuth1CredentialFlowState expected = new OAuth1CredentialFlowState.Builder().accessToken(new OAuthToken("access-token-value", "access-token-secret")).token(new OAuthToken("token-value", "token-secret")).verifier("verifier").key("key").providerId("twitter").returnUrl(URI.create("https://localhost:4200/connections/create/configure-fields?state=create-connection&connectorId=twitter")).build();
    assertThat(flowState).isEqualToIgnoringGivenFields(expected, "accessToken", "token");
    assertThat(flowState.getAccessToken()).isEqualToComparingFieldByField(expected.getAccessToken());
    assertThat(flowState.getToken()).isEqualToComparingFieldByField(expected.getToken());
}
Also used : OAuthToken(org.springframework.social.oauth1.OAuthToken) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

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