org.apache.oltu.oauth2.common.exception.OAuthProblemException Java Examples

The following examples show how to use org.apache.oltu.oauth2.common.exception.OAuthProblemException. 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: RetryingOAuth.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException {
    if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) {
        try {
            OAuthJSONAccessTokenResponse accessTokenResponse =
                    oAuthClient.accessToken(tokenRequestBuilder.buildBodyMessage());
            if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) {
                setAccessToken(accessTokenResponse.getAccessToken());
                return !getAccessToken().equals(requestAccessToken);
            }
        } catch (OAuthSystemException | OAuthProblemException e) {
            throw new IOException(e);
        }
    }

    return false;
}
 
Example #2
Source File: CarbonOAuthAuthzRequest.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
protected OAuthValidator<HttpServletRequest> initValidator() throws OAuthProblemException, OAuthSystemException {

        String responseTypeValue = getParam(OAuth.OAUTH_RESPONSE_TYPE);
        if (OAuthUtils.isEmpty(responseTypeValue)) {
            throw OAuthUtils.handleOAuthProblemException("Missing response_type parameter value");
        }

        Class<? extends OAuthValidator<HttpServletRequest>> clazz = OAuthServerConfiguration
                .getInstance().getSupportedResponseTypeValidators().get(responseTypeValue);

        if (clazz == null) {
            if (log.isDebugEnabled()) {
                //Do not change this log format as these logs use by external applications
                log.debug("Unsupported Response Type : " + responseTypeValue +
                        " for client id : " + getClientId());
            }
            throw OAuthUtils.handleOAuthProblemException("Invalid response_type parameter value");
        }

        return OAuthUtils.instantiateClass(clazz);
    }
 
Example #3
Source File: RetryingOAuth.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException {
    if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) {
        try {
            OAuthJSONAccessTokenResponse accessTokenResponse =
                    oAuthClient.accessToken(tokenRequestBuilder.buildBodyMessage());
            if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) {
                setAccessToken(accessTokenResponse.getAccessToken());
                return !getAccessToken().equals(requestAccessToken);
            }
        } catch (OAuthSystemException | OAuthProblemException e) {
            throw new IOException(e);
        }
    }

    return false;
}
 
Example #4
Source File: CarbonOAuthTokenRequest.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Initialize a grant type validator
 *
 * @return an instance of OAuthValidator
 * @throws OAuthProblemException
 * @throws OAuthSystemException
 */
@Override
protected OAuthValidator<HttpServletRequest> initValidator() throws OAuthProblemException, OAuthSystemException {

    String requestTypeValue = getParam(OAuth.OAUTH_GRANT_TYPE);
    if (OAuthUtils.isEmpty(requestTypeValue)) {
        throw OAuthUtils.handleOAuthProblemException("Missing grant_type parameter value");
    }

    Class<? extends OAuthValidator<HttpServletRequest>> clazz = OAuthServerConfiguration
            .getInstance().getSupportedGrantTypeValidators().get(requestTypeValue);

    if (clazz == null) {
        if (log.isDebugEnabled()) {
            //Do not change this log format as these logs use by external applications
            log.debug("Unsupported Grant Type : " + requestTypeValue +
                    " for client id : " + getClientId());
        }
        throw OAuthUtils.handleOAuthProblemException("Invalid grant_type parameter value");
    }

    return OAuthUtils.instantiateClass(clazz);
}
 
Example #5
Source File: CarbonOAuthTokenRequest.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs CarbonOAuthTokenRequest from the given HttpServletRequest
 *
 * @param request an instance of HttpServletRequest that represents an OAuth token request
 * @throws OAuthSystemException
 * @throws OAuthProblemException
 */
public CarbonOAuthTokenRequest(HttpServletRequest request) throws OAuthSystemException,
        OAuthProblemException {

    super(request);
    assertion = request.getParameter(OAuth.OAUTH_ASSERTION);
    windows_token = request.getParameter(OAuthConstants.WINDOWS_TOKEN);
    tenantDomain = request.getParameter(MultitenantConstants.TENANT_DOMAIN);
    if (tenantDomain == null) {
        tenantDomain = MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
    }

    // Store all request parameters
    if (request.getParameterNames() != null) {
        List<RequestParameter> requestParameterList = new ArrayList<RequestParameter>();
        while (request.getParameterNames().hasMoreElements()) {
            String key = request.getParameterNames().nextElement();
            String value = request.getParameter(key);
            requestParameterList.add(new RequestParameter(key, value));
        }
        requestParameters =
                requestParameterList.toArray(new RequestParameter[requestParameterList.size()]);
    }
}
 
Example #6
Source File: AbstractValidator.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void validateOptionalParameters(T request) throws OAuthProblemException {
    final Set<String> missingParameters = new HashSet<String>();

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

    if (!missingParameters.isEmpty()) {
        throw OAuthUtils.handleMissingParameters(missingParameters);
    }
}
 
