private void redirectBackToConsumerVersion1(HttpServletRequest request, HttpServletResponse response, ServiceProviderToken token) throws IOException { String callback = request.getParameter(OAUTH_CALLBACK); if (isEmpty(callback) && token.getConsumer().getCallback() != null) { callback = token.getConsumer().getCallback().toString(); } if (isEmpty(callback)) { // no call back it must be a client response.setContentType("text/html"); if (token.getAuthorization() == Authorization.AUTHORIZED) { // no call back, display the verification code so the user can enter it manually templateRenderer.render(AUTH_NO_CALLBACK_APPROVAL_V1_TEMPLATE, ImmutableMap.<String, Object>of("token", token), response.getWriter()); } else { templateRenderer.render(AUTH_NO_CALLBACK_DENIED_TEMPLATE, ImmutableMap.<String, Object>of("token", token), response.getWriter()); } } else { if (token.getToken() != null) { callback = addParameters(callback, "oauth_token", token.getToken()); } response.sendRedirect(callback); } }
private void redirectBackToConsumerVersion1a(HttpServletRequest request, HttpServletResponse response, ServiceProviderToken token) throws IOException { URI callback = token.getCallback() == null ? token.getConsumer().getCallback() : token.getCallback(); if (callback == null) { response.setContentType("text/html"); if (token.getAuthorization() == Authorization.AUTHORIZED) { // no call back, display the verification code so the user can enter it manually templateRenderer.render(AUTH_NO_CALLBACK_APPROVAL_V1A_TEMPLATE, ImmutableMap.<String, Object>of("token", token), response.getWriter()); } else { templateRenderer.render(AUTH_NO_CALLBACK_DENIED_TEMPLATE, ImmutableMap.<String, Object>of("token", token), response.getWriter()); } } else { // add the token and verifier parameters to the callback and send the redirect // if the token was denied, then the verifier is set to a placeholder value in case consumers don't handle // a blank or missing verifier parameter response.sendRedirect(addParameters(callback.toString(), OAUTH_TOKEN, token.getToken(), OAUTH_VERIFIER, token.getAuthorization() == Authorization.AUTHORIZED ? token.getVerifier() : "denied") ); } }
public static Matcher<? super ServiceProviderToken> equalTo(ServiceProviderToken token) { // Hamcrest's is/equalTo matcher uses Object.equals so Consumer and Session properties // need to be explicitly checked Consumer consumer = token.getConsumer(); Matcher<?> consumerMatcher = consumer == null ? nullValue() : samePropertyValuesAs(consumer); ServiceProviderToken.Session session = token.getSession(); Matcher<?> sessionMatcher = session == null ? nullValue() : samePropertyValuesAs(session); return allOf(hasProperty("consumer", consumerMatcher), hasProperty("session", sessionMatcher), hasProperty("authorization", is(token.getAuthorization())), hasProperty("callback", is(token.getCallback())), hasProperty("creationTime", is(token.getCreationTime())), hasProperty("timeToLive", is(token.getTimeToLive())), hasProperty("user", is(token.getUser())), hasProperty("verifier", is(token.getVerifier())), hasProperty("version", is(token.getVersion())), hasProperty("properties", is(token.getProperties())), hasProperty("token", is(token.getToken())), hasProperty("tokenSecret", is(token.getTokenSecret()))); }
String newCallback = OAuth.addParameters(callback.toString(), OAuth.OAUTH_TOKEN, token.getToken()); if (token.getVersion() == ServiceProviderToken.Version.V_1_0_A) { newCallback = OAuth.addParameters(newCallback, OAuth.OAUTH_VERIFIER, token.getAuthorization() == ServiceProviderToken .Authorization.AUTHORIZED ? token.getVerifier() : "denied"); } else { String view; if (token.getAuthorization() == ServiceProviderToken.Authorization.AUTHORIZED) { view = "approved-authorization.jelly"; } else {
public ServiceProviderToken getTokenForAuthorization(HttpServletRequest request) throws OAuthProblemException, IOException { OAuthMessage requestMessage = OAuthServlet.getMessage(request, null); requestMessage.requireParameters(OAUTH_TOKEN); ServiceProviderToken token; try { token = store.get(requestMessage.getToken()); } catch (InvalidTokenException e) { throw new OAuthProblemException(TOKEN_REJECTED); } if (token == null || token.isAccessToken()) { throw new OAuthProblemException(TOKEN_REJECTED); } if (token.getAuthorization() == Authorization.AUTHORIZED || token.getAuthorization() == Authorization.DENIED) { throw new OAuthProblemException(TOKEN_USED); } if (token.hasExpired(clock)) { throw new OAuthProblemException(TOKEN_EXPIRED); } return token; }
private void checkRequestToken(OAuthMessage requestMessage, ServiceProviderToken token) throws Exception { if (token.hasExpired(clock)) { throw new OAuthProblemException(TOKEN_EXPIRED); } if (token.getAuthorization() == Authorization.NONE) { throw new OAuthProblemException(PERMISSION_UNKNOWN); } if (token.getAuthorization() == Authorization.DENIED) { throw new OAuthProblemException(PERMISSION_DENIED); } if (!token.getConsumer().getKey().equals(requestMessage.getConsumerKey())) { throw new OAuthProblemException(TOKEN_REJECTED); } if (V_1_0_A.equals(token.getVersion())) { requestMessage.requireParameters(OAUTH_VERIFIER); if (!token.getVerifier().equals(requestMessage.getParameter(OAUTH_VERIFIER))) { throw new OAuthProblemException(TOKEN_REJECTED); } } }
/** * Check the given {code token} to see if its a valid request token * * @param requestMessage the {@link net.oauth.OAuthMessage} * @param token the {@link com.atlassian.oauth.serviceprovider.ServiceProviderToken request token} * @throws OAuthProblemException * @throws IOException */ private void checkRequestToken(OAuthMessage requestMessage, ServiceProviderToken token) throws OAuthProblemException, IOException { if (token.hasExpired(clock)) { throw new OAuthProblemException(OAuth.Problems.TOKEN_EXPIRED); } if (token.getAuthorization() == ServiceProviderToken.Authorization.NONE) { throw new OAuthProblemException(OAuth.Problems.PERMISSION_UNKNOWN); } if (token.getAuthorization() == ServiceProviderToken.Authorization.DENIED) { throw new OAuthProblemException(OAuth.Problems.PERMISSION_DENIED); } if (!token.getConsumer().getKey().equals(requestMessage.getConsumerKey())) { throw new OAuthProblemException(OAuth.Problems.TOKEN_REJECTED); } if (ServiceProviderToken.Version.V_1_0_A.equals(token.getVersion())) { requestMessage.requireParameters(OAuth.OAUTH_VERIFIER); if (token.getVerifier() != null && !token.getVerifier().equals(requestMessage.getParameter(OAuth.OAUTH_VERIFIER))) { throw new OAuthProblemException(OAuth.Problems.TOKEN_REJECTED); } } }
public ServiceProviderToken generateAccessToken(ServiceProviderToken token) { checkNotNull(token, "token"); // make sure token is authorized if (token.isRequestToken() && token.getAuthorization() != Authorization.AUTHORIZED) { throw new IllegalArgumentException("token is not an authorized request token"); } String t = randomizer.randomAlphanumericString(32); return ServiceProviderToken.newAccessToken(t) .tokenSecret(token.getTokenSecret()) .consumer(token.getConsumer()) .authorizedBy(token.getUser()) .properties(propertiesFactory.newAccessTokenProperties(token)) .session(newSession(token)) .build(); }
private static void setTokenData(OAuthAccessor accessor, ServiceProviderToken token) { Tokens.setCommonTokenData(accessor, token); if (token.isRequestToken()) { if (token.getAuthorization() == Authorization.AUTHORIZED) { accessor.setProperty(Tokens.AccessorProperty.USER, token.getUser()); accessor.setProperty(Tokens.AccessorProperty.AUTHORIZED, true); } else if (token.getAuthorization() == Authorization.DENIED) { accessor.setProperty(Tokens.AccessorProperty.USER, token.getUser()); accessor.setProperty(Tokens.AccessorProperty.AUTHORIZED, false); } } else { accessor.accessToken = token.getToken(); accessor.setProperty(Tokens.AccessorProperty.USER, token.getUser()); accessor.setProperty(Tokens.AccessorProperty.AUTHORIZED, true); } accessor.tokenSecret = token.getTokenSecret(); accessor.setProperty(Tokens.AccessorProperty.CREATION_TIME, token.getCreationTime()); } }
if (token == null || token.isAccessToken()) { throw new OAuthProblemException(OAuth.Problems.TOKEN_REJECTED); } else if (token.getAuthorization() == ServiceProviderToken.Authorization.AUTHORIZED || token.getAuthorization() == ServiceProviderToken.Authorization.DENIED) { throw new OAuthProblemException(OAuth.Problems.TOKEN_USED);