Example usage for org.springframework.security.oauth2.common.exceptions UnapprovedClientAuthenticationException UnapprovedClientAuthenticationException

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

Introduction

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

Prototype

public UnapprovedClientAuthenticationException(String msg, Throwable t) 

Source Link

Usage

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

private String getUnsuccessfulRedirect(AuthorizationRequest authorizationRequest, OAuth2Exception failure,
        boolean fragment) {
    if (isNull(authorizationRequest) || isNull(authorizationRequest.getRedirectUri())) {
        // we have no redirect for the user. very sad.
        throw new UnapprovedClientAuthenticationException("Authorization failure, and no redirect URI.",
                failure);/*from w ww.ja v a2 s  . c o m*/
    }

    Map<String, String> query = new LinkedHashMap<>();

    query.put("error", failure.getOAuth2ErrorCode());
    query.put("error_description", failure.getMessage());

    if (nonNull(authorizationRequest.getState())) {
        query.put("state", authorizationRequest.getState());
    }

    if (nonNull(failure.getAdditionalInformation())) {
        for (Map.Entry<String, String> additionalInfo : failure.getAdditionalInformation().entrySet()) {
            query.put(additionalInfo.getKey(), additionalInfo.getValue());
        }
    }

    return append(authorizationRequest.getRedirectUri(), query, fragment);
}

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

private String getUnsuccessfulRedirect(AuthorizationRequest authorizationRequest, OAuth2Exception failure,
        boolean fragment) {

    if (authorizationRequest == null || authorizationRequest.getRedirectUri() == null) {
        // we have no redirect for the user. very sad.
        throw new UnapprovedClientAuthenticationException("Authorization failure, and no redirect URI.",
                failure);/*from w  ww  . jav  a2 s.  co m*/
    }

    UriComponentsBuilder template = UriComponentsBuilder.fromUriString(authorizationRequest.getRedirectUri());
    StringBuilder values = new StringBuilder();

    values.append("error=" + encode(failure.getOAuth2ErrorCode()));
    values.append("&error_description=" + encode(failure.getMessage()));

    if (authorizationRequest.getState() != null) {
        values.append("&state=" + encode(authorizationRequest.getState()));
    }

    if (failure.getAdditionalInformation() != null) {
        for (Map.Entry<String, String> additionalInfo : failure.getAdditionalInformation().entrySet()) {
            values.append("&" + encode(additionalInfo.getKey()) + "=" + encode(additionalInfo.getValue()));
        }
    }

    if (fragment) {
        template.fragment(values.toString());
    } else {
        template.query(values.toString());
    }

    return template.build(true).toUriString();

}

From source file:org.cloudfoundry.identity.uaa.authentication.BackwardsCompatibleTokenEndpointAuthenticationFilter.java

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    final HttpServletRequest request = (HttpServletRequest) req;
    final HttpServletResponse response = (HttpServletResponse) res;

    try {/*from   w w  w . j  a v  a 2s  . co  m*/
        Authentication userAuthentication = extractCredentials(request, response);

        if (userAuthentication != null) {
            Authentication clientAuth = SecurityContextHolder.getContext().getAuthentication();
            if (clientAuth == null) {
                throw new BadCredentialsException(
                        "No client authentication found. Remember to put a filter upstream of the TokenEndpointAuthenticationFilter.");
            }

            Map<String, String> map = getSingleValueMap(request);
            map.put(OAuth2Utils.CLIENT_ID, clientAuth.getName());

            SecurityContextHolder.getContext().setAuthentication(userAuthentication);
            AuthorizationRequest authorizationRequest = oAuth2RequestFactory.createAuthorizationRequest(map);

            //authorizationRequest.setScope(getScope(request));
            if (clientAuth.isAuthenticated()) {
                // Ensure the OAuth2Authentication is authenticated
                authorizationRequest.setApproved(true);
            }

            OAuth2Request storedOAuth2Request = oAuth2RequestFactory.createOAuth2Request(authorizationRequest);

            SecurityContextHolder.getContext()
                    .setAuthentication(new OAuth2Authentication(storedOAuth2Request, userAuthentication));

            onSuccessfulAuthentication(request, response, userAuthentication);
        }
    } catch (UnauthorizedClientException failed) {
        //happens when all went well, but the client is not authorized for the identity provider
        UnapprovedClientAuthenticationException ex = new UnapprovedClientAuthenticationException(
                failed.getMessage(), failed);
        SecurityContextHolder.clearContext();
        logger.debug("Authentication request for failed: " + failed);
        onUnsuccessfulAuthentication(request, response, ex);
        authenticationEntryPoint.commence(request, response, ex);
        return;
    } catch (AuthenticationException failed) {
        SecurityContextHolder.clearContext();
        logger.debug("Authentication request for failed: " + failed);
        onUnsuccessfulAuthentication(request, response, failed);
        authenticationEntryPoint.commence(request, response, failed);
        return;
    } catch (InvalidScopeException ex) {
        String message = ex.getMessage();
        response.sendError(UNAUTHORIZED.value(), message);
        return;
    }

    chain.doFilter(request, response);
}

From source file:org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.java

private String getUnsuccessfulRedirect(AuthorizationRequest authorizationRequest, OAuth2Exception failure,
        boolean fragment) {

    if (authorizationRequest == null || authorizationRequest.getRedirectUri() == null) {
        // we have no redirect for the user. very sad.
        throw new UnapprovedClientAuthenticationException("Authorization failure, and no redirect URI.",
                failure);/*from   ww  w  .  j  av a2  s .  co m*/
    }

    Map<String, String> query = new LinkedHashMap<String, String>();

    query.put("error", failure.getOAuth2ErrorCode());
    query.put("error_description", failure.getMessage());

    if (authorizationRequest.getState() != null) {
        query.put("state", authorizationRequest.getState());
    }

    if (failure.getAdditionalInformation() != null) {
        for (Map.Entry<String, String> additionalInfo : failure.getAdditionalInformation().entrySet()) {
            query.put(additionalInfo.getKey(), additionalInfo.getValue());
        }
    }

    return append(authorizationRequest.getRedirectUri(), query, fragment);

}