Search in sources :

Example 6 with SessionId

use of uk.gov.ida.common.SessionId in project verify-hub by alphagov.

the class SamlMessageReceiverApi method handleRequestPost.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Timed
public Response handleRequestPost(SamlRequestDto samlRequestDto) {
    relayStateValidator.validate(samlRequestDto.getRelayState());
    AuthnRequest authnRequest = stringSamlAuthnRequestTransformer.apply(samlRequestDto.getSamlRequest());
    SamlValidationResponse signatureValidationResponse = authnRequestSignatureValidator.validate(authnRequest, SPSSODescriptor.DEFAULT_ELEMENT_NAME);
    protectiveMonitoringLogger.logAuthnRequest(authnRequest, Direction.INBOUND, signatureValidationResponse.isOK());
    if (!signatureValidationResponse.isOK()) {
        SamlValidationSpecificationFailure failure = signatureValidationResponse.getSamlValidationSpecificationFailure();
        throw new SamlTransformationErrorException(failure.getErrorMessage(), signatureValidationResponse.getCause(), Level.ERROR);
    }
    SamlAuthnRequestContainerDto samlAuthnRequestContainerDto = new SamlAuthnRequestContainerDto(samlRequestDto.getSamlRequest(), Optional.ofNullable(samlRequestDto.getRelayState()), samlRequestDto.getPrincipalIpAsSeenByFrontend());
    SessionId sessionId = sessionProxy.createSession(samlAuthnRequestContainerDto);
    return Response.ok(sessionId).build();
}
Also used : SamlValidationResponse(uk.gov.ida.saml.core.validation.SamlValidationResponse) SamlValidationSpecificationFailure(uk.gov.ida.saml.core.validation.SamlValidationSpecificationFailure) SamlAuthnRequestContainerDto(uk.gov.ida.hub.samlproxy.domain.SamlAuthnRequestContainerDto) AuthnRequest(org.opensaml.saml.saml2.core.AuthnRequest) SamlTransformationErrorException(uk.gov.ida.saml.core.validation.SamlTransformationErrorException) SessionId(uk.gov.ida.common.SessionId) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 7 with SessionId

use of uk.gov.ida.common.SessionId in project verify-hub by alphagov.

the class SamlMessageSenderHandlerTest method generateErrorResponseFromHub_shouldThrowSamlTransformationException.

@Test(expected = SamlTransformationErrorException.class)
public void generateErrorResponseFromHub_shouldThrowSamlTransformationException() throws MarshallingException, SignatureException {
    SessionId sessionId = SessionId.createNewSessionId();
    String expectedSamlMessageId = UUID.randomUUID().toString();
    Response openSamlResponse = setUpErrorResponseFromHub(sessionId, expectedSamlMessageId);
    when(samlMessageSignatureValidator.validate(openSamlResponse, SPSSODescriptor.DEFAULT_ELEMENT_NAME)).thenReturn(SamlValidationResponse.anInvalidResponse(new SamlValidationSpecification("bad", true)));
    samlMessageSenderHandler.generateErrorResponseFromHub(sessionId, principalIpAddressAsSeenByHub);
}
Also used : Response(org.opensaml.saml.saml2.core.Response) ResponseBuilder.aResponse(uk.gov.ida.saml.core.test.builders.ResponseBuilder.aResponse) SamlValidationResponse(uk.gov.ida.saml.core.validation.SamlValidationResponse) Matchers.anyString(org.mockito.Matchers.anyString) SessionId(uk.gov.ida.common.SessionId) SamlValidationSpecification(uk.gov.ida.saml.core.validation.errors.SamlValidationSpecification) Test(org.junit.Test)

Example 8 with SessionId

use of uk.gov.ida.common.SessionId in project verify-hub by alphagov.

the class SamlMessageSenderHandlerTest method generateAuthRequestFromHub_shouldThrowSamlTransformationException.

@Test(expected = SamlTransformationErrorException.class)
public void generateAuthRequestFromHub_shouldThrowSamlTransformationException() throws MarshallingException, SignatureException {
    SessionId sessionId = SessionId.createNewSessionId();
    String expectedSamlMessageId = UUID.randomUUID().toString();
    when(sessionProxy.getAuthnRequestFromHub(sessionId)).thenReturn(new AuthnRequestFromHubContainerDto(samlRequest, postEndPoint, true));
    AuthnRequest authnRequest = anAuthnRequest().withId(expectedSamlMessageId).build();
    when(authnRequestTransformer.apply(samlRequest)).thenReturn(authnRequest);
    when(samlMessageSignatureValidator.validate(authnRequest, SPSSODescriptor.DEFAULT_ELEMENT_NAME)).thenReturn(SamlValidationResponse.anInvalidResponse(new SamlValidationSpecification("bad", true)));
    samlMessageSenderHandler.generateAuthnRequestFromHub(sessionId, principalIpAddressAsSeenByHub);
}
Also used : AuthnRequest(org.opensaml.saml.saml2.core.AuthnRequest) AuthnRequestBuilder.anAuthnRequest(uk.gov.ida.saml.core.test.builders.AuthnRequestBuilder.anAuthnRequest) Matchers.anyString(org.mockito.Matchers.anyString) AuthnRequestFromHubContainerDto(uk.gov.ida.hub.samlproxy.domain.AuthnRequestFromHubContainerDto) SessionId(uk.gov.ida.common.SessionId) SamlValidationSpecification(uk.gov.ida.saml.core.validation.errors.SamlValidationSpecification) Test(org.junit.Test)

