Search in sources :

Example 6 with RelayState

use of org.opensaml.saml.saml2.ecp.RelayState in project ddf by codice.

the class LogoutRequestServiceTest method testGetLogoutRequestNotParsable.

@Test
public void testGetLogoutRequestNotParsable() throws Exception {
    String signature = "signature";
    String signatureAlgorithm = "sha1";
    String relayState = UUID.randomUUID().toString();
    String deflatedSamlRequest = RestSecurity.deflateAndBase64Encode("deflatedSamlRequest");
    when(logoutMessage.extractSamlLogoutRequest(eq("deflatedSamlRequest"))).thenReturn(null);
    Response response = logoutRequestService.getLogoutRequest(deflatedSamlRequest, null, relayState, signatureAlgorithm, signature);
    assertEquals(Response.Status.SEE_OTHER.getStatusCode(), response.getStatus());
    String msg = "Unable to parse logout request.".replaceAll(" ", "+");
    assertTrue("Expected message containing " + msg, response.getLocation().getQuery().contains(msg));
}
Also used : LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Response(javax.ws.rs.core.Response) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 7 with RelayState

use of org.opensaml.saml.saml2.ecp.RelayState in project ddf by codice.

the class LogoutRequestServiceTest method testPostLogoutRequestResponseNotParsable.

@Test
public void testPostLogoutRequestResponseNotParsable() throws Exception {
    String relayState = UUID.randomUUID().toString();
    String encodedSamlResponse = "encodedSamlRequest";
    when(logoutMessage.extractSamlLogoutResponse(any(String.class))).thenReturn(null);
    Response response = logoutRequestService.postLogoutRequest(null, encodedSamlResponse, relayState);
    assertEquals(Response.Status.SEE_OTHER.getStatusCode(), response.getStatus());
    String msg = "Unable to parse logout response.".replaceAll(" ", "+");
    assertTrue("Expected message containing " + msg, response.getLocation().getQuery().contains(msg));
}
Also used : LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Response(javax.ws.rs.core.Response) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 8 with RelayState

use of org.opensaml.saml.saml2.ecp.RelayState in project ddf by codice.

the class LogoutRequestServiceTest method testPostLogoutRequestResponse.

@Test
public void testPostLogoutRequestResponse() throws Exception {
    String relayState = UUID.randomUUID().toString();
    String encodedSamlResponse = "encodedSamlRequest";
    String issuerStr = "issuer";
    Issuer issuer = mock(Issuer.class);
    LogoutResponse logoutResponse = mock(LogoutResponse.class);
    logoutResponse.setIssuer(issuer);
    when(logoutMessage.extractSamlLogoutResponse(any(String.class))).thenReturn(logoutResponse);
    when(request.getRequestURL()).thenReturn(new StringBuffer("www.url.com/url"));
    when(logoutResponse.getIssuer()).thenReturn(issuer);
    when(logoutResponse.getIssueInstant()).thenReturn(new DateTime());
    when(logoutResponse.getVersion()).thenReturn(SAMLVersion.VERSION_20);
    when(logoutResponse.getID()).thenReturn("id");
    when(issuer.getValue()).thenReturn(issuerStr);
    when(idpMetadata.getSingleLogoutBinding()).thenReturn(SamlProtocol.POST_BINDING);
    when(idpMetadata.getSingleLogoutLocation()).thenReturn(postLogoutUrl);
    Response response = logoutRequestService.postLogoutRequest(null, encodedSamlResponse, relayState);
    assertEquals(Response.Status.SEE_OTHER.getStatusCode(), response.getStatus());
    assertTrue("Expected a successful logout message", response.getLocation().toString().contains("logged+out+successfully."));
}
Also used : LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Response(javax.ws.rs.core.Response) LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Issuer(org.opensaml.saml.saml2.core.Issuer) Matchers.anyString(org.mockito.Matchers.anyString) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 9 with RelayState

use of org.opensaml.saml.saml2.ecp.RelayState in project ddf by codice.

the class IdpHandler method doHttpRedirectBinding.

