use of org.springframework.social.oauth2.AccessGrant in project syndesis by syndesisio.
the class CredentialITCase method shouldReceiveCallbacksFromResourceProviders.
@Test
public void shouldReceiveCallbacksFromResourceProviders() {
final OAuth2CredentialFlowState flowState = new OAuth2CredentialFlowState.Builder().providerId("test-provider").key(UUID.randomUUID().toString()).returnUrl(URI.create("/ui#state")).build();
final HttpHeaders cookies = persistAsCookie(flowState);
final ResponseEntity<Void> callbackResponse = http(HttpMethod.GET, "/api/v1/credentials/callback?state=test-state&code=code", null, Void.class, null, cookies, HttpStatus.TEMPORARY_REDIRECT);
assertThat(callbackResponse.getStatusCode()).as("Status should be temporarry redirect (307)").isEqualTo(HttpStatus.TEMPORARY_REDIRECT);
assertThat(callbackResponse.hasBody()).as("Should not contain HTTP body").isFalse();
assertThat(callbackResponse.getHeaders().getLocation().toString()).matches("http.?://localhost:[0-9]*/api/v1/ui#%7B%22connectorId%22:%22test-provider%22,%22message%22:%22Successfully%20authorized%20Syndesis's%20access%22,%22status%22:%22SUCCESS%22%7D");
final List<String> receivedCookies = callbackResponse.getHeaders().get("Set-Cookie");
assertThat(receivedCookies).hasSize(1);
final OAuth2CredentialFlowState endingFlowState = clientSideState.restoreFrom(Cookie.valueOf(receivedCookies.get(0)), OAuth2CredentialFlowState.class);
// AccessGrant does not implement equals/hashCode
assertThat(endingFlowState).isEqualToIgnoringGivenFields(new OAuth2CredentialFlowState.Builder().createFrom(flowState).code("code").build(), "accessGrant");
assertThat(endingFlowState.getAccessGrant()).isEqualToComparingFieldByField(new AccessGrant("token"));
}
use of org.springframework.social.oauth2.AccessGrant 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.AccessGrant 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");
}
use of org.springframework.social.oauth2.AccessGrant in project syndesis by syndesisio.
the class OAuth2ApplicatorTest method shouldApplyAccessGrants.
@Test
public void shouldApplyAccessGrants() {
final SocialProperties properties = new SocialProperties() {
};
properties.setAppId("appId");
properties.setAppSecret("appSecret");
final OAuth2Applicator applicator = new OAuth2Applicator(properties);
applicator.setAccessTokenProperty("accessTokenProperty");
applicator.setClientIdProperty("clientIdProperty");
applicator.setClientSecretProperty("clientSecretProperty");
applicator.setRefreshTokenProperty("refreshTokenProperty");
final Connection connection = new Connection.Builder().build();
final Connection result = applicator.applyTo(connection, new AccessGrant("accessToken", "scope", "refreshToken", 1L));
final Connection expected = new Connection.Builder().putConfiguredProperty("accessTokenProperty", "accessToken").putConfiguredProperty("clientIdProperty", "appId").putConfiguredProperty("clientSecretProperty", "appSecret").putConfiguredProperty("refreshTokenProperty", "refreshToken").build();
assertThat(result).isEqualToIgnoringGivenFields(expected, "lastUpdated");
assertThat(result.getLastUpdated()).isPresent();
}
Aggregations