Example 9 with SessionId

use of uk.gov.ida.common.SessionId in project verify-hub by alphagov.

the class SamlMessageSenderHandlerTest method generateAuthnResponseFromHub_shouldAddExternalCommunicationEvent.

@Test
public void generateAuthnResponseFromHub_shouldAddExternalCommunicationEvent() throws Exception {
    SessionId sessionId = SessionId.createNewSessionId();
    String expectedSamlMessageId = UUID.randomUUID().toString();
    Response openSamlResponse = setUpAuthnResponseFromHub(sessionId, expectedSamlMessageId);
    SamlMessage authnResponse = samlMessageSenderHandler.generateAuthnResponseFromHub(sessionId, principalIpAddressAsSeenByHub);
    assertThat(authnResponse.getSamlMessage()).isEqualTo(samlRequest);
    assertThat(authnResponse.getPostEndpoint()).isEqualTo(postEndPoint.toString());
    assertThat(authnResponse.getRegistration().isPresent()).isFalse();
    assertThat(authnResponse.getRelayState().isPresent()).isTrue();
    assertThat(authnResponse.getRelayState().get()).isEqualTo(relayState.get());
    assertThat(authnResponse.getSamlMessageType()).isEqualTo(SamlMessageType.SAML_RESPONSE);
    verify(externalCommunicationEventLogger).logResponseFromHub(expectedSamlMessageId, sessionId, postEndPoint, principalIpAddressAsSeenByHub);
    verify(protectiveMonitoringLogger).logAuthnResponse(openSamlResponse, Direction.OUTBOUND, true);
}
Also used : Response(org.opensaml.saml.saml2.core.Response) ResponseBuilder.aResponse(uk.gov.ida.saml.core.test.builders.ResponseBuilder.aResponse) SamlValidationResponse(uk.gov.ida.saml.core.validation.SamlValidationResponse) SamlMessage(uk.gov.ida.hub.samlproxy.controllogic.SamlMessageSenderHandler.SamlMessage) Matchers.anyString(org.mockito.Matchers.anyString) SessionId(uk.gov.ida.common.SessionId) Test(org.junit.Test)

Example 10 with SessionId

use of uk.gov.ida.common.SessionId in project verify-hub by alphagov.

the class SamlMessageSenderHandlerTest method generateAuthResponseFromHub_shouldThrowSamlTransformationException.

@Test(expected = SamlTransformationErrorException.class)
public void generateAuthResponseFromHub_shouldThrowSamlTransformationException() throws MarshallingException, SignatureException {
    SessionId sessionId = SessionId.createNewSessionId();
    String expectedSamlMessageId = UUID.randomUUID().toString();
    Response openSamlResponse = setUpAuthnResponseFromHub(sessionId, expectedSamlMessageId);
    when(samlMessageSignatureValidator.validate(openSamlResponse, SPSSODescriptor.DEFAULT_ELEMENT_NAME)).thenReturn(SamlValidationResponse.anInvalidResponse(new SamlValidationSpecification("bad", true)));
    samlMessageSenderHandler.generateAuthnResponseFromHub(sessionId, principalIpAddressAsSeenByHub);
}
Also used : Response(org.opensaml.saml.saml2.core.Response) ResponseBuilder.aResponse(uk.gov.ida.saml.core.test.builders.ResponseBuilder.aResponse) SamlValidationResponse(uk.gov.ida.saml.core.validation.SamlValidationResponse) Matchers.anyString(org.mockito.Matchers.anyString) SessionId(uk.gov.ida.common.SessionId) SamlValidationSpecification(uk.gov.ida.saml.core.validation.errors.SamlValidationSpecification) Test(org.junit.Test)

Aggregations

SessionId (uk.gov.ida.common.SessionId)39 Test (org.junit.Test)32 URI (java.net.URI)17 ResponseBuilder.aResponse (uk.gov.ida.saml.core.test.builders.ResponseBuilder.aResponse)15 SamlValidationResponse (uk.gov.ida.saml.core.validation.SamlValidationResponse)15 Response (javax.ws.rs.core.Response)12 Response (org.opensaml.saml.saml2.core.Response)12 Element (org.w3c.dom.Element)10 Matchers.anyString (org.mockito.Matchers.anyString)7 AuthnResponseFromHubContainerDto (uk.gov.ida.hub.samlproxy.contracts.AuthnResponseFromHubContainerDto)6 SamlRequestDto (uk.gov.ida.hub.samlproxy.contracts.SamlRequestDto)5 OutboundResponseFromHub (uk.gov.ida.saml.core.domain.OutboundResponseFromHub)5 ResponseActionDto (uk.gov.ida.hub.samlproxy.domain.ResponseActionDto)4 HubTransformersFactory (uk.gov.ida.saml.hub.api.HubTransformersFactory)4 Timed (com.codahale.metrics.annotation.Timed)3 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 Produces (javax.ws.rs.Produces)3 AuthnRequest (org.opensaml.saml.saml2.core.AuthnRequest)3 SamlMessage (uk.gov.ida.hub.samlproxy.controllogic.SamlMessageSenderHandler.SamlMessage)3