Example #7
Source File: AbstractValidator.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void validateClientAuthenticationCredentials(T request) throws OAuthProblemException {
    if (enforceClientAuthentication) {
        Set<String> missingParameters = new HashSet<String>();
        String clientAuthHeader = request.getHeader(OAuth.HeaderType.AUTHORIZATION);
        String[] clientCreds = OAuthUtils.decodeClientAuthenticationHeader(clientAuthHeader);

        // Only fallback to params if the auth header is not correct. Don't allow a mix of auth header vs params
        if (clientCreds == null || OAuthUtils.isEmpty(clientCreds[0]) || OAuthUtils.isEmpty(clientCreds[1])) {

            if (OAuthUtils.isEmpty(request.getParameter(OAuth.OAUTH_CLIENT_ID))) {
                missingParameters.add(OAuth.OAUTH_CLIENT_ID);
            }
            if (OAuthUtils.isEmpty(request.getParameter(OAuth.OAUTH_CLIENT_SECRET))) {
                missingParameters.add(OAuth.OAUTH_CLIENT_SECRET);
            }
        }

        if (!missingParameters.isEmpty()) {
            throw OAuthUtils.handleMissingParameters(missingParameters);
        }
    }
}
 
Example #8
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 #9
Source File: RetryingOAuth.java    From eve-esi with Apache License 2.0 6 votes vote down vote up
public synchronized boolean updateAccessToken(String requestAccessToken) throws IOException {
    if (getAccessToken() == null || getAccessToken().equals(requestAccessToken)) {
        try {
            OAuthJSONAccessTokenResponse accessTokenResponse = oAuthClient.accessToken(tokenRequestBuilder
                    .buildBodyMessage());
            if (accessTokenResponse != null && accessTokenResponse.getAccessToken() != null) {
                setAccessToken(accessTokenResponse.getAccessToken());
                return !getAccessToken().equals(requestAccessToken);
            }
        } catch (OAuthSystemException | OAuthProblemException e) {
            throw new IOException(e);
        }
    }

    return false;
}
 
Example #10
Source File: AbstractValidator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void validateContentType(T request) throws OAuthProblemException {
    String contentType = request.getContentType();
    final String expectedContentType = OAuth.ContentType.URL_ENCODED;
    if (!OAuthUtils.hasContentType(contentType, expectedContentType)) {
        throw OAuthUtils.handleBadContentTypeException(expectedContentType);
    }
}
 
Example #11
Source File: OAuthClientResponseFactory.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public static OAuthClientResponse createGitHubTokenResponse(String body, String contentType,
                                                            int responseCode)
    throws OAuthProblemException {
    GitHubTokenResponse resp = new GitHubTokenResponse();
    resp.init(body, contentType, responseCode);
    return resp;
}
 
Example #12
Source File: OAuthUtils.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public static OAuthProblemException handleNotAllowedParametersOAuthException(
    List<String> notAllowedParams) {
    StringBuffer sb = new StringBuffer("Not allowed parameters: ");
    if (notAllowedParams != null) {
        for (String notAllowed : notAllowedParams) {
            sb.append(notAllowed).append(" ");
        }
    }
    return handleOAuthProblemException(sb.toString().trim());
}
 
Example #13
Source File: OAuthUtils.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates OAuthProblemException that contains set of missing oauth parameters
 *
 * @param missingParams missing oauth parameters
 * @return OAuthProblemException with user friendly message about missing oauth parameters
 */

public static OAuthProblemException handleMissingParameters(Set<String> missingParams) {
    StringBuffer sb = new StringBuffer("Missing parameters: ");
    if (!OAuthUtils.isEmpty(missingParams)) {
        for (String missingParam : missingParams) {
            sb.append(missingParam).append(" ");
        }
    }
    return handleOAuthProblemException(sb.toString().trim());
}
 
Example #14
Source File: OAuthResponse.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public OAuthErrorResponseBuilder error(OAuthProblemException ex) {
    this.parameters.put(OAuthError.OAUTH_ERROR, ex.getError());
    this.parameters.put(OAuthError.OAUTH_ERROR_DESCRIPTION, ex.getDescription());
    this.parameters.put(OAuthError.OAUTH_ERROR_URI, ex.getUri());
    this.parameters.put(OAuth.OAUTH_STATE, ex.getState());
    return this;
}
 
Example #15
Source File: OAuthClient.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public <T extends OAuthAccessTokenResponse> T accessToken(
    OAuthClientRequest request, String requestMethod, Class<T> responseClass)
    throws OAuthSystemException, OAuthProblemException {

    Map<String, String> headers = new HashMap<String, String>();
    headers.put(OAuth.HeaderType.CONTENT_TYPE, OAuth.ContentType.URL_ENCODED);

    return httpClient.execute(request, headers, requestMethod, responseClass);
}
 
Example #16
Source File: OAuthClient.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public <T extends OAuthAccessTokenResponse> T accessToken(
    OAuthClientRequest request,
    Class<T> responseClass)
    throws OAuthSystemException, OAuthProblemException {

    return accessToken(request, OAuth.HttpMethod.POST, responseClass);
}
 
