public ConsumerToken getAccessToken(ServiceProvider serviceProvider, ConsumerToken requestTokenPair, String requestVerifier, final String consumerKey) throws ResponseException { final List<Request.Parameter> parameters = new ArrayList<Request.Parameter>(); parameters.add(new Request.Parameter(OAuth.OAUTH_TOKEN, requestTokenPair.getToken())); if (StringUtils.isNotBlank(requestVerifier)) // Added in OAuth 1.0a { parameters.add(new Request.Parameter(OAuth.OAUTH_VERIFIER, requestVerifier)); } final Request oAuthRequest = new Request(Request.HttpMethod.POST, serviceProvider.getAccessTokenUri(), parameters); final Request signedRequest = consumerService.sign(oAuthRequest, serviceProvider, requestTokenPair); final TokenAndSecret tokenAndSecret = requestToken(serviceProvider.getAccessTokenUri().toString(), signedRequest); ConsumerToken accessToken = ConsumerToken.newAccessToken(tokenAndSecret.token).tokenSecret(tokenAndSecret.secret).consumer(getConsumer(consumerKey)).build(); assert (accessToken.isAccessToken()); return accessToken; }
public void addConsumerToken(final ApplicationLink applicationLink, final String username, final ConsumerToken consumerToken) { Preconditions.checkNotNull(applicationLink, "applicationLink"); Preconditions.checkNotNull(username, "username"); Preconditions.checkNotNull(consumerToken, "consumerToken"); verifyOAuthOutgoingEnabled(applicationLink.getId()); final Map<String, String> tokenProperties = new HashMap<String, String>(); tokenProperties.put(APPLINKS_APPLICATION_LINK_ID, applicationLink.getId().get()); final ConsumerTokenStore.Key key = makeOAuthApplinksConsumerKey(username, applicationLink.getId().get()); ConsumerToken.ConsumerTokenBuilder tokenBuilder; if (consumerToken.isAccessToken()) { tokenBuilder = ConsumerToken.newAccessToken(consumerToken.getToken()); } else { tokenBuilder = ConsumerToken.newRequestToken(consumerToken.getToken()); } tokenBuilder.tokenSecret(consumerToken.getTokenSecret()).consumer(consumerToken.getConsumer()).properties(tokenProperties); consumerTokenStore.put(key, tokenBuilder.build()); }
private void getAccessToken(String requestToken, final ApplicationLink applicationLink, final HttpServletRequest request) throws ResponseException { final String username = getRemoteUsername(request); final ConsumerToken requestTokenPair = consumerTokenStoreService.getConsumerToken(applicationLink, username); if (requestTokenPair == null) { throw new ResponseException("Cannot get access token as no request token pair can be found"); } if (requestTokenPair.isAccessToken()) { //Already has access token. return; } if (!requestToken.equals(requestTokenPair.getToken())) { throw new ResponseException("The oauth_token in the request is not the same as the token persisted in the system."); } final Map<String, String> config = authenticationConfigurationManager.getConfiguration(applicationLink.getId(), OAuthAuthenticationProvider.class); final ServiceProvider serviceProvider = ServiceProviderUtil.getServiceProvider(config, applicationLink); final String requestVerifier = request.getParameter(OAuth.OAUTH_VERIFIER); final String consumerKey = getConsumerKey(applicationLink); final ConsumerToken accessToken = oAuthTokenRetriever.getAccessToken(serviceProvider, requestTokenPair, requestVerifier, consumerKey); consumerTokenStoreService.removeConsumerToken(applicationLink.getId(), username); consumerTokenStoreService.addConsumerToken(applicationLink, username, accessToken); }