Java源码示例:org.springframework.social.connect.support.OAuth2ConnectionFactory
示例1
@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);
}
示例2
@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;
}
示例3
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();
}
示例4
@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;
}
示例5
@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());
}
示例6
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())));
}
示例7
@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");
}
示例8
@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");
}
示例9
@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());
}
示例10
public AlipayOAuth2AuthenticationService(OAuth2ConnectionFactory<Alipay> connectionFactory) {
super(connectionFactory);
}
示例11
public SpringSocialTokenServices(OAuth2ConnectionFactory<?> connectionFactory, String clientId) {
this.connectionFactory = connectionFactory;
this.clientId = clientId;
}
示例12
public OAuth2CredentialProvider(final String id, final OAuth2ConnectionFactory<S> connectionFactory, final Applicator<AccessGrant> applicator,
final Map<String, String> additionalQueryParameters) {
this(id, connectionFactory, applicator, additionalQueryParameters, true);
}