Search in sources :

Example 1 with OAuthCredentialsResponse

use of com.google.api.client.auth.oauth.OAuthCredentialsResponse 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)

Example 2 with OAuthCredentialsResponse

use of com.google.api.client.auth.oauth.OAuthCredentialsResponse in project che by eclipse.

the class OAuthAuthenticator method getAuthenticateUrl.

/**
     * Create authentication URL.
     *
     * @param requestUrl
     *         URL of current HTTP request. This parameter required to be able determine URL for redirection after
     *         authentication. If URL contains query parameters they will be copied to 'state' parameter and returned to
     *         callback method.
     * @param requestMethod
     *         HTTP request method that will be used to request temporary token
     * @param signatureMethod
     *         OAuth signature algorithm
     * @return URL for authentication.
     * @throws OAuthAuthenticationException
     *         if authentication failed.
     */
String getAuthenticateUrl(final URL requestUrl, @Nullable final String requestMethod, @Nullable final String signatureMethod) throws OAuthAuthenticationException {
    try {
        final GenericUrl callbackUrl = new GenericUrl(redirectUri);
        callbackUrl.put(STATE_PARAM_KEY, requestUrl.getQuery());
        OAuthGetTemporaryToken temporaryToken;
        if (requestMethod != null && "post".equals(requestMethod.toLowerCase())) {
            temporaryToken = new OAuthPostTemporaryToken(requestTokenUri);
        } else {
            temporaryToken = new OAuthGetTemporaryToken(requestTokenUri);
        }
        if (signatureMethod != null && "rsa".equals(signatureMethod.toLowerCase())) {
            temporaryToken.signer = getOAuthRsaSigner();
        } else {
            temporaryToken.signer = getOAuthHmacSigner(null, null);
        }
        temporaryToken.consumerKey = clientId;
        temporaryToken.callback = callbackUrl.build();
        temporaryToken.transport = httpTransport;
        final OAuthCredentialsResponse credentialsResponse = temporaryToken.execute();
        final OAuthAuthorizeTemporaryTokenUrl authorizeTemporaryTokenUrl = new OAuthAuthorizeTemporaryTokenUrl(authorizeTokenUri);
        authorizeTemporaryTokenUrl.temporaryToken = credentialsResponse.token;
        sharedTokenSecrets.put(credentialsResponse.token, credentialsResponse.tokenSecret);
        return authorizeTemporaryTokenUrl.build();
    } catch (Exception e) {
        throw new OAuthAuthenticationException(e.getMessage());
    }
}
Also used : OAuthGetTemporaryToken(com.google.api.client.auth.oauth.OAuthGetTemporaryToken) GenericUrl(com.google.api.client.http.GenericUrl) OAuthAuthorizeTemporaryTokenUrl(com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl) 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)

Example 3 with OAuthCredentialsResponse

use of com.google.api.client.auth.oauth.OAuthCredentialsResponse in project che by eclipse.

the class OAuthAuthenticator method getToken.

private OAuthToken getToken(final String userId) {
    OAuthCredentialsResponse credentials;
    credentialsStoreLock.lock();
    try {
        credentials = credentialsStore.get(userId);
    } finally {
        credentialsStoreLock.unlock();
    }
    if (credentials != null) {
        return newDto(OAuthToken.class).withToken(credentials.token).withScope(credentials.tokenSecret);
    }
    return null;
}
Also used : OAuthCredentialsResponse(com.google.api.client.auth.oauth.OAuthCredentialsResponse)

Example 4 with OAuthCredentialsResponse

use of com.google.api.client.auth.oauth.OAuthCredentialsResponse in project che by eclipse.

the class OAuthAuthenticator method computeAuthorizationHeader.

/**
     * Compute the Authorization header to sign the OAuth 1 request.
     *
     * @param userId
     *         the user id.
     * @param requestMethod
     *         the HTTP request method.
     * @param requestUrl
     *         the HTTP request url with encoded query parameters.
     * @return the authorization header value, or {@code null} if token was not found for given user id.
     * @throws OAuthAuthenticationException
     *         if authentication failed.
     */
String computeAuthorizationHeader(final String userId, final String requestMethod, final String requestUrl) throws OAuthAuthenticationException {
    final OAuthCredentialsResponse credentials = new OAuthCredentialsResponse();
    OAuthToken oauthToken = getToken(userId);
    credentials.token = oauthToken != null ? oauthToken.getToken() : null;
    if (credentials.token != null) {
        return computeAuthorizationHeader(requestMethod, requestUrl, credentials.token, credentials.tokenSecret);
    }
    return null;
}
Also used : OAuthToken(org.eclipse.che.api.auth.shared.dto.OAuthToken) OAuthCredentialsResponse(com.google.api.client.auth.oauth.OAuthCredentialsResponse)

Aggregations

OAuthCredentialsResponse (com.google.api.client.auth.oauth.OAuthCredentialsResponse)4 GenericUrl (com.google.api.client.http.GenericUrl)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 OAuthAuthorizeTemporaryTokenUrl (com.google.api.client.auth.oauth.OAuthAuthorizeTemporaryTokenUrl)1 OAuthGetAccessToken (com.google.api.client.auth.oauth.OAuthGetAccessToken)1 OAuthGetTemporaryToken (com.google.api.client.auth.oauth.OAuthGetTemporaryToken)1 OAuthToken (org.eclipse.che.api.auth.shared.dto.OAuthToken)1