/** * Gets the authorized {@link OAuthAccessor} that is identified by the given * access token. Any changes made to the accessor's fields, except the * consumer, will not be reflected in this container. * * @param accessToken the access token used for identification. * @throws OAuthProblemException if the token does not map to an accessor. */ public OAuthAccessor getAccessTokenAccessor(String accessToken) throws OAuthProblemException { OAuthAccessor accessor = accessTokenAccessors.get(accessToken); if (accessor == null) { OAuthProblemException exception = OAuthUtil.newOAuthProblemException(OAuth.Problems.TOKEN_REJECTED); exception.setParameter(OAuth.OAUTH_TOKEN, accessToken); throw exception; } return accessor.clone(); }
/** * Gets the {@link OAuthAccessor} that is identified by the given request * token. Any changes made to the accessor's fields, except the consumer, will * not be reflected in this container. * * @param requestToken the request token used for identification. * @throws OAuthProblemException if the token does not map to an accessor. */ public OAuthAccessor getRequestTokenAccessor(String requestToken) throws OAuthProblemException { OAuthAccessor accessor = requestTokenAccessors.get(requestToken); if (accessor == null) { OAuthProblemException exception = OAuthUtil.newOAuthProblemException(OAuth.Problems.TOKEN_REJECTED); exception.setParameter(OAuth.OAUTH_TOKEN, requestToken); throw exception; } return accessor.clone(); }
/** * Rejects authorization of a request token. * * @param requestToken the request token used for identification. * @throws OAuthProblemException if the request token does not map to an * accessor or if the token was already used. */ public void rejectRequestToken(String requestToken) throws OAuthProblemException { OAuthAccessor accessor = getRequestTokenAccessor(requestToken); if (accessor.getProperty(USER_PROPERTY_NAME) != null) { throw OAuthUtil.newOAuthProblemException(OAuth.Problems.TOKEN_USED); } // Can't use remove(String, OAuthAccessor) since equals is not defined. requestTokenAccessors.remove(requestToken); LOG.info("Rejected request token " + requestToken); }
/** * Authorizes a request token to be exchanged for an access token. * * @param requestToken the request token used for identification. * @param user the user that has authorized the token. * @throws OAuthProblemException if the request token does not map to an * accessor or if the token was already used. */ public OAuthAccessor authorizeRequestToken(String requestToken, ParticipantId user) throws OAuthProblemException { Preconditions.checkNotNull(user, "User must not be null"); OAuthAccessor accessor = getRequestTokenAccessor(requestToken); if (accessor.getProperty(USER_PROPERTY_NAME) != null) { throw OAuthUtil.newOAuthProblemException(OAuth.Problems.TOKEN_USED); } accessor.setProperty(USER_PROPERTY_NAME, user); requestTokenAccessors.put(requestToken, accessor); LOG.info("Authorized request token for " + user); return accessor.clone(); }
/** * Authorize the {@link OAuthAccessor} by generating a new access token and * token secret. * * @param requestToken the requestToken used for identifying the accessor that * needs to be authorized. * @return a new {@link OAuthAccessor} with the access token and token secret * set. * @throws OAuthProblemException if the request token in the accessor is not * known. */ public OAuthAccessor generateAccessToken(String requestToken) throws OAuthProblemException { OAuthAccessor accessor = getRequestTokenAccessor(requestToken); if (accessor.getProperty(USER_PROPERTY_NAME) == null) { // User has not given the consumer permission yet. throw OAuthUtil.newOAuthProblemException(OAuth.Problems.PERMISSION_UNKNOWN); } // Token secret does not need to unique so can be generated now. accessor.tokenSecret = generateToken(); do { accessor.accessToken = generateToken(); } while (accessTokenAccessors.putIfAbsent(accessor.accessToken, accessor) != null); requestTokenAccessors.remove(accessor.requestToken); LOG.info("Generated access token for " + accessor.getProperty(USER_PROPERTY_NAME)); return accessor.clone(); }