Example usage for org.springframework.security.oauth2.common.exceptions OAuth2Exception addAdditionalInformation

List of usage examples for org.springframework.security.oauth2.common.exceptions OAuth2Exception addAdditionalInformation

Introduction

In this page you can find the example usage for org.springframework.security.oauth2.common.exceptions OAuth2Exception addAdditionalInformation.

Prototype

public void addAdditionalInformation(String key, String value) 

Source Link

Document

Add some additional information with this OAuth error.

Usage

From source file:com.epam.reportportal.auth.OAuthErrorHandler.java

@Override
public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {

    if (e instanceof OAuth2Exception) {
        ResponseEntity<OAuth2Exception> translate = super.translate(e);
        OAuth2Exception body = translate.getBody();
        body.addAdditionalInformation("message", body.getMessage());
        body.addAdditionalInformation("error_code", String.valueOf(ErrorType.ACCESS_DENIED.getCode()));
        return translate;
    } else {//from w w  w  .  j  a v a2  s.  c  o  m
        RestError restError = errorResolver.resolveError(e);
        OAuth2Exception exception = OAuth2Exception.create(
                String.valueOf(restError.getErrorRS().getErrorType().getCode()),
                restError.getErrorRS().getMessage());
        exception.addAdditionalInformation("message", restError.getErrorRS().getMessage());
        return new ResponseEntity<>(exception, restError.getHttpStatus());
    }

}

From source file:org.joyrest.oauth2.endpoint.AuthorizationEndpoint.java

private String generateCode(AuthorizationRequest authorizationRequest) throws AuthenticationException {
    try {/* w  w  w. j a  v  a  2  s.  co m*/
        OAuth2Request storedOAuth2Request = requestFactory.createOAuth2Request(authorizationRequest);
        OAuth2Authentication combinedAuth = new OAuth2Authentication(storedOAuth2Request, null);
        return authorizationCodeServices.createAuthorizationCode(combinedAuth);
    } catch (OAuth2Exception e) {
        if (authorizationRequest.getState() != null) {
            e.addAdditionalInformation("state", authorizationRequest.getState());
        }
        throw e;
    }
}

From source file:org.cloudfoundry.identity.uaa.oauth.UaaAuthorizationEndpoint.java

private String generateCode(AuthorizationRequest authorizationRequest, Authentication authentication)
        throws AuthenticationException {

    try {//from  w  w w  .  j a v  a  2  s.  co m

        OAuth2Request storedOAuth2Request = getOAuth2RequestFactory().createOAuth2Request(authorizationRequest);

        OAuth2Authentication combinedAuth = new OAuth2Authentication(storedOAuth2Request, authentication);
        String code = authorizationCodeServices.createAuthorizationCode(combinedAuth);

        return code;

    } catch (OAuth2Exception e) {

        if (authorizationRequest.getState() != null) {
            e.addAdditionalInformation("state", authorizationRequest.getState());
        }

        throw e;

    }
}

From source file:org.springframework.security.oauth2.common.exceptions.OAuth2ExceptionJackson2Deserializer.java

@Override
public OAuth2Exception deserialize(JsonParser jp, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

    JsonToken t = jp.getCurrentToken();/*from   w w w  .  ja v  a  2  s .  co  m*/
    if (t == JsonToken.START_OBJECT) {
        t = jp.nextToken();
    }
    Map<String, Object> errorParams = new HashMap<String, Object>();
    for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) {
        // Must point to field name
        String fieldName = jp.getCurrentName();
        // And then the value...
        t = jp.nextToken();
        // Note: must handle null explicitly here; value deserializers won't
        Object value;
        if (t == JsonToken.VALUE_NULL) {
            value = null;
        }
        // Some servers might send back complex content
        else if (t == JsonToken.START_ARRAY) {
            value = jp.readValueAs(List.class);
        } else if (t == JsonToken.START_OBJECT) {
            value = jp.readValueAs(Map.class);
        } else {
            value = jp.getText();
        }
        errorParams.put(fieldName, value);
    }

    Object errorCode = errorParams.get("error");
    String errorMessage = errorParams.containsKey("error_description")
            ? errorParams.get("error_description").toString()
            : null;
    if (errorMessage == null) {
        errorMessage = errorCode == null ? "OAuth Error" : errorCode.toString();
    }

    OAuth2Exception ex;
    if ("invalid_client".equals(errorCode)) {
        ex = new InvalidClientException(errorMessage);
    } else if ("unauthorized_client".equals(errorCode)) {
        ex = new UnauthorizedUserException(errorMessage);
    } else if ("invalid_grant".equals(errorCode)) {
        if (errorMessage.toLowerCase().contains("redirect") && errorMessage.toLowerCase().contains("match")) {
            ex = new RedirectMismatchException(errorMessage);
        } else {
            ex = new InvalidGrantException(errorMessage);
        }
    } else if ("invalid_scope".equals(errorCode)) {
        ex = new InvalidScopeException(errorMessage);
    } else if ("invalid_token".equals(errorCode)) {
        ex = new InvalidTokenException(errorMessage);
    } else if ("invalid_request".equals(errorCode)) {
        ex = new InvalidRequestException(errorMessage);
    } else if ("redirect_uri_mismatch".equals(errorCode)) {
        ex = new RedirectMismatchException(errorMessage);
    } else if ("unsupported_grant_type".equals(errorCode)) {
        ex = new UnsupportedGrantTypeException(errorMessage);
    } else if ("unsupported_response_type".equals(errorCode)) {
        ex = new UnsupportedResponseTypeException(errorMessage);
    } else if ("insufficient_scope".equals(errorCode)) {
        ex = new InsufficientScopeException(errorMessage,
                OAuth2Utils.parseParameterList((String) errorParams.get("scope")));
    } else if ("access_denied".equals(errorCode)) {
        ex = new UserDeniedAuthorizationException(errorMessage);
    } else {
        ex = new OAuth2Exception(errorMessage);
    }

    Set<Map.Entry<String, Object>> entries = errorParams.entrySet();
    for (Map.Entry<String, Object> entry : entries) {
        String key = entry.getKey();
        if (!"error".equals(key) && !"error_description".equals(key)) {
            Object value = entry.getValue();
            ex.addAdditionalInformation(key, value == null ? null : value.toString());
        }
    }

    return ex;

}