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