String consKey = params.getConsumerKey();
if (consKey == null) {
throw new OAuthException(Response.Status.BAD_REQUEST, null);
}
OAuthToken rt = provider.getRequestToken(params.getToken());
if (rt == null) {
// token invalid
throw new OAuthException(Response.Status.BAD_REQUEST, null);
}
OAuthConsumer consumer = rt.getConsumer();
if (consumer == null || !consKey.equals(consumer.getKey())) {
// token invalid
throw new OAuthException(Response.Status.BAD_REQUEST, null);
}
OAuthSecrets secrets = new OAuthSecrets().consumerSecret(consumer.getSecret()).tokenSecret(rt.getSecret());
try {
sigIsOk = OAuthSignature.verify(request, params, secrets);
} catch (OAuthSignatureException ex) {
Logger.getLogger(AccessTokenRequest.class.getName()).log(Level.SEVERE, null, ex);
}
if (!sigIsOk) {
// signature invalid
throw new OAuthException(Response.Status.BAD_REQUEST, null);
}
// We're good to go.
OAuthToken at = provider.newAccessToken(rt, params.getVerifier());
if(at == null) {
throw new OAuthException(Response.Status.BAD_REQUEST, null);
}
// Preparing the response.
Form resp = new Form();
resp.putSingle(OAuthParameters.TOKEN, at.getToken());
resp.putSingle(OAuthParameters.TOKEN_SECRET, at.getSecret());
resp.putAll(at.getAttributes());
return Response.ok(resp).build();
} catch (OAuthException e) {
// map the exception to avoid having to add the mapper to the providers
return e.toResponse();
}