Search in sources :

Example 6 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project components by Talend.

the class Oauth2ImplicitClient method getToken.

public <T extends OAuthAccessTokenResponse> T getToken(Class<T> tokenResponseClass) {
    try {
        TokenRequestBuilder builder = // 
        OAuthClientRequest.tokenLocation(// 
        tokenLocation.toString()).setGrantType(// 
        grantType).setClientId(// 
        clientID).setClientSecret(clientSecret);
        if (GrantType.AUTHORIZATION_CODE == grantType) {
            builder = // 
            builder.setRedirectURI(callbackURL.toString()).setCode(getAuthorizationCode());
        } else if (GrantType.REFRESH_TOKEN == grantType) {
            builder = builder.setRefreshToken(refreshToken);
        }
        OAuthClientRequest request = builder.buildQueryMessage();
        OAuthClient oauthClient = new OAuthClient(new URLConnectionClient());
        return oauthClient.accessToken(request, tokenResponseClass);
    } catch (OAuthSystemException e) {
        throw new RuntimeException(e);
    } catch (OAuthProblemException e) {
        throw new RuntimeException(e);
    }
}
Also used : TokenRequestBuilder(org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 7 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project intermine by intermine.

the class Callback method getAccessToken.

private String getAccessToken(String redirect, OAuthAuthzResponse oar, OAuthProvider provider) throws OAuthSystemException, OAuthProblemException {
    OAuthAccessTokenResponse oauthResponse = getTokenResponse(redirect, oar, provider);
    String accessToken = oauthResponse.getAccessToken();
    return accessToken;
}
Also used : OAuthAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse)

Example 8 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project intermine by intermine.

the class Callback method googleProviderFlow.

/**
 * Google's OpenID2.0 -> OpenIDConnect (ie. Open Auth 2.0) migration makes this
 * special branch necessary.
 */
private ActionMessages googleProviderFlow(HttpServletRequest request, String providerName, String redirectUri, OAuthProvider provider, OAuthAuthzResponse oar) throws ForseenProblem, OAuthSystemException, OAuthProblemException, JSONException {
    // Special flow just for Google, because Google is special (not in a good way).
    OAuthAccessTokenResponse resp = getTokenResponse(redirectUri, oar, provider);
    LOG.debug("GOOGLE RESPONSE: " + resp.getBody());
    MigrationMapping migrationMapping = null;
    Base64 decoder = new Base64();
    String accessToken = resp.getAccessToken();
    JSONObject respData;
    try {
        respData = new JSONObject(resp.getBody());
    } catch (JSONException e) {
        throw new ForseenProblem("oauth2.error.bad-json");
    }
    String jwt = respData.optString("id_token");
    if (jwt != null) {
        String[] pieces = jwt.split("\\.");
        if (pieces.length == 3) {
            JSONObject claims = new JSONObject(new String(decoder.decode(pieces[1])));
            String openidID = claims.optString("openid_id");
            String sub = claims.optString("sub");
            migrationMapping = new MigrationMapping(openidID, sub);
        } else {
            LOG.error("id_token is not a valid JWT - has Google changed their API?");
        }
    } else {
        LOG.debug("No id_token (and thus migration info) provided by Google");
    }
    DelegatedIdentity identity = getDelegatedIdentity(providerName, accessToken);
    return loginUser(request, identity, migrationMapping);
}
Also used : Base64(org.apache.commons.codec.binary.Base64) JSONObject(org.json.JSONObject) OAuthAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse) JSONException(org.json.JSONException)

Example 9 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project intermine by intermine.

the class CallbackService method getAccessToken.

private String getAccessToken(String redirect, OAuthAuthzResponse oar, OAuthProvider provider) throws OAuthSystemException, OAuthProblemException {
    OAuthClient oauthClient = new OAuthClient(new URLConnectionClient());
    OAuthClientRequest clientReq;
    OAuthClientRequest.TokenRequestBuilder requestBuilder = OAuthClientRequest.tokenLocation(provider.getTokenUrl()).setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(provider.getClientId()).setClientSecret(provider.getClientSecret()).setRedirectURI(redirect).setCode(oar.getCode());
    switch(provider.getMessageFormat()) {
        case BODY:
            clientReq = requestBuilder.buildBodyMessage();
            break;
        case QUERY:
            clientReq = requestBuilder.buildQueryMessage();
            break;
        default:
            throw new RuntimeException("Unknown message format");
    }
    LOG.info("Requesting access token: URI = " + clientReq.getLocationUri() + " BODY = " + clientReq.getBody());
    try {
        OAuthAccessTokenResponse tokenResponse = null;
        switch(provider.getResponseType()) {
            case FORM:
                tokenResponse = oauthClient.accessToken(clientReq, GitHubTokenResponse.class);
                break;
            case JSON:
                tokenResponse = oauthClient.accessToken(clientReq);
                break;
            default:
                throw new RuntimeException("Unknown response type");
        }
        return tokenResponse.getAccessToken();
    } catch (OAuthProblemException ex) {
        throw new BadRequestException(ex.getMessage());
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse) GitHubTokenResponse(org.apache.oltu.oauth2.client.response.GitHubTokenResponse) BadRequestException(org.intermine.webservice.server.exceptions.BadRequestException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 10 with OAuthAccessTokenResponse

use of org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse in project structr by structr.

the class StructrOAuthClient method getAccessTokenResponse.

private OAuthAccessTokenResponse getAccessTokenResponse(final HttpServletRequest request) {
    if (tokenResponse != null) {
        return tokenResponse;
    }
    try {
        String code = getCode(request);
        if (code == null) {
            logger.error("Could not get code from request, cancelling authorization process");
            return null;
        }
        OAuthClientRequest clientReq = OAuthClientRequest.tokenLocation(tokenLocation).setGrantType(getGrantType()).setClientId(clientId).setClientSecret(clientSecret).setRedirectURI(getAbsoluteUrl(request, redirectUri)).setScope(getScope()).setCode(getCode(request)).buildBodyMessage();
        if (isVerboseLoggingEnabled()) {
            logger.info("Request body: {}", clientReq.getBody());
        }
        final OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
        tokenResponse = oAuthClient.accessToken(clientReq, tokenResponseClass);
        if (isVerboseLoggingEnabled()) {
            logger.info("Access token response: {}", tokenResponse.getBody());
        }
        return tokenResponse;
    } catch (Throwable t) {
        logger.error("Could not get access token response", t);
    }
    return null;
}
Also used : URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Aggregations

OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)8 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)6 OAuthAccessTokenResponse (org.apache.oltu.oauth2.client.response.OAuthAccessTokenResponse)6 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)5 TokenRequestBuilder (org.apache.oltu.oauth2.client.request.OAuthClientRequest.TokenRequestBuilder)3 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)3 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)2 ProgressDialog (android.app.ProgressDialog)1 DialogInterface (android.content.DialogInterface)1 AsyncTask (android.os.AsyncTask)1 Base64 (org.apache.commons.codec.binary.Base64)1 GitHubTokenResponse (org.apache.oltu.oauth2.client.response.GitHubTokenResponse)1 BadRequestException (org.intermine.webservice.server.exceptions.BadRequestException)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1