org.springframework.social.connect.support.OAuth2ConnectionFactory Java Examples

The following examples show how to use org.springframework.social.connect.support.OAuth2ConnectionFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OAuth2CredentialProviderTest.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCreateAcquisitionMethod() {
    @SuppressWarnings("unchecked")
    final OAuth2CredentialProvider<?> oauth2 = new OAuth2CredentialProvider<>("provider2",
        mock(OAuth2ConnectionFactory.class), mock(Applicator.class), Collections.emptyMap());

    final AcquisitionMethod method2 = new AcquisitionMethod.Builder()
        .description("provider2")
        .label("provider2")
        .icon("provider2")
        .type(Type.OAUTH2)
        .configured(true)
        .build();

    assertThat(oauth2.acquisitionMethod()).isEqualTo(method2);
}
 
Example #2
Source File: AlipayConnectConfiguration.java    From cola with MIT License 5 votes vote down vote up
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
	if (bean.getClass().equals(SocialAuthenticationServiceRegistry.class)) {
		SocialAuthenticationServiceRegistry registry = (SocialAuthenticationServiceRegistry) bean;
		AlipayOAuth2AuthenticationService alipayOAuth2AuthenticationService = new AlipayOAuth2AuthenticationService((OAuth2ConnectionFactory) createConnectionFactory());
		registry.addAuthenticationService(alipayOAuth2AuthenticationService);
	}
	return bean;
}
 
Example #3
Source File: ResourceServerTokenServicesConfiguration.java    From spring-security-oauth2-boot with Apache License 2.0 5 votes vote down vote up
public SocialTokenServicesConfiguration(ResourceServerProperties sso,
		ObjectProvider<OAuth2ConnectionFactory<?>> connectionFactory,
		UserInfoRestTemplateFactory restTemplateFactory,
		ObjectProvider<AuthoritiesExtractor> authoritiesExtractor,
		ObjectProvider<PrincipalExtractor> principalExtractor) {
	this.sso = sso;
	this.connectionFactory = connectionFactory.getIfAvailable();
	this.restTemplate = restTemplateFactory.getUserInfoRestTemplate();
	this.authoritiesExtractor = authoritiesExtractor.getIfAvailable();
	this.principalExtractor = principalExtractor.getIfAvailable();
}
 
Example #4
Source File: TestCredentialProviderFactory.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@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, Collections.emptyMap());

    final OAuth2Operations operations = spy(new OAuth2Template("testClientId", "testClientSecret",
        "https://test/oauth2/authorize", "https://test/oauth2/token"));
    doReturn(new AccessGrant("token")).when(operations).exchangeForAccess(ArgumentMatchers.anyString(),
        ArgumentMatchers.anyString(), ArgumentMatchers.isNull());

    when(connectionFactory.getOAuthOperations()).thenReturn(operations);

    return credentialProvider;
}
 
Example #5
Source File: OAuth2CredentialProviderFactory.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Override
public CredentialProvider create(final SocialProperties properties) {
    if (properties instanceof UnconfiguredProperties) {
        return new OAuth2CredentialProvider<>(OAUTH_2);
    }

    if (!(properties instanceof OAuth2ConnectorProperties)) {
        throw new IllegalArgumentException(String.format("Unsupported social properties instance - " +
                "expected properties of type %s, but found %s", OAuth2ConnectorProperties.class, properties.getClass()));
    }

    final OAuth2ConnectorProperties oauth2Properties = (OAuth2ConnectorProperties) properties;

    final String appId = oauth2Properties.getAppId();
    final String appSecret = oauth2Properties.getAppSecret();
    final String authorizationUrl = oauth2Properties.getAuthorizationUrl();
    final String authenticationUrl = oauth2Properties.getAuthenticationUrl();
    final String accessTokenUrl = oauth2Properties.getAccessTokenUrl();
    final boolean useParametersForClientCredentials = oauth2Properties.isUseParametersForClientCredentials();
    final TokenStrategy tokenStrategy = oauth2Properties.getTokenStrategy();
    final String scope = oauth2Properties.getScope();

    final OAuth2ServiceProvider<RestOperations> serviceProvider = new GenericOAuth2ServiceProvider(appId, appSecret, authorizationUrl,
        authenticationUrl, accessTokenUrl, useParametersForClientCredentials, tokenStrategy);

    final OAuth2ConnectionFactory<RestOperations> connectionFactory = new OAuth2ConnectionFactory<>(OAUTH_2, serviceProvider, null);
    connectionFactory.setScope(scope);

    final OAuth2Applicator applicator = new OAuth2Applicator(properties);
    applicator.setAccessTokenProperty("accessToken");
    applicator.setAccessTokenExpiresAtProperty("accessTokenExpiresAt");

    applicator.setRefreshTokenProperty("refreshToken");
    applicator.setClientIdProperty("clientId");
    applicator.setClientSecretProperty("clientSecret");

    return new OAuth2CredentialProvider<>(OAUTH_2, connectionFactory, applicator, oauth2Properties.getAdditionalQueryParameters());
}
 
Example #6
Source File: OAuth2CredentialProvider.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private OAuth2CredentialProvider(final String id, final OAuth2ConnectionFactory<S> connectionFactory, final Applicator<AccessGrant> applicator,
    final Map<String, String> additionalQueryParameters, final boolean configured) {
    this.id = id;
    this.connectionFactory = connectionFactory;
    this.applicator = applicator;
    this.configured = configured;
    this.additionalQueryParameters = additionalQueryParameters.entrySet().stream()
        .collect(Collectors.toMap(Map.Entry::getKey, e -> Collections.singletonList(e.getValue())));
}
 
Example #7
Source File: CredentialsTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@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, Collections.emptyMap()));

    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");
}
 
Example #8
Source File: CredentialsTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAcquireOAuth2CredentialsWithAdditionalQueryParameters() {
    final OAuth2ConnectionFactory<?> oauth2 = mock(OAuth2ConnectionFactory.class);
    @SuppressWarnings("unchecked")
    final Applicator<AccessGrant> applicator = mock(Applicator.class);
    final Map<String, String> queryParameters = new HashMap<>();
    queryParameters.put("q1", "v1");
    queryParameters.put("q2", "v2");
    when(locator.providerWithId("providerId"))
        .thenReturn(new OAuth2CredentialProvider<>("providerId", oauth2, applicator, queryParameters));

    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");
    assertThat(capturedParameters.get("q1")).containsOnly("v1");
    assertThat(capturedParameters.get("q2")).containsOnly("v2");
}
 
Example #9
Source File: CredentialsTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@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, Collections.emptyMap()));
    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());
}
 
Example #10
Source File: AlipayOAuth2AuthenticationService.java    From cola with MIT License 4 votes vote down vote up
public AlipayOAuth2AuthenticationService(OAuth2ConnectionFactory<Alipay> connectionFactory) {
	super(connectionFactory);
}
 
Example #11
Source File: SpringSocialTokenServices.java    From spring-security-oauth2-boot with Apache License 2.0 4 votes vote down vote up
public SpringSocialTokenServices(OAuth2ConnectionFactory<?> connectionFactory, String clientId) {
	this.connectionFactory = connectionFactory;
	this.clientId = clientId;
}
 
Example #12
Source File: OAuth2CredentialProvider.java    From syndesis with Apache License 2.0 4 votes vote down vote up
public OAuth2CredentialProvider(final String id, final OAuth2ConnectionFactory<S> connectionFactory, final Applicator<AccessGrant> applicator,
    final Map<String, String> additionalQueryParameters) {
    this(id, connectionFactory, applicator, additionalQueryParameters, true);
}