org.apache.oltu.oauth2.client.response.OAuthClientResponse Java Examples

The following examples show how to use org.apache.oltu.oauth2.client.response.OAuthClientResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OAuthClientValidator.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
public void validateRequiredParameters(OAuthClientResponse response) throws OAuthProblemException {
    Set<String> missingParameters = new HashSet<String>();

    for (Map.Entry<String, String[]> requiredParam : requiredParams.entrySet()) {
        String paramName = requiredParam.getKey();
        String val = response.getParam(paramName);
        if (OAuthUtils.isEmpty(val)) {
            missingParameters.add(paramName);
        } else {
            String[] dependentParams = requiredParam.getValue();
            if (!OAuthUtils.hasEmptyValues(dependentParams)) {
                for (String dependentParam : dependentParams) {
                    val = response.getParam(dependentParam);
                    if (OAuthUtils.isEmpty(val)) {
                        missingParameters.add(dependentParam);
                    }
                }
            }
        }
    }

    if (!missingParameters.isEmpty()) {
        throw OAuthUtils.handleMissingParameters(missingParameters);
    }
}
 
Example #2
Source File: OAuthOkHttpClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
                                                 String requestMethod, Class<T> responseClass)
        throws OAuthSystemException, OAuthProblemException {

    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());

    if(headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    requestBuilder.method(requestMethod, body);

    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(
                response.body().string(),
                response.body().contentType().toString(),
                response.code(),
                responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
 
Example #3
Source File: OAuthOkHttpClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
        String requestMethod, Class<T> responseClass)
                throws OAuthSystemException, OAuthProblemException {

    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());

    if(headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    requestBuilder.method(requestMethod, body);

    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(
                response.body().string(), 
                response.body().contentType().toString(),
                response.code(),
                responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
 
Example #4
Source File: OAuthOkHttpClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
                                                 String requestMethod, Class<T> responseClass)
        throws OAuthSystemException, OAuthProblemException {

    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());

    if(headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    requestBuilder.method(requestMethod, body);

    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(
                response.body().string(),
                response.body().contentType().toString(),
                response.code(),
                responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
 
Example #5
Source File: OAuthOkHttpClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
        String requestMethod, Class<T> responseClass)
                throws OAuthSystemException, OAuthProblemException {

    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());

    if(headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    requestBuilder.method(requestMethod, body);

    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(
                response.body().string(), 
                response.body().contentType().toString(),
                response.code(),
                responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
 
Example #6
Source File: OAuthOkHttpClient.java    From android with MIT License 5 votes vote down vote up
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
        String requestMethod, Class<T> responseClass)
                throws OAuthSystemException, OAuthProblemException {

    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());

    if(headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    requestBuilder.method(requestMethod, body);

    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(
                response.body().string(), 
                response.body().contentType().toString(),
                response.code(),
                response.headers().toMultimap(),
                responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
 
Example #7
Source File: OAuthOkHttpClient.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
        String requestMethod, Class<T> responseClass)
                throws OAuthSystemException, OAuthProblemException {

    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());

    if(headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    requestBuilder.method(requestMethod, body);

    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(
                response.body().string(), 
                response.body().contentType().toString(),
                response.code(),
                responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
 
Example #8
Source File: WindowsLiveOAuth2Authenticator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @return userInfoEndpoint
 */
@Override
protected String getUserInfoEndpoint(OAuthClientResponse token, Map<String, String> authenticatorProperties) {

    if (StringUtils.isBlank(this.userInfoEndpoint)) {
        initUserInfoEndPoint();
    }

    return this.userInfoEndpoint;
}
 
Example #9
Source File: OAuthClientValidator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public void validateErrorResponse(OAuthClientResponse response) throws OAuthProblemException {
    String error = response.getParam(OAuthError.OAUTH_ERROR);
    if (!OAuthUtils.isEmpty(error)) {
        String errorDesc = response.getParam(OAuthError.OAUTH_ERROR_DESCRIPTION);
        String errorUri = response.getParam(OAuthError.OAUTH_ERROR_URI);
        String state = response.getParam(OAuth.OAUTH_STATE);
        throw OAuthProblemException.error(error).description(errorDesc).uri(errorUri).state(state);
    }
}
 
Example #10
Source File: OAuthClientValidator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public void validateNotAllowedParameters(OAuthClientResponse response) throws OAuthProblemException {
    List<String> notAllowedParameters = new ArrayList<String>();
    for (String requiredParam : notAllowedParams) {
        String val = response.getParam(requiredParam);
        if (!OAuthUtils.isEmpty(val)) {
            notAllowedParameters.add(requiredParam);
        }
    }
    if (!notAllowedParameters.isEmpty()) {
        throw OAuthUtils.handleNotAllowedParametersOAuthException(notAllowedParameters);
    }
}
 
Example #11
Source File: GoogleOAuth2Authenticator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
protected String getAuthenticateUser(AuthenticationContext context, Map<String, Object> jsonObject, OAuthClientResponse token) {
    if (jsonObject.get(OIDCAuthenticatorConstants.Claim.EMAIL) == null) {
        return (String) jsonObject.get("sub");
    } else {
        return (String) jsonObject.get(OIDCAuthenticatorConstants.Claim.EMAIL);
    }
}
 
Example #12
Source File: OAuthOkHttpClient.java    From eve-esi with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends OAuthClientResponse> T execute(OAuthClientRequest request, Map<String, String> headers,
        String requestMethod, Class<T> responseClass) throws OAuthSystemException, OAuthProblemException {

    MediaType mediaType = MediaType.parse("application/json");
    Request.Builder requestBuilder = new Request.Builder().url(request.getLocationUri());

    if (headers != null) {
        for (Entry<String, String> entry : headers.entrySet()) {
            if (entry.getKey().equalsIgnoreCase("Content-Type")) {
                mediaType = MediaType.parse(entry.getValue());
            } else {
                requestBuilder.addHeader(entry.getKey(), entry.getValue());
            }
        }
    }

    RequestBody body = request.getBody() != null ? RequestBody.create(mediaType, request.getBody()) : null;
    requestBuilder.method(requestMethod, body);

    try {
        Response response = client.newCall(requestBuilder.build()).execute();
        return OAuthClientResponseFactory.createCustomResponse(response.body().string(), response.body()
                .contentType().toString(), response.code(), responseClass);
    } catch (IOException e) {
        throw new OAuthSystemException(e);
    }
}
 
Example #13
Source File: YahooOAuth2Authenticator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Get the user info endpoint.
 *
 * @param token OAuth client response.
 * @return User info endpoint.
 */
@Override
protected String getUserInfoEndpoint(OAuthClientResponse token, Map<String, String> authenticatorProperties) {

    String userGUID = token.getParam(YahooOAuth2AuthenticatorConstants.USER_GUID);
    return getUserInfoURL() + userGUID + YahooOAuth2AuthenticatorConstants.YAHOO_USER_DETAILS_JSON;
}
 
Example #14
Source File: WindowsLiveOAuth2Authenticator.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * @param token
 * @return
 */
@Override
protected String getAuthenticateUser(AuthenticationContext context, Map<String, Object> jsonObject, OAuthClientResponse token) {
    return token.getParam(WindowsLiveOAuth2AuthenticatorConstants.USER_ID);
}
 
Example #15
Source File: YahooOAuth2Authenticator.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Get subject attributes.
 *
 * @param token                   OAuthClientResponse
 * @param authenticatorProperties Map<String, String>
 * @return Map<ClaimMapping, String> Claim mappings.
 */
protected Map<ClaimMapping, String> getSubjectAttributes(OAuthClientResponse token,
                                                         Map<String, String> authenticatorProperties) {

    Map<ClaimMapping, String> claims = new HashMap<>();

    try {
        String accessToken = token.getParam(OIDCAuthenticatorConstants.ACCESS_TOKEN);
        String url = getUserInfoEndpoint(token, authenticatorProperties);
        String json = sendRequest(url, accessToken);

        if (StringUtils.isBlank(json)) {
            if (log.isDebugEnabled()) {
                log.debug("Unable to fetch user claims. Proceeding without user claims");
            }
            return claims;
        }

        Map<String, Object> jsonObject = JSONUtils.parseJSON(json);
        Map<String, Object> profile = null;

        if (!jsonObject.isEmpty()) {

            // Extract the inner profile JSON object.
            profile = JSONUtils.parseJSON(jsonObject.entrySet().iterator().next().getValue().toString());
        }

        if (profile == null) {
            if (log.isDebugEnabled()) {
                log.debug("Invalid user profile object. Proceeding without user claims");
            }
            return claims;
        }

        for (Map.Entry<String, Object> data : profile.entrySet()) {
            String key = data.getKey();
            claims.put(ClaimMapping.build(key, key, null, false), profile.get(key).toString());

            if (log.isDebugEnabled()
                    && IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.USER_CLAIMS)) {
                log.debug("Adding claims from end-point data mapping : " + key + " - " +
                        profile.get(key).toString());
            }
        }
    } catch (IOException e) {
        log.error("Communication error occurred while accessing user info endpoint", e);
    }
    return claims;
}
 
Example #16
Source File: OpenIDConnectAuthenticator.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Get subject attributes.
 * @param token OAuthClientResponse
 * @param authenticatorProperties Map<String, String> (Authenticator property, Property value)
 * @return Map<ClaimMapping, String> Claim mappings.
 */
protected Map<ClaimMapping, String> getSubjectAttributes(OAuthClientResponse token,
                                                         Map<String, String> authenticatorProperties) {

    Map<ClaimMapping, String> claims = new HashMap<>();

    try {
        String accessToken = token.getParam(OIDCAuthenticatorConstants.ACCESS_TOKEN);
        String url = getUserInfoEndpoint(token, authenticatorProperties);
        String json = sendRequest(url, accessToken);

        if (StringUtils.isBlank(json)) {
            if(log.isDebugEnabled()) {
                log.debug("Empty JSON response from user info endpoint. Unable to fetch user claims." +
                        " Proceeding without user claims");
            }
            return claims;
        }

        Map<String, Object> jsonObject = JSONUtils.parseJSON(json);

        for (Map.Entry<String, Object> data : jsonObject.entrySet()) {
            String key = data.getKey();
            Object value = data.getValue();

            if (value != null) {
                claims.put(ClaimMapping.build(key, key, null, false), value.toString());
            }

            if (log.isDebugEnabled() &&
                    IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.USER_CLAIMS)) {
                log.debug("Adding claims from end-point data mapping : " + key + " - " +
                        jsonObject.get(key).toString());
            }
        }
    } catch (IOException e) {
        log.error("Communication error occurred while accessing user info endpoint", e);
    }

    return claims;
}
 
Example #17
Source File: OAuthClient.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public  <T extends OAuthClientResponse> T resource(OAuthClientRequest request, String requestMethod,Class<T> responseClass) throws OAuthSystemException, OAuthProblemException{
    return httpClient.execute(request, null, requestMethod, responseClass);
}
 
Example #18
Source File: HttpClient.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public <T extends OAuthClientResponse> T execute(
OAuthClientRequest request,
Map<String, String> headers,
String requestMethod,
Class<T> responseClass)
throws OAuthSystemException, OAuthProblemException;
 
Example #19
Source File: OAuthClientValidator.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public void validateParameters(OAuthClientResponse response) throws OAuthProblemException {
    validateRequiredParameters(response);
    validateNotAllowedParameters(response);
}
 
Example #20
Source File: OAuthClientValidator.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public void validate(OAuthClientResponse response) throws OAuthProblemException {
    validateErrorResponse(response);
    validateParameters(response);
}
 
Example #21
Source File: OpenIDConnectAuthenticator.java    From carbon-identity with Apache License 2.0 2 votes vote down vote up
/**
 * Get user info endpoint.
 * @param token OAuthClientResponse
 * @param authenticatorProperties Map<String, String> (Authenticator property, Property value)
 * @return User info endpoint.
 */
protected String getUserInfoEndpoint(OAuthClientResponse token, Map<String, String> authenticatorProperties) {
    return authenticatorProperties.get(IdentityApplicationConstants.Authenticator.OIDC.USER_INFO_URL);
}
 
Example #22
Source File: YahooOAuth2Authenticator.java    From carbon-identity with Apache License 2.0 2 votes vote down vote up
/**
 * Get Authenticated User
 *
 * @param token OAuth client response.
 * @return GUID of the authenticated user.
 */
@Override
protected String getAuthenticateUser(AuthenticationContext context, Map<String, Object> jsonObject, OAuthClientResponse token) {

    return token.getParam(YahooOAuth2AuthenticatorConstants.USER_GUID);
}
 
Example #23
Source File: OpenIDConnectAuthenticator.java    From carbon-identity with Apache License 2.0 2 votes vote down vote up
/**
 *
 * @param context
 * @param jsonObject
 * @param token
 * @return
 */

protected String getAuthenticateUser(AuthenticationContext context, Map<String, Object> jsonObject,OAuthClientResponse token) {
    return (String) jsonObject.get("sub");
}
 
Example #24
Source File: GoogleOAuth2Authenticator.java    From carbon-identity with Apache License 2.0 2 votes vote down vote up
/**
 * Get google user info endpoint.
 * @param token OAuth client response.
 * @return User info endpoint.
 */
@Override
protected String getUserInfoEndpoint(OAuthClientResponse token, Map<String, String> authenticatorProperties) {
    return getUserInfoURL();
}