All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cloudfoundry.identity.uaa.authentication.UaaExceptionTranslator Maven / Gradle / Ivy

The newest version!
package org.cloudfoundry.identity.uaa.authentication;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.DefaultWebResponseExceptionTranslator;

import java.io.IOException;

public class UaaExceptionTranslator extends DefaultWebResponseExceptionTranslator {

    @Override
    public ResponseEntity translate(Exception e) throws Exception {
        if (e instanceof AccountNotVerifiedException) {
            return handleOAuth2Exception(new ForbiddenException(e.getMessage(), e));
        }

        return super.translate(e);
    }

    private ResponseEntity handleOAuth2Exception(OAuth2Exception e) throws IOException {

        int status = e.getHttpErrorCode();
        HttpHeaders headers = new HttpHeaders();
        headers.set("Cache-Control", "no-store");
        headers.set("Pragma", "no-cache");

        ResponseEntity response = new ResponseEntity(e, headers,
            HttpStatus.valueOf(status));

        return response;

    }

    private static class ForbiddenException extends OAuth2Exception {

        public ForbiddenException(String msg, Throwable t) {
            super(msg, t);
        }

        public String getOAuth2ErrorCode() {
            return "access_denied";
        }

        public int getHttpErrorCode() {
            return 403;
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy