use of com.google.api.client.auth.oauth.OAuthGetAccessToken in project che by eclipse.
the class OAuthAuthenticator method callback.
/**
* Process callback request.
*
* @param requestUrl
* request URI. URI should contain OAuth token and OAuth verifier.
* @return id of authenticated user
* @throws OAuthAuthenticationException
* if authentication failed or {@code requestUrl} does not contain required parameters.
*/
String callback(final URL requestUrl) throws OAuthAuthenticationException {
try {
final GenericUrl callbackUrl = new GenericUrl(requestUrl.toString());
if (callbackUrl.getFirst(OAUTH_TOKEN_PARAM_KEY) == null) {
throw new OAuthAuthenticationException("Missing oauth_token parameter");
}
if (callbackUrl.getFirst(OAUTH_VERIFIER_PARAM_KEY) == null) {
throw new OAuthAuthenticationException("Missing oauth_verifier parameter");
}
final String state = (String) callbackUrl.getFirst(STATE_PARAM_KEY);
String requestMethod = getParameterFromState(state, REQUEST_METHOD_PARAM_KEY);
String signatureMethod = getParameterFromState(state, SIGNATURE_METHOD_PARAM_KEY);
final String oauthTemporaryToken = (String) callbackUrl.getFirst(OAUTH_TOKEN_PARAM_KEY);
OAuthGetAccessToken getAccessToken;
if (requestMethod != null && "post".equals(requestMethod.toLowerCase())) {
getAccessToken = new OAuthPostAccessToken(accessTokenUri);
} else {
getAccessToken = new OAuthGetAccessToken(accessTokenUri);
}
getAccessToken.consumerKey = clientId;
getAccessToken.temporaryToken = oauthTemporaryToken;
getAccessToken.verifier = (String) callbackUrl.getFirst(OAUTH_VERIFIER_PARAM_KEY);
getAccessToken.transport = httpTransport;
if (signatureMethod != null && "rsa".equals(signatureMethod.toLowerCase())) {
getAccessToken.signer = getOAuthRsaSigner();
} else {
getAccessToken.signer = getOAuthHmacSigner(clientSecret, sharedTokenSecrets.remove(oauthTemporaryToken));
}
final OAuthCredentialsResponse credentials = getAccessToken.execute();
String userId = getParameterFromState(state, USER_ID_PARAM_KEY);
credentialsStoreLock.lock();
try {
final OAuthCredentialsResponse userId2Credential = credentialsStore.get(userId);
if (userId2Credential == null) {
credentialsStore.put(userId, credentials);
} else {
userId2Credential.token = credentials.token;
userId2Credential.tokenSecret = credentials.tokenSecret;
}
} finally {
credentialsStoreLock.unlock();
}
return userId;
} catch (Exception e) {
throw new OAuthAuthenticationException(e.getMessage());
}
}
Aggregations