@Override public OAuthToken exchangeForAccessToken(AuthorizedRequestToken requestToken, MultiValueMap<String, String> additionalParameters) { return selectedOauth1Operations.exchangeForAccessToken(requestToken, additionalParameters); // delegate }
private OAuthToken fetchRequestToken(NativeWebRequest request, MultiValueMap<String, String> requestParameters, OAuth1Operations oauthOperations) { if (oauthOperations.getVersion() == OAuth1Version.CORE_10_REVISION_A) { return oauthOperations.fetchRequestToken(callbackUrl(request), requestParameters); } return oauthOperations.fetchRequestToken(null, requestParameters); }
private String buildOAuth1Url(OAuth1Operations oauthOperations, String requestToken, OAuth1Parameters parameters) { if (useAuthenticateUrl) { return oauthOperations.buildAuthenticateUrl(requestToken, parameters); } else { return oauthOperations.buildAuthorizeUrl(requestToken, parameters); } }
@Override public CredentialFlowState prepare(final String connectorId, final URI baseUrl, final URI returnUrl) { final OAuth1CredentialFlowState.Builder flowState = new OAuth1CredentialFlowState.Builder().returnUrl(returnUrl) .providerId(id); final OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations(); final OAuth1Parameters parameters = new OAuth1Parameters(); final String stateKey = UUID.randomUUID().toString(); flowState.key(stateKey); final OAuthToken oAuthToken; final OAuth1Version oAuthVersion = oauthOperations.getVersion(); if (oAuthVersion == OAuth1Version.CORE_10) { parameters.setCallbackUrl(callbackUrlFor(baseUrl, EMPTY)); oAuthToken = oauthOperations.fetchRequestToken(null, null); } else if (oAuthVersion == OAuth1Version.CORE_10_REVISION_A) { oAuthToken = oauthOperations.fetchRequestToken(callbackUrlFor(baseUrl, EMPTY), null); } else { throw new IllegalStateException("Unsupported OAuth 1 version: " + oAuthVersion); } flowState.token(oAuthToken); final String redirectUrl = oauthOperations.buildAuthorizeUrl(oAuthToken.getValue(), parameters); flowState.redirectUrl(redirectUrl); flowState.connectorId(connectorId); return flowState.build(); }
OAuthToken requestToken = ops.fetchRequestToken(returnToUrl, null); request.getSession().setAttribute(OAUTH_TOKEN_ATTRIBUTE, requestToken); if (ops.getVersion() == OAuth1Version.CORE_10) { params = new OAuth1Parameters(); params.setCallbackUrl(returnToUrl); params = OAuth1Parameters.NONE; throw new SocialAuthenticationRedirectException(ops.buildAuthenticateUrl(requestToken.getValue(), params)); } else { return null; OAuthToken accessToken = getConnectionFactory().getOAuthOperations().exchangeForAccessToken(new AuthorizedRequestToken(requestToken, verifier), null);
@Override public OAuth1Version getVersion() { return selectedOauth1Operations.getVersion(); // delegate }
@Override public OAuthToken fetchRequestToken(String callbackUrl, MultiValueMap<String, String> additionalParameters) { return selectedOauth1Operations.fetchRequestToken(callbackUrl, additionalParameters); // delegate }
@Override public String buildAuthenticateUrl(String requestToken, OAuth1Parameters parameters) { return selectedOauth1Operations.buildAuthorizeUrl(requestToken, parameters); // delegate }
@Override public CredentialFlowState prepare(final String connectorId, final URI baseUrl, final URI returnUrl) { final OAuth1CredentialFlowState.Builder flowState = new OAuth1CredentialFlowState.Builder().returnUrl(returnUrl) .providerId(id); final OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations(); final OAuth1Parameters parameters = new OAuth1Parameters(); final String stateKey = UUID.randomUUID().toString(); flowState.key(stateKey); final OAuthToken oAuthToken; final OAuth1Version oAuthVersion = oauthOperations.getVersion(); if (oAuthVersion == OAuth1Version.CORE_10) { parameters.setCallbackUrl(callbackUrlFor(baseUrl, EMPTY)); oAuthToken = oauthOperations.fetchRequestToken(null, null); } else if (oAuthVersion == OAuth1Version.CORE_10_REVISION_A) { oAuthToken = oauthOperations.fetchRequestToken(callbackUrlFor(baseUrl, EMPTY), null); } else { throw new IllegalStateException("Unsupported OAuth 1 version: " + oAuthVersion); } flowState.token(oAuthToken); final String redirectUrl = oauthOperations.buildAuthorizeUrl(oAuthToken.getValue(), parameters); flowState.redirectUrl(redirectUrl); flowState.connectorId(connectorId); return flowState.build(); }
OAuthToken requestToken = ops.fetchRequestToken(returnToUrl, null); request.getSession().setAttribute(OAUTH_TOKEN_ATTRIBUTE, requestToken); if (ops.getVersion() == OAuth1Version.CORE_10) { params = new OAuth1Parameters(); params.setCallbackUrl(returnToUrl); params = OAuth1Parameters.NONE; throw new SocialAuthenticationRedirectException(ops.buildAuthenticateUrl(requestToken.getValue(), params)); } else { return null; OAuthToken accessToken = getConnectionFactory().getOAuthOperations().exchangeForAccessToken(new AuthorizedRequestToken(requestToken, verifier), null);
private String buildOAuth1Url(OAuth1ConnectionFactory<?> connectionFactory, NativeWebRequest request, MultiValueMap<String, String> additionalParameters) { OAuth1Operations oauthOperations = connectionFactory.getOAuthOperations(); MultiValueMap<String, String> requestParameters = getRequestParameters(request); OAuth1Parameters parameters = getOAuth1Parameters(request, additionalParameters); parameters.putAll(requestParameters); if (oauthOperations.getVersion() == OAuth1Version.CORE_10) { parameters.setCallbackUrl(callbackUrl(request)); } OAuthToken requestToken = fetchRequestToken(request, requestParameters, oauthOperations); sessionStrategy.setAttribute(request, OAUTH_TOKEN_ATTRIBUTE, requestToken); return buildOAuth1Url(oauthOperations, requestToken.getValue(), parameters); }
@Override public String buildAuthorizeUrl(String requestToken, OAuth1Parameters parameters) { return selectedOauth1Operations.buildAuthorizeUrl(requestToken, parameters); // delegate }
@Test public void shouldAcquireOAuth1Credentials() { final OAuth1ConnectionFactory<?> oauth1 = mock(OAuth1ConnectionFactory.class); @SuppressWarnings("unchecked") final Applicator<OAuthToken> applicator = mock(Applicator.class); when(locator.providerWithId("providerId")) .thenReturn(new OAuth1CredentialProvider<>("providerId", oauth1, applicator)); final OAuth1Operations operations = mock(OAuth1Operations.class); when(oauth1.getOAuthOperations()).thenReturn(operations); when(operations.getVersion()).thenReturn(OAuth1Version.CORE_10); final OAuthToken token = new OAuthToken("value", "secret"); when(operations.fetchRequestToken(null, null)).thenReturn(token); final ArgumentCaptor<OAuth1Parameters> parameters = ArgumentCaptor.forClass(OAuth1Parameters.class); when(operations.buildAuthorizeUrl(eq("value"), 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 OAuth1CredentialFlowState.Builder().providerId("providerId") .redirectUrl("https://provider.io/oauth/authorize").returnUrl(URI.create("/ui#state")).token(token).build(); final AcquisitionFlow expected = new AcquisitionFlow.Builder().type(Type.OAUTH1) .redirectUrl("https://provider.io/oauth/authorize").state(expectedFlowState).build(); assertThat(acquisition).isEqualToIgnoringGivenFields(expected, "state"); final Optional<CredentialFlowState> maybeState = acquisition.state(); assertThat(maybeState).isPresent(); final CredentialFlowState state = maybeState.get(); assertThat(state).isEqualToIgnoringGivenFields(expectedFlowState, "key"); assertThat(state.getKey()).isNotNull(); final OAuth1Parameters oAuth1Parameters = parameters.getValue(); assertThat(oAuth1Parameters.getCallbackUrl()).isEqualTo("https://syndesis.io/api/v1/credentials/callback"); }
/** * Complete the connection to the OAuth1 provider. * @param connectionFactory the service provider's connection factory e.g. FacebookConnectionFactory * @param request the current web request * @return a new connection to the service provider */ public Connection<?> completeConnection(OAuth1ConnectionFactory<?> connectionFactory, NativeWebRequest request) { String verifier = request.getParameter("oauth_verifier"); AuthorizedRequestToken requestToken = new AuthorizedRequestToken(extractCachedRequestToken(request), verifier); OAuthToken accessToken = connectionFactory.getOAuthOperations().exchangeForAccessToken(requestToken, null); return connectionFactory.createConnection(accessToken); }
when(operations.getVersion()).thenReturn(OAuth1Version.CORE_10_REVISION_A); final OAuthToken token = new OAuthToken("value", "secret"); when(operations.fetchRequestToken("https://syndesis.io/api/v1/credentials/callback", null)).thenReturn(token); when(operations.buildAuthorizeUrl(eq("value"), parameters.capture())) .thenReturn("https://provider.io/oauth/authorize");
@Override public CredentialFlowState finish(final CredentialFlowState givenFlowState, final URI baseUrl) { final OAuth1CredentialFlowState flowState = flowState(givenFlowState); final AuthorizedRequestToken requestToken = new AuthorizedRequestToken(flowState.getToken(), flowState.getVerifier()); final OAuthToken accessToken = connectionFactory.getOAuthOperations().exchangeForAccessToken(requestToken, null); return new OAuth1CredentialFlowState.Builder().createFrom(flowState).accessToken(accessToken).build(); }
@Override public CredentialFlowState finish(final CredentialFlowState givenFlowState, final URI baseUrl) { final OAuth1CredentialFlowState flowState = flowState(givenFlowState); final AuthorizedRequestToken requestToken = new AuthorizedRequestToken(flowState.getToken(), flowState.getVerifier()); final OAuthToken accessToken = connectionFactory.getOAuthOperations().exchangeForAccessToken(requestToken, null); return new OAuth1CredentialFlowState.Builder().createFrom(flowState).accessToken(accessToken).build(); }
@Test public void shouldFinishOAuth1Acquisition() { final OAuthToken token = new OAuthToken("value", "secret"); final OAuth1ConnectionFactory<?> oauth1 = mock(OAuth1ConnectionFactory.class); final OAuth1Applicator applicator = new OAuth1Applicator(properties); when(locator.providerWithId("providerId")) .thenReturn(new OAuth1CredentialProvider<>("providerId", oauth1, applicator)); final OAuth1Operations operations = mock(OAuth1Operations.class); when(oauth1.getOAuthOperations()).thenReturn(operations); final ArgumentCaptor<AuthorizedRequestToken> requestToken = ArgumentCaptor .forClass(AuthorizedRequestToken.class); final OAuthToken accessToken = new OAuthToken("tokenValue", "tokenSecret"); @SuppressWarnings({"unchecked", "rawtypes"}) final Class<MultiValueMap<String, String>> multimapType = (Class) MultiValueMap.class; when(operations.exchangeForAccessToken(requestToken.capture(), isNull(multimapType))).thenReturn(accessToken); applicator.setAccessTokenSecretProperty("accessTokenSecretProperty"); applicator.setAccessTokenValueProperty("accessTokenValueProperty"); applicator.setConsumerKeyProperty("consumerKeyProperty"); applicator.setConsumerSecretProperty("consumerSecretProperty"); final CredentialFlowState flowState = new OAuth1CredentialFlowState.Builder().providerId("providerId") .token(token).returnUrl(URI.create("/ui#state")).verifier("verifier").build(); final CredentialFlowState finalFlowState = credentials.finishAcquisition(flowState, URI.create("https://www.example.com")); final AuthorizedRequestToken capturedRequestToken = requestToken.getValue(); assertThat(capturedRequestToken.getValue()).isEqualTo("value"); assertThat(capturedRequestToken.getSecret()).isEqualTo("secret"); assertThat(capturedRequestToken.getVerifier()).isEqualTo("verifier"); assertThat(finalFlowState) .isEqualTo(new OAuth1CredentialFlowState.Builder().createFrom(flowState).accessToken(accessToken).build()); }