Search in sources :

Example 1 with OAuthGetAccessToken

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());
    }
}
Also used : OAuthGetAccessToken(com.google.api.client.auth.oauth.OAuthGetAccessToken) GenericUrl(com.google.api.client.http.GenericUrl) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OAuthCredentialsResponse(com.google.api.client.auth.oauth.OAuthCredentialsResponse)

Aggregations

OAuthCredentialsResponse (com.google.api.client.auth.oauth.OAuthCredentialsResponse)1 OAuthGetAccessToken (com.google.api.client.auth.oauth.OAuthGetAccessToken)1 GenericUrl (com.google.api.client.http.GenericUrl)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1