/** * Constructs and returns the final request {@code ConsumerToken} instance. * * @return the final request {@code ConsumerToken} instance */ public ConsumerToken build() { return new ConsumerToken(this); } }
private Consumer getConsumer(final String consumerKey) { return consumerService.getConsumerByKey(consumerKey) == null ? consumerService.getConsumer() : consumerService.getConsumerByKey(consumerKey); }
private Request sign(Request request, ConsumerToken token) { return consumer.sign(request, SP, token); }
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 ConsumerToken asToken(HttpServletRequest request) { return ConsumerToken.newAccessToken(request.getParameter("token")) .tokenSecret(request.getParameter("token-secret")) .consumer(consumer.getConsumer()) .build(); }
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(); } } }
private String getHostClientKey() { return consumerService.getConsumer().getKey(); }
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 Request sign(Request request, ServiceProvider serviceProvider, ConsumerToken token) { ConsumerAndSecret cas = getConsumerAndSecret(token.getConsumer().getKey()); OAuthAccessor accessor = asOAuthAccessor(token, cas, serviceProvider); return sign(request, accessor); }
public boolean removeConsumerToken(final ApplicationId applicationId, final String username) { Preconditions.checkNotNull(applicationId, "applicationLink"); verifyOAuthOutgoingEnabled(applicationId); final ConsumerTokenStore.Key key = makeOAuthApplinksConsumerKey(username, applicationId.get()); if (consumerTokenStore.get(key) != null) { consumerTokenStore.remove(key); return true; } return false; }
public static ConsumerTokenStore.Key makeOAuthApplinksConsumerKey(final String username, final String applicationLinkId) { Preconditions.checkNotNull(username, "Username cannot be null!"); Preconditions.checkNotNull(applicationLinkId, "Application Link Id cannot be null!"); return new ConsumerTokenStore.Key(applicationLinkId + ":" + username); }
@Override public void deleteTrustCertificate(final String id, final OrphanedTrustCertificate.Type type) { checkCertificateType(type); if (type == OrphanedTrustCertificate.Type.OAUTH) { serviceProviderConsumerStore.remove(id); } else if (type == OrphanedTrustCertificate.Type.OAUTH_SERVICE_PROVIDER) { consumerService.removeConsumerByKey(id); } }
/** * Static factory method that starts the process of building a request {@code ConsumerToken} instance. Returns a * {@code ConsumerTokenBuilder} so the additional token attributes can be set. * * @param token unique token used to the {@code ConsumerToken} to be used in OAuth operations * @return the builder to set additional token attributes and build the {@code ConsumerToken} */ public static ConsumerTokenBuilder newRequestToken(String token) { return new ConsumerTokenBuilder(Type.REQUEST, checkNotNull(token, "token")); }
private ConsumerToken retrieveConsumerToken(final String username) throws CredentialsRequiredException { final ConsumerToken consumerToken = consumerTokenStoreService.getConsumerToken(applicationLink, username); // token should never be a request token, we only store access tokens. if (consumerToken == null || consumerToken.isRequestToken()) { throw new CredentialsRequiredException(this, "You do not have an authorized access token for the remote resource."); } return consumerToken; }
/** * Add an Outgoing Consumer for a 3rdParty link. */ private Consumer add3rdPartyOutgoingConsumer(final ConsumerEntity consumerEntity) { final Consumer consumer = Consumer.key(consumerEntity.getKey()) .name(consumerEntity.getName()) .signatureMethod(Consumer.SignatureMethod.HMAC_SHA1) .description(consumerEntity.getDescription()) .build(); consumerService.add(consumer.getName(), consumer, consumerEntity.getSharedSecret()); return consumer; }
private String getHostClientKey() { return consumerService.getConsumer().getKey(); }
public static ConsumerTokenStore.Key makeOAuthApplinksConsumerKey(final String username, final String applicationLinkId) { checkNotNull(username, "Username cannot be null!"); checkNotNull(applicationLinkId, "Application Link Id cannot be null!"); return new ConsumerTokenStore.Key(applicationLinkId + ":" + username); } }
/** * Static factory method that starts the process of building an access {@code ConsumerToken} instance. Returns a * {@code ConsumerTokenBuilder} so the additional token attributes can be set. * * @param token unique token used to the {@code ConsumerToken} to be used in OAuth operations * @return the builder to set additional token attributes and build the {@code ConsumerToken} */ public static ConsumerTokenBuilder newAccessToken(String token) { return new ConsumerTokenBuilder(Type.ACCESS, checkNotNull(token, "token")); }
private boolean jwtWasIssuedByHost(String issuer) { try { final String consumerKey = consumerService.getConsumer().getKey(); log.debug("jwtWasIssuedByHost: consumer-key='{}', issuer='{}'", consumerKey, issuer); return consumerKey.equals(issuer); } catch(ConsumerCreationException e) { log.error("Could not get the consumer that provides issuer Id.", e); } return false; }