Example #17
Source File: OAuthClientResponse.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
protected void init(String body, String contentType, int responseCode) throws OAuthProblemException {
    this.setBody(body);
    this.setContentType(contentType);
    this.setResponseCode(responseCode);
    this.validate();

}
 
Example #18
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 #19
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 #20
Source File: OidcAuthenticator.java    From entando-components with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void fetchAndProcessToken(HttpServletRequest req, String code) throws
        OAuthSystemException, OAuthProblemException, ApsSystemException {
    OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
    OAuthClientRequest oAuthClientRequest = this.oidcHelper.buildOauthRequest(req, code);
    OAuthJSONAccessTokenResponse oAuthResponse = oAuthClient.resource(oAuthClientRequest, OAuth.HttpMethod.POST, OAuthJSONAccessTokenResponse.class);

    _logger.info("----------------------TOKEN------------------- ");
    String accessToken = oAuthResponse.getAccessToken();
    _logger.info("accessToken -> " + accessToken);
    UserDetails cdpUser = this.oidcHelper.getOidcUser(oAuthResponse.getAccessToken());
    HttpSession session = req.getSession();
    session.setAttribute(SystemConstants.SESSIONPARAM_CURRENT_USER, cdpUser);
}
 
Example #21
Source File: AbstractValidator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void validateRequiredParameters(T request) throws OAuthProblemException {
    final Set<String> missingParameters = new HashSet<String>();
    for (String requiredParam : requiredParams) {
        String val = request.getParameter(requiredParam);
        if (OAuthUtils.isEmpty(val)) {
            missingParameters.add(requiredParam);
        }
    }
    if (!missingParameters.isEmpty()) {
        throw OAuthUtils.handleMissingParameters(missingParameters);
    }
}
 
Example #22
Source File: AbstractValidator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void validateNotAllowedParameters(T request) throws OAuthProblemException {
    List<String> notAllowedParameters = new ArrayList<String>();
    for (String requiredParam : notAllowedParams) {
        String val = request.getParameter(requiredParam);
        if (!OAuthUtils.isEmpty(val)) {
            notAllowedParameters.add(requiredParam);
        }
    }
    if (!notAllowedParameters.isEmpty()) {
        throw OAuthUtils.handleNotAllowedParametersOAuthException(notAllowedParameters);
    }
}
 
Example #23
Source File: AbstractValidator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void performAllValidations(T request) throws OAuthProblemException {
    this.validateContentType(request);
    this.validateMethod(request);
    this.validateRequiredParameters(request);
    this.validateOptionalParameters(request);
    this.validateNotAllowedParameters(request);
    this.validateClientAuthenticationCredentials(request);
}
 
Example #24
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 #25
Source File: FacebookAuthenticator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
private String getAuthorizationCode(HttpServletRequest request) throws ApplicationAuthenticatorException {
    OAuthAuthzResponse authzResponse;
    try {
        authzResponse = OAuthAuthzResponse.oauthCodeAuthzResponse(request);
        return authzResponse.getCode();
    } catch (OAuthProblemException e) {
        throw new ApplicationAuthenticatorException("Exception while reading authorization code.", e);
    }
}
 
Example #26
Source File: IDTokenResponseValidator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public void validateRequiredParameters(HttpServletRequest request) throws OAuthProblemException {

        super.validateRequiredParameters(request);

        String nonce = request.getParameter("nonce");
        if(StringUtils.isBlank(nonce)){
            throw OAuthProblemException.error(OAuthError.TokenResponse.INVALID_REQUEST)
                    .description("\'response_type\' contains \'id_token\'; but \'nonce\' parameter not found");
        }
    }
 
Example #27
Source File: IDTokenResponseValidator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void validateMethod(HttpServletRequest request) throws OAuthProblemException {
    String method = request.getMethod();
    if (!OAuth.HttpMethod.GET.equals(method) && !OAuth.HttpMethod.POST.equals(method)) {
        throw OAuthProblemException.error(OAuthError.CodeResponse.INVALID_REQUEST)
                .description("Method not correct.");
    }
}
 
Example #28
Source File: IDTokenTokenResponseValidator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public void validateRequiredParameters(HttpServletRequest request) throws OAuthProblemException {

        super.validateRequiredParameters(request);

        String nonce = request.getParameter("nonce");
        if(StringUtils.isBlank(nonce)){
            throw OAuthProblemException.error(OAuthError.TokenResponse.INVALID_REQUEST)
                    .description("\'response_type\' contains \'id_token\'; but \'nonce\' parameter not found");
        }
    }
 
Example #29
Source File: IDTokenTokenResponseValidator.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void validateMethod(HttpServletRequest request) throws OAuthProblemException {
    String method = request.getMethod();
    if (!OAuth.HttpMethod.GET.equals(method) && !OAuth.HttpMethod.POST.equals(method)) {
        throw OAuthProblemException.error(OAuthError.CodeResponse.INVALID_REQUEST)
                                   .description("Method not correct.");
    }
}
 
Example #30
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);
    }
}