Search in sources :

Example 1 with NoKeyConfiguredForEntityException

use of uk.gov.ida.saml.metadata.exceptions.NoKeyConfiguredForEntityException in project verify-hub by alphagov.

the class SamlEngineExceptionMapperTest method shouldReturnBadRequestForNoKeyConfiguredForEntityException.

@Test
public void shouldReturnBadRequestForNoKeyConfiguredForEntityException() {
    Response response = samlEngineExceptionMapper.toResponse(new NoKeyConfiguredForEntityException("error"));
    assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode());
    final ErrorStatusDto errorStatusDto = (ErrorStatusDto) response.getEntity();
    assertThat(errorStatusDto.getExceptionType()).isEqualTo(ExceptionType.NO_KEY_CONFIGURED_FOR_ENTITY);
    checkLogLevel(Level.ERROR);
}
Also used : Response(javax.ws.rs.core.Response) ErrorStatusDto(uk.gov.ida.common.ErrorStatusDto) NoKeyConfiguredForEntityException(uk.gov.ida.saml.metadata.exceptions.NoKeyConfiguredForEntityException) Test(org.junit.Test)

Example 2 with NoKeyConfiguredForEntityException

use of uk.gov.ida.saml.metadata.exceptions.NoKeyConfiguredForEntityException in project verify-hub by alphagov.

the class SamlEngineExceptionMapper method toResponse.

@Override
public Response toResponse(Exception exception) {
    final UUID errorId = UUID.randomUUID();
    Response.ResponseBuilder response = Response.status(Response.Status.BAD_REQUEST);
    if (exception instanceof ApplicationException) {
        ApplicationException applicationException = (ApplicationException) exception;
        response.entity(logAndGetErrorStatusDto(applicationException.getExceptionType().getLevel(), applicationException.getExceptionType(), applicationException, applicationException.getErrorId(), applicationException.isAudited()));
    } else if (exception instanceof SamlContextException) {
        SamlContextException contextException = (SamlContextException) exception;
        response.entity(logAndGetErrorStatusDto(contextException.getLogLevel(), contextException.getExceptionType(), exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof SamlFailedToDecryptException) {
        response.entity(logAndGetErrorStatusDto(((SamlFailedToDecryptException) exception).getLogLevel(), ExceptionType.INVALID_SAML_FAILED_TO_DECRYPT, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof SamlDuplicateRequestIdException) {
        response.entity(logAndGetErrorStatusDto(((SamlDuplicateRequestIdException) exception).getLogLevel(), ExceptionType.INVALID_SAML_DUPLICATE_REQUEST_ID, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof SamlRequestTooOldException) {
        response.entity(logAndGetErrorStatusDto(((SamlTransformationErrorException) exception).getLogLevel(), ExceptionType.INVALID_SAML_REQUEST_TOO_OLD, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof SamlTransformationErrorException) {
        response.entity(logAndGetErrorStatusDto(((SamlTransformationErrorException) exception).getLogLevel(), ExceptionType.INVALID_SAML, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof UnableToGenerateSamlException) {
        response.entity(logAndGetErrorStatusDto(((UnableToGenerateSamlException) exception).getLogLevel(), ExceptionType.INVALID_INPUT, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else if (exception instanceof NoKeyConfiguredForEntityException) {
        response.entity(logAndGetErrorStatusDto(ERROR, ExceptionType.NO_KEY_CONFIGURED_FOR_ENTITY, exception, errorId, HAS_NOT_BEEN_AUDITED_YET));
    } else {
        levelLogger.log(WARN, exception, errorId);
    }
    return response.build();
}
Also used : Response(javax.ws.rs.core.Response) ApplicationException(uk.gov.ida.exceptions.ApplicationException) SamlTransformationErrorException(uk.gov.ida.saml.core.validation.SamlTransformationErrorException) NoKeyConfiguredForEntityException(uk.gov.ida.saml.metadata.exceptions.NoKeyConfiguredForEntityException) UUID(java.util.UUID) SamlDuplicateRequestIdException(uk.gov.ida.saml.hub.exception.SamlDuplicateRequestIdException) SamlRequestTooOldException(uk.gov.ida.saml.hub.exception.SamlRequestTooOldException) SamlFailedToDecryptException(uk.gov.ida.saml.security.exception.SamlFailedToDecryptException)

Example 3 with NoKeyConfiguredForEntityException

use of uk.gov.ida.saml.metadata.exceptions.NoKeyConfiguredForEntityException in project verify-hub by alphagov.

the class NoKeyConfiguredForEntityExceptionMapperTest method assertThatLogIsCreatedAtErrorLevelAndAuditIsSentToEventSink.

@Test
public void assertThatLogIsCreatedAtErrorLevelAndAuditIsSentToEventSink() throws Exception {
    when(context.get()).thenReturn(httpServletRequest);
    when(levelLoggerFactory.createLevelLogger(NoKeyConfiguredForEntityExceptionMapper.class)).thenReturn(levelLogger);
    NoKeyConfiguredForEntityExceptionMapper mapper = new NoKeyConfiguredForEntityExceptionMapper(context, levelLoggerFactory, eventSinkMessageSender);
    NoKeyConfiguredForEntityException exception = new NoKeyConfiguredForEntityException("entityId");
    mapper.toResponse(exception);
    verify(levelLogger).log(Level.ERROR, exception);
    verify(eventSinkMessageSender).audit(any(NoKeyConfiguredForEntityException.class), any(UUID.class), any(SessionId.class));
}
Also used : NoKeyConfiguredForEntityException(uk.gov.ida.saml.metadata.exceptions.NoKeyConfiguredForEntityException) UUID(java.util.UUID) SessionId(uk.gov.ida.common.SessionId) Test(org.junit.Test)

Aggregations

NoKeyConfiguredForEntityException (uk.gov.ida.saml.metadata.exceptions.NoKeyConfiguredForEntityException)3 UUID (java.util.UUID)2 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 ErrorStatusDto (uk.gov.ida.common.ErrorStatusDto)1 SessionId (uk.gov.ida.common.SessionId)1 ApplicationException (uk.gov.ida.exceptions.ApplicationException)1 SamlTransformationErrorException (uk.gov.ida.saml.core.validation.SamlTransformationErrorException)1 SamlDuplicateRequestIdException (uk.gov.ida.saml.hub.exception.SamlDuplicateRequestIdException)1 SamlRequestTooOldException (uk.gov.ida.saml.hub.exception.SamlRequestTooOldException)1 SamlFailedToDecryptException (uk.gov.ida.saml.security.exception.SamlFailedToDecryptException)1