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

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

Introduction

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

Prototype

public static OAuth2Exception create(String errorCode, String errorMessage) 

Source Link

Document

Creates the appropriate subclass of OAuth2Exception given the errorCode.

Usage

From source file:nl.surfnet.coin.api.service.JanusClientDetailsService.java

@Override
@Cacheable(value = { "janus-meta-data" })
public ClientDetails loadClientByClientId(String consumerKey) throws OAuth2Exception {
    EntityMetadata metadata = getJanusMetadataByConsumerKey(consumerKey,
            OAuth2Exception.create(OAuth2Exception.INVALID_CLIENT, null));
    validateMetadata(consumerKey, metadata);
    final OpenConextClientDetails clientDetails = new OpenConextClientDetails();
    ClientMetaData clientMetaData = new JanusClientMetadata(metadata);
    clientDetails.setClientMetaData(clientMetaData);
    clientDetails.setClientSecret(metadata.getOauthConsumerSecret());
    clientDetails.setClientId(metadata.getOauthConsumerKey());
    clientDetails.setRegisteredRedirectUri(getCallbackUrlCollection(metadata));
    clientDetails.setScope(Arrays.asList("read"));

    clientDetails.setAuthorizedGrantTypes(Arrays.asList("implicit", "authorization_code"));
    if (metadata.isTwoLeggedOauthAllowed()) {
        clientDetails.getAuthorizedGrantTypes().add("client_credentials");
    }/* w  ww .  j a  va  2s .c om*/
    ArrayList<GrantedAuthority> authorities = new ArrayList<>(clientDetails.getAuthorities());
    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    clientDetails.setAuthorities(authorities);
    ClientMetaDataHolder.setClientMetaData(clientMetaData);
    return clientDetails;
}

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   ww  w.jav  a  2  s .c  om
        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.cloudfoundry.identity.uaa.login.RemoteUaaController.java

@RequestMapping(value = "/oauth/authorize", params = "response_type")
public ModelAndView startAuthorization(HttpServletRequest request, @RequestParam Map<String, String> parameters,
        Map<String, Object> model, @RequestHeader HttpHeaders headers, Principal principal) throws Exception {

    String path = extractPath(request);

    MultiValueMap<String, String> map = new LinkedMaskingMultiValueMap<String, String>();
    map.setAll(parameters);/* ww  w.j  a v a2 s . co  m*/

    String redirectUri = parameters.get("redirect-uri");
    if (redirectUri != null && !redirectUri.matches("(http:|https:)?//.*")) {
        redirectUri = "http://" + redirectUri;
        map.set("redirect-uri", redirectUri);
    }

    if (principal != null) {
        map.set("source", "login");
        map.setAll(getLoginCredentials(principal));
        map.remove("credentials"); // legacy cf might break otherwise
        map.remove("password"); // request for token will not use password
    } else {
        throw new BadCredentialsException("No principal found in authorize endpoint");
    }

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.putAll(getRequestHeaders(headers));
    requestHeaders.remove(AUTHORIZATION.toLowerCase());
    requestHeaders.remove(USER_AGENT);
    requestHeaders.remove(ACCEPT.toLowerCase());
    requestHeaders.remove(CONTENT_TYPE.toLowerCase());
    requestHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    requestHeaders.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    requestHeaders.remove(COOKIE);
    requestHeaders.remove(COOKIE.toLowerCase());

    @SuppressWarnings("rawtypes")
    ResponseEntity<Map> response;

    response = authorizationTemplate.exchange(getUaaBaseUrl() + "/" + path, HttpMethod.POST,
            new HttpEntity<MultiValueMap<String, String>>(map, requestHeaders), Map.class);

    saveCookie(response.getHeaders(), model);

    @SuppressWarnings("unchecked")
    Map<String, Object> body = response.getBody();
    if (body != null) {
        // User approval is required
        logger.debug("Response: " + body);
        model.putAll(body);
        model.put("links", getLinksInfo());
        if (!body.containsKey("options")) {
            String errorMsg = "No options returned from UAA for user approval";
            if (body.containsKey("error")) {
                throw OAuth2Exception.create((String) body.get("error"),
                        (String) (body.containsKey("error_description") ? body.get("error_description")
                                : errorMsg));
            } else {
                throw new OAuth2Exception(errorMsg);
            }
        }
        logger.info("Approval required in /oauth/authorize for: " + principal.getName());
        return new ModelAndView("access_confirmation", model);
    }

    String location = response.getHeaders().getFirst("Location");
    if (location != null) {
        logger.info("Redirect in /oauth/authorize for: " + principal.getName());
        // Don't expose model attributes (cookie) in redirect
        return new ModelAndView(new RedirectView(location, false, true, false));
    }

    throw new IllegalStateException("Neither a redirect nor a user approval");

}