private void doHttpRedirectBinding(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    String redirectUrl;
    String idpRequest = null;
    String relayState = createRelayState(request);
    try {
        IDPSSODescriptor idpssoDescriptor = idpMetadata.getDescriptor();
        if (idpssoDescriptor == null) {
            throw new ServletException("IdP metadata is missing. No IDPSSODescriptor present.");
        }
        String queryParams = String.format("SAMLRequest=%s&RelayState=%s", encodeAuthnRequest(createAndSignAuthnRequest(false, idpssoDescriptor.getWantAuthnRequestsSigned()), false), URLEncoder.encode(relayState, "UTF-8"));
        idpRequest = idpMetadata.getSingleSignOnLocation() + "?" + queryParams;
        UriBuilder idpUri = new UriBuilderImpl(new URI(idpRequest));
        simpleSign.signUriString(queryParams, idpUri);
        redirectUrl = idpUri.build().toString();
    } catch (UnsupportedEncodingException e) {
        LOGGER.info("Unable to encode relay state: {}", relayState, e);
        throw new ServletException("Unable to create return location");
    } catch (SimpleSign.SignatureException e) {
        String msg = "Unable to sign request";
        LOGGER.info(msg, e);
        throw new ServletException(msg);
    } catch (URISyntaxException e) {
        LOGGER.info("Unable to parse IDP request location: {}", idpRequest, e);
        throw new ServletException("Unable to determine IDP location.");
    }
    try {
        response.sendRedirect(redirectUrl);
        response.flushBuffer();
    } catch (IOException e) {
        LOGGER.info("Unable to redirect AuthnRequest to {}", redirectUrl, e);
        throw new ServletException("Unable to redirect to IdP");
    }
}
Also used : ServletException(javax.servlet.ServletException) SimpleSign(ddf.security.samlp.SimpleSign) IDPSSODescriptor(org.opensaml.saml.saml2.metadata.IDPSSODescriptor) UnsupportedEncodingException(java.io.UnsupportedEncodingException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UriBuilder(javax.ws.rs.core.UriBuilder) UriBuilderImpl(org.apache.cxf.jaxrs.impl.UriBuilderImpl) URI(java.net.URI)

Example 10 with RelayState

use of org.opensaml.saml.saml2.ecp.RelayState in project ddf by codice.

the class LogoutRequestServiceTest method testGetLogoutRequestResponse.

@Test
public void testGetLogoutRequestResponse() throws Exception {
    String signature = "signature";
    String signatureAlgorithm = "sha1";
    String relayState = UUID.randomUUID().toString();
    String deflatedSamlResponse = RestSecurity.deflateAndBase64Encode("deflatedSamlResponse");
    LogoutResponse logoutResponse = mock(LogoutResponse.class);
    when(logoutResponse.getIssueInstant()).thenReturn(new DateTime());
    when(logoutResponse.getVersion()).thenReturn(SAMLVersion.VERSION_20);
    when(logoutResponse.getID()).thenReturn("id");
    when(logoutMessage.extractSamlLogoutResponse(eq("deflatedSamlResponse"))).thenReturn(logoutResponse);
    Response response = logoutRequestService.getLogoutRequest(null, deflatedSamlResponse, relayState, signatureAlgorithm, signature);
    assertEquals(Response.Status.SEE_OTHER.getStatusCode(), response.getStatus());
    assertTrue("Expected a successful logout message", response.getLocation().toString().contains("logged+out+successfully."));
}
Also used : LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Response(javax.ws.rs.core.Response) LogoutResponse(org.opensaml.saml.saml2.core.LogoutResponse) Matchers.anyString(org.mockito.Matchers.anyString) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Aggregations

LogoutResponse (org.opensaml.saml.saml2.core.LogoutResponse)18 Response (javax.ws.rs.core.Response)14 Test (org.junit.Test)10 Matchers.anyString (org.mockito.Matchers.anyString)10 IOException (java.io.IOException)9 ValidationException (ddf.security.samlp.ValidationException)8 LogoutRequest (org.opensaml.saml.saml2.core.LogoutRequest)8 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)7 XMLStreamException (javax.xml.stream.XMLStreamException)6 Path (javax.ws.rs.Path)5 SimpleSign (ddf.security.samlp.SimpleSign)4 GET (javax.ws.rs.GET)4 NewCookie (javax.ws.rs.core.NewCookie)4 AuthnRequest (org.opensaml.saml.saml2.core.AuthnRequest)4 SecurityServiceException (ddf.security.service.SecurityServiceException)3 URI (java.net.URI)3 POST (javax.ws.rs.POST)3 DateTime (org.joda.time.DateTime)3 URISyntaxException (java.net.URISyntaxException)2 Cookie (javax.servlet.http.Cookie)2