Search in sources :

Example 1 with OAuthResourceResponse

use of org.apache.oltu.oauth2.client.response.OAuthResourceResponse in project airavata by apache.

the class AuthResponse method authenticate.

public static AuthResponse authenticate(String username, String password) throws AuthenticationException {
    try {
        OAuthClientRequest request = OAuthClientRequest.tokenLocation(hostName + "/oauth2/token").setClientId(clientId).setClientSecret(clientSecret).setGrantType(GrantType.PASSWORD).setRedirectURI("").setUsername(username).setPassword(password).setScope("openid").buildBodyMessage();
        URLConnectionClient ucc = new URLConnectionClient();
        org.apache.oltu.oauth2.client.OAuthClient oAuthClient = new org.apache.oltu.oauth2.client.OAuthClient(ucc);
        OAuthResourceResponse resp = oAuthClient.resource(request, OAuth.HttpMethod.POST, OAuthResourceResponse.class);
        // converting JSON to object
        ObjectMapper mapper = new ObjectMapper();
        AuthResponse authResponse;
        try {
            authResponse = mapper.readValue(resp.getBody(), AuthResponse.class);
        } catch (Exception e) {
            return null;
        }
        String accessToken = authResponse.getAccess_token();
        if (accessToken != null && !accessToken.isEmpty()) {
            request = new OAuthBearerClientRequest(hostName + "/oauth2/userinfo?schema=openid").buildQueryMessage();
            ucc = new URLConnectionClient();
            request.setHeader("Authorization", "Bearer " + accessToken);
            oAuthClient = new org.apache.oltu.oauth2.client.OAuthClient(ucc);
            resp = oAuthClient.resource(request, OAuth.HttpMethod.GET, OAuthResourceResponse.class);
            Map<String, String> profile = mapper.readValue(resp.getBody(), Map.class);
            return authResponse;
        }
    } catch (Exception ex) {
        throw new AuthenticationException(ex.getMessage());
    }
    return null;
}
Also used : OAuthResourceResponse(org.apache.oltu.oauth2.client.response.OAuthResourceResponse) DefaultOAuthClient(org.apache.airavata.service.security.oauth.DefaultOAuthClient) AuthenticationException(org.apache.airavata.model.error.AuthenticationException) AuthenticationException(org.apache.airavata.model.error.AuthenticationException) AiravataSecurityException(org.apache.airavata.security.AiravataSecurityException) OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 2 with OAuthResourceResponse

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

the class LinkedInAuthClient method getCredential.

@Override
public String getCredential(final HttpServletRequest request) {
    OAuthResourceResponse userResponse = getUserResponse(request);
    if (userResponse == null) {
        return null;
    }
    String body = userResponse.getBody();
    logger.debug("User response body: {}", body);
    String[] addresses = StringUtils.stripAll(StringUtils.stripAll(StringUtils.stripEnd(StringUtils.stripStart(body, "["), "]").split(",")), "\"");
    return addresses.length > 0 ? addresses[0] : null;
}
Also used : OAuthResourceResponse(org.apache.oltu.oauth2.client.response.OAuthResourceResponse)

Example 3 with OAuthResourceResponse

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

the class StructrOAuthClient method getUserResponse.

protected OAuthResourceResponse getUserResponse(final HttpServletRequest request) {
    if (userResponse != null) {
        return userResponse;
    }
    try {
        String accessToken = getAccessToken(request);
        if (accessToken != null) {
            final String accessTokenParameterKey = this.getAccessTokenParameterKey();
            OAuthClientRequest clientReq = new OAuthBearerClientRequest(getUserResourceUri()) {

                @Override
                public OAuthBearerClientRequest setAccessToken(String accessToken) {
                    this.parameters.put(accessTokenParameterKey, accessToken);
                    return this;
                }
            }.setAccessToken(accessToken).buildQueryMessage();
            // needed for LinkedIn
            clientReq.setHeader("x-li-format", "json");
            logger.info("User info request: {}", clientReq.getLocationUri());
            OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
            userResponse = oAuthClient.resource(clientReq, "GET", OAuthResourceResponse.class);
            logger.info("User info response: {}", userResponse);
            return userResponse;
        }
    } catch (Throwable t) {
        logger.error("Could not get user response", t);
    }
    return null;
}
Also used : OAuthBearerClientRequest(org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthResourceResponse(org.apache.oltu.oauth2.client.response.OAuthResourceResponse) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest)

Example 4 with OAuthResourceResponse

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

the class StructrOAuthClient method getValue.

public String getValue(final HttpServletRequest request, final String key) {
    try {
        OAuthResourceResponse userResponse = getUserResponse(request);
        if (userResponse == null) {
            return null;
        }
        String body = userResponse.getBody();
        logger.info("User response body: {}", body);
        return (String) JSONUtils.parseJSON(body).get(key);
    } catch (Exception ex) {
        logger.warn("Could not extract {} from JSON response", ex);
    }
    return null;
}
Also used : OAuthResourceResponse(org.apache.oltu.oauth2.client.response.OAuthResourceResponse) OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException)

Example 5 with OAuthResourceResponse

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

the class GitHubAuthClient method getCredential.

@Override
public String getCredential(final HttpServletRequest request) {
    final OAuthResourceResponse userResponse = getUserResponse(request);
    if (userResponse == null) {
        return null;
    }
    final String body = userResponse.getBody();
    logger.debug("User response body: {}", body);
    final JsonParser parser = new JsonParser();
    final JsonElement result = parser.parse(body);
    if (result instanceof JsonArray) {
        final JsonArray arr = (JsonArray) result;
        final Iterator<JsonElement> iterator = arr.iterator();
        if (iterator.hasNext()) {
            final JsonElement el = iterator.next();
            final String address = el.getAsJsonObject().get("email").getAsString();
            logger.info("Got 'email' credential from GitHub: {}", address);
            return address;
        }
    }
    return null;
}
Also used : JsonArray(com.google.gson.JsonArray) OAuthResourceResponse(org.apache.oltu.oauth2.client.response.OAuthResourceResponse) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Aggregations

OAuthResourceResponse (org.apache.oltu.oauth2.client.response.OAuthResourceResponse)5 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)2 OAuthBearerClientRequest (org.apache.oltu.oauth2.client.request.OAuthBearerClientRequest)2 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)2 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 AuthenticationException (org.apache.airavata.model.error.AuthenticationException)1 AiravataSecurityException (org.apache.airavata.security.AiravataSecurityException)1 DefaultOAuthClient (org.apache.airavata.service.security.oauth.DefaultOAuthClient)1 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)1 OAuthProblemException (org.apache.oltu.oauth2.common.exception.OAuthProblemException)1 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1