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();
}
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());
}
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();
}
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;
});
}
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());
}
Aggregations