Search in sources :

Example 6 with AccessGrant

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"));
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) AccessGrant(org.springframework.social.oauth2.AccessGrant) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) OAuth2CredentialFlowState(io.syndesis.server.credential.OAuth2CredentialFlowState) Test(org.junit.Test)

Example 7 with AccessGrant

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;
}
Also used : OAuth2Applicator(io.syndesis.server.credential.OAuth2Applicator) OAuth2CredentialProvider(io.syndesis.server.credential.OAuth2CredentialProvider) OAuth2Template(org.springframework.social.oauth2.OAuth2Template) AccessGrant(org.springframework.social.oauth2.AccessGrant) OAuth2CredentialProvider(io.syndesis.server.credential.OAuth2CredentialProvider) CredentialProvider(io.syndesis.server.credential.CredentialProvider) OAuth2Operations(org.springframework.social.oauth2.OAuth2Operations) MultiValueMap(org.springframework.util.MultiValueMap)

Example 8 with AccessGrant

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");
}
Also used : OAuth2Parameters(org.springframework.social.oauth2.OAuth2Parameters) AccessGrant(org.springframework.social.oauth2.AccessGrant) OAuth2Operations(org.springframework.social.oauth2.OAuth2Operations) Test(org.junit.Test)

Example 9 with AccessGrant

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();
}
Also used : AccessGrant(org.springframework.social.oauth2.AccessGrant) Connection(io.syndesis.common.model.connection.Connection) SocialProperties(org.springframework.boot.autoconfigure.social.SocialProperties) Test(org.junit.Test)

Aggregations

AccessGrant (org.springframework.social.oauth2.AccessGrant)9 Test (org.junit.Test)6 Connection (io.syndesis.common.model.connection.Connection)3 OAuth2Operations (org.springframework.social.oauth2.OAuth2Operations)3 OAuth2CredentialFlowState (io.syndesis.server.credential.OAuth2CredentialFlowState)2 HttpHeaders (org.springframework.http.HttpHeaders)2 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)2 CredentialProvider (io.syndesis.server.credential.CredentialProvider)1 OAuth2Applicator (io.syndesis.server.credential.OAuth2Applicator)1 OAuth2CredentialProvider (io.syndesis.server.credential.OAuth2CredentialProvider)1 SalesforceApplicator (io.syndesis.server.credential.salesforce.SalesforceConfiguration.SalesforceApplicator)1 SocialProperties (org.springframework.boot.autoconfigure.social.SocialProperties)1 UserProfile (org.springframework.social.connect.UserProfile)1 OAuth2Parameters (org.springframework.social.oauth2.OAuth2Parameters)1 OAuth2Template (org.springframework.social.oauth2.OAuth2Template)1 Salesforce (org.springframework.social.salesforce.api.Salesforce)1 SalesforceConnectionFactory (org.springframework.social.salesforce.connect.SalesforceConnectionFactory)1 MultiValueMap (org.springframework.util.MultiValueMap)1