use of org.codice.ddf.security.jaxrs.impl.SamlSecurity in project ddf by codice.
the class LogoutRequestServiceTest method testGetLogoutRequestResponseInvalidSignature.
@Test
public void testGetLogoutRequestResponseInvalidSignature() throws Exception {
SamlSecurity samlSecurity = new SamlSecurity();
String deflatedSamlResponse = samlSecurity.deflateAndBase64Encode(UNENCODED_SAML_RESPONSE);
LogoutResponse logoutResponse = mock(LogoutResponse.class);
LogoutWrapper<LogoutResponse> responseLogoutWrapper = new LogoutWrapperImpl<>(logoutResponse);
when(logoutMessage.extractSamlLogoutResponse(eq(UNENCODED_SAML_RESPONSE))).thenReturn(responseLogoutWrapper);
logoutRequestService.setLogoutMessage(logoutMessage);
LogoutRequestService lrs = new LogoutRequestService(simpleSign, idpMetadata, relayStates);
lrs.setEncryptionService(encryptionService);
lrs.setLogOutPageTimeOut(LOGOUT_PAGE_TIMEOUT);
lrs.setLogoutMessage(logoutMessage);
lrs.setRequest(request);
lrs.setSessionFactory(sessionFactory);
lrs.setSamlSecurity(samlSecurity);
lrs.init();
Response response = lrs.getLogoutRequest(null, deflatedSamlResponse, relayState, SIGNATURE_ALGORITHM, SIGNATURE);
assertEquals(Response.Status.SEE_OTHER.getStatusCode(), response.getStatus());
String msg = "Unable to validate".replaceAll(" ", "+");
assertTrue("Expected message containing " + msg, response.getLocation().getQuery().contains(msg));
}
use of org.codice.ddf.security.jaxrs.impl.SamlSecurity in project ddf by codice.
the class LogoutMessageImpl method signSamlGet.
private URI signSamlGet(LogoutWrapper samlObject, URI target, String relayState, String requestType) throws LogoutSecurityException, SignatureException, IOException {
try {
Document doc = DOMUtils.createDocument();
doc.appendChild(doc.createElement("root"));
SamlSecurity samlSecurity = new SamlSecurity();
String encodedResponse = URLEncoder.encode(samlSecurity.deflateAndBase64Encode(DOM2Writer.nodeToString(OpenSAMLUtil.toDom((XMLObject) samlObject.getMessage(), doc, false))), "UTF-8");
String requestToSign = String.format("%s=%s&%s=%s", requestType, encodedResponse, SSOConstants.RELAY_STATE, relayState);
UriBuilder uriBuilder = UriBuilder.fromUri(target);
uriBuilder.queryParam(requestType, encodedResponse);
uriBuilder.queryParam(SSOConstants.RELAY_STATE, relayState);
new SimpleSign(systemCrypto).signUriString(requestToSign, uriBuilder);
return uriBuilder.build();
} catch (WSSecurityException e) {
throw new LogoutSecurityException(e);
}
}
use of org.codice.ddf.security.jaxrs.impl.SamlSecurity in project ddf by codice.
the class PaosInInterceptorTest method getHttpUnsuccessfulResponseHandlerHeaderTest.
@Test
public void getHttpUnsuccessfulResponseHandlerHeaderTest() throws IOException {
Message message = new MessageImpl();
message.put(Message.HTTP_REQUEST_METHOD, "GET");
HashMap<String, List> protocolHeaders = new HashMap<>();
message.put(Message.PROTOCOL_HEADERS, protocolHeaders);
protocolHeaders.put("X-Custom-Header", Collections.singletonList("Custom"));
PaosInInterceptor paosInInterceptor = spy(new PaosInInterceptor(Phase.RECEIVE, new SamlSecurity()));
doReturn(true).when(paosInInterceptor).isRedirect(any(HttpRequest.class), any(HttpResponse.class), any(String.class));
GenericUrl url = new GenericUrl("https://localhost:8993/PAOSConsumer");
HttpRequest request = new MockHttpTransport().createRequestFactory().buildGetRequest(url);
request.getUrl().set("url", "https://localhost:8993/PAOSConsumer");
// Using request.execute to create an HttpResponse since it's final and cannot be mocked
HttpResponse response = request.execute();
response.getHeaders().setLocation("https://localhost:8993/PAOSConsumer");
response.getHeaders().set("set-cookie", Collections.singletonList("cookie"));
HttpUnsuccessfulResponseHandler responseHandler = paosInInterceptor.getHttpUnsuccessfulResponseHandler(message);
boolean returned = responseHandler.handleResponse(request, response, true);
assertThat(returned, is(true));
// HttpHeaders ignores header case
assertThat(request.getHeaders().containsKey("x-custom-header"), is(true));
assertThat(request.getHeaders().get("x-custom-header"), is(Collections.singletonList("Custom")));
}
use of org.codice.ddf.security.jaxrs.impl.SamlSecurity in project ddf by codice.
the class PaosInInterceptorTest method handleMessagePaosResponseBasicBad.
@Test(expected = Fault.class)
public void handleMessagePaosResponseBasicBad() throws IOException {
Message message = new MessageImpl();
message.setContent(InputStream.class, PaosInInterceptorTest.class.getClassLoader().getResource("ecprequest.xml").openStream());
message.put(Message.CONTENT_TYPE, "application/vnd.paos+xml");
Message outMessage = new MessageImpl();
HashMap<String, List> protocolHeaders = new HashMap<>();
outMessage.put(Message.PROTOCOL_HEADERS, protocolHeaders);
outMessage.put(Message.HTTP_REQUEST_METHOD, "GET");
protocolHeaders.put("Authorization", Collections.singletonList("BASIC dGVzdDp0ZXN0"));
ExchangeImpl exchange = new ExchangeImpl();
exchange.setOutMessage(outMessage);
message.setExchange(exchange);
PaosInInterceptor paosInInterceptor = new PaosInInterceptor(Phase.RECEIVE, new SamlSecurity()) {
HttpResponseWrapper getHttpResponse(String responseConsumerURL, String soapResponse, Message message) throws IOException {
HttpResponseWrapper httpResponseWrapper = new HttpResponseWrapper();
if (responseConsumerURL.equals("https://sp.example.org/PAOSConsumer")) {
httpResponseWrapper.statusCode = 400;
httpResponseWrapper.content = new ByteArrayInputStream("actual content".getBytes());
} else if (responseConsumerURL.equals("https://idp.example.org/saml2/sso")) {
httpResponseWrapper.statusCode = 200;
httpResponseWrapper.content = PaosInInterceptorTest.class.getClassLoader().getResource("idpresponse.xml").openStream();
}
return httpResponseWrapper;
}
};
paosInInterceptor.handleMessage(message);
}
use of org.codice.ddf.security.jaxrs.impl.SamlSecurity in project ddf by codice.
the class PaosInInterceptorTest method handleMessagePaosResponseBasicNoIdp.
@Test(expected = Fault.class)
public void handleMessagePaosResponseBasicNoIdp() throws IOException {
Message message = new MessageImpl();
message.setContent(InputStream.class, PaosInInterceptorTest.class.getClassLoader().getResource("ecprequest_noidp.xml").openStream());
message.put(Message.CONTENT_TYPE, "application/vnd.paos+xml");
Message outMessage = new MessageImpl();
HashMap<String, List> protocolHeaders = new HashMap<>();
outMessage.put(Message.PROTOCOL_HEADERS, protocolHeaders);
outMessage.put(Message.HTTP_REQUEST_METHOD, "GET");
protocolHeaders.put("Authorization", Collections.singletonList("BASIC dGVzdDp0ZXN0"));
ExchangeImpl exchange = new ExchangeImpl();
exchange.setOutMessage(outMessage);
message.setExchange(exchange);
PaosInInterceptor paosInInterceptor = new PaosInInterceptor(Phase.RECEIVE, new SamlSecurity());
paosInInterceptor.handleMessage(message);
}
Aggregations