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()); }
public static Matcher<? super ConsumerToken> equalTo(ConsumerToken token) { // Hamcrest's is/equalTo matcher uses Object.equals so Consumer properties // need to be explicitly checked // Note: token properties excluded from match as they do not survive conversion // from OAuthAccessors return allOf(hasProperty("consumer", samePropertyValuesAs(token.getConsumer())), hasProperty("token", is(token.getToken())), hasProperty("tokenSecret", is(token.getTokenSecret()))); }
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; }
/** * Converts an {@code OAuthAccessor} to a {@code ConsumerToken}. The token will be set to either an access * or request token depending on whether the {@code accessToken} or {@code requestToken} field is set on the * {@code accessor}. * * <p>Warning: It is impossible to get all the properties that have been set on an {@code OAuthAccessor} so we * cannot copy them into the {@code ConsumerToken}. * * @param accessor {@code OAuthAccessor} to convert to a {@code ConsumerToken} * @return {@code ConsumerToken} converted from the {@code OAuthAccessor} */ public static ConsumerToken asConsumerToken(OAuthAccessor accessor) { checkNotNull(accessor, "accessor"); Consumer consumer = Consumers.fromOAuthConsumer(accessor.consumer); if (accessor.accessToken != null) { return ConsumerToken.newAccessToken(accessor.accessToken) .tokenSecret(accessor.tokenSecret) .consumer(consumer) .build(); } else { return ConsumerToken.newRequestToken(accessor.requestToken) .tokenSecret(accessor.tokenSecret) .consumer(consumer) .build(); } } }
/** * Converts a {@code ConsumerToken} to an {@code OAuthAccessor}. Sets the {@code requestToken} or {@code accessToken} * accordingly to the type of the {@code ConsumerToken}. Also uses the {@code sharedSecret} when converting the * {@code Consumer} to make the returned {@code OAuthAccessor} suitable for signing requests. * * @param token {@code ConsumerToken} to convert to {@code OAuthAccessor} * @param sharedSecret shared secret to use when converting the {@code Consumer} * @param serviceProvider {@code OAuthServiceProvider} for the application * @return {@code OAuthAccessor} converted from the {@code ConsumerToken}, suitable for signing requests */ public static OAuthAccessor asOAuthAccessor(ConsumerToken token, String sharedSecret, ServiceProvider serviceProvider) { checkNotNull(token, "token"); checkNotNull(serviceProvider, "serviceProvider"); OAuthAccessor accessor = new OAuthAccessor(Consumers.asOAuthConsumer(token.getConsumer(), sharedSecret, serviceProvider)); Tokens.setCommonTokenData(accessor, token); return accessor; }
private ConsumerToken asToken(HttpServletRequest request) { return ConsumerToken.newAccessToken(request.getParameter("token")) .tokenSecret(request.getParameter("token-secret")) .consumer(consumer.getConsumer()) .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); }
public ConsumerToken getRequestToken(ServiceProvider serviceProvider, final String consumerKey, String callback) throws ResponseException { final Request oAuthRequest = new Request(Request.HttpMethod.POST, serviceProvider.getRequestTokenUri(), Collections.singleton(new Request.Parameter(OAuth.OAUTH_CALLBACK, callback))); final Request signedRequest = consumerService.sign(oAuthRequest, consumerKey, serviceProvider); final com.atlassian.sal.api.net.Request tokenRequest = requestFactory.createRequest( com.atlassian.sal.api.net.Request.MethodType.POST, serviceProvider.getRequestTokenUri().toString()); tokenRequest.addRequestParameters(parameterToStringArray(signedRequest.getParameters())); final TokenAndSecret tokenAndSecret = requestToken(serviceProvider.getRequestTokenUri().toString(), signedRequest); final ConsumerToken requestToken = ConsumerToken.newRequestToken(tokenAndSecret.token) .tokenSecret(tokenAndSecret.secret) .consumer(getConsumer(consumerKey)) .build(); assert (requestToken.isRequestToken()); return requestToken; }
@Override protected Request createUnsignedRequest() { // a 3LO request needs a valid OAuth token. return new com.atlassian.oauth.Request(toOAuthMethodType(methodType), URI.create(url), toOAuthParameters(consumerToken.getToken())); }
/** * Converts a {@code ConsumerToken} to an {@code OAuthAccessor}, setting the {@code requestToken} or * {@code accessToken} accordingly to the type of the {@code ConsumerToken}. * * @param token {@code ConsumerToken} to convert to {@code OAuthAccessor} * @param serviceProvider {@code OAuthServiceProvider} for the application * @return {@code OAuthAccessor} converted from the {@code ConsumerToken} */ public static OAuthAccessor asOAuthAccessor(ConsumerToken token, ServiceProvider serviceProvider) { checkNotNull(token, "token"); checkNotNull(serviceProvider, "serviceProvider"); OAuthAccessor accessor = new OAuthAccessor(Consumers.asOAuthConsumer(token.getConsumer(), serviceProvider)); Tokens.setCommonTokenData(accessor, token); return accessor; }
@PUT @Path("consumer/{applinkId}/{username}") public Response createConsumerAccessToken(@PathParam("applinkId") String applinkId, @PathParam("username") String username, RestAccessToken restToken) throws TypeNotInstalledException { ApplicationId applicationId = new ApplicationId(applinkId); ApplicationLink link = getApplicationLink(applicationId); ConsumerToken token = ConsumerToken.newAccessToken(restToken.getToken()) .tokenSecret(restToken.getTokenSecret()) .consumer(consumerService.getConsumer()) // this app's consumer .build(); consumerTokenStoreService.removeConsumerToken(applicationId, username); consumerTokenStoreService.addConsumerToken(link, username, token); return Response.created(URI.create("")) .entity(new RestAccessToken(token)) .build(); }
private void obtainAndAuthorizeRequestToken(final ApplicationLink applicationLink, final HttpServletResponse resp, final HttpServletRequest req) throws ResponseException, IOException { final Map<String, String> config = authenticationConfigurationManager.getConfiguration(applicationLink.getId(), OAuthAuthenticationProvider.class); final ServiceProvider serviceProvider = ServiceProviderUtil.getServiceProvider(config, applicationLink); final String consumerKey = getConsumerKey(applicationLink); final String redirectUrl = getRedirectUrl(req); URI baseUrl = RequestUtil.getBaseURLFromRequest(req, internalHostApplication.getBaseUrl()); final String redirectToMeUrl = baseUrl + ServletPathConstants.APPLINKS_SERVLETS_PATH + "/oauth/login-dance/" + ACCESS_PATH + "?" + APPLICATION_LINK_ID_PARAM + "=" + applicationLink.getId() + (redirectUrl != null ? "&" + REDIRECT_URL_PARAM + "=" + URLEncoder.encode(redirectUrl, "UTF-8") : ""); final ConsumerToken requestToken = oAuthTokenRetriever.getRequestToken(serviceProvider, consumerKey, redirectToMeUrl); consumerTokenStoreService.addConsumerToken(applicationLink, getRemoteUsername(req), requestToken); Map<String, String> parameters = new HashMap<String, String>(); parameters.put(OAuth.OAUTH_TOKEN, requestToken.getToken()); parameters.put(OAuth.OAUTH_CALLBACK, redirectToMeUrl); resp.sendRedirect(serviceProvider.getAuthorizeUri() + "?" + OAuth.formEncode(parameters.entrySet())); }
/** * Converts a {@code ConsumerToken} to an {@code OAuthAccessor}. Sets the {@code requestToken} or {@code accessToken} * accordingly to the type of the {@code ConsumerToken}. Also uses the {@code privateKey} when converting the * {@code Consumer} to make the returned {@code OAuthAccessor} suitable for signing requests. * * @param token {@code ConsumerToken} to convert to {@code OAuthAccessor} * @param privateKey {@code PrivateKey} to use when converting the {@code Consumer} * @param serviceProvider {@code OAuthServiceProvider} for the application * @return {@code OAuthAccessor} converted from the {@code ConsumerToken}, suitable for signing requests */ public static OAuthAccessor asOAuthAccessor(ConsumerToken token, PrivateKey privateKey, ServiceProvider serviceProvider) { checkNotNull(token, "token"); checkNotNull(serviceProvider, "serviceProvider"); OAuthAccessor accessor = new OAuthAccessor(Consumers.asOAuthConsumer(token.getConsumer(), privateKey, serviceProvider)); Tokens.setCommonTokenData(accessor, token); return accessor; }
public Request sign(Request request, ServiceProvider serviceProvider, ConsumerToken token) { ConsumerAndSecret cas = getConsumerAndSecret(token.getConsumer().getKey()); OAuthAccessor accessor = asOAuthAccessor(token, cas, serviceProvider); return sign(request, accessor); }