use of org.codice.ddf.security.handler.api.HandlerResult in project ddf by codice.
the class LoginFilterTest method testExpiredSamlCookie.
@Test(expected = ServletException.class)
public void testExpiredSamlCookie() throws IOException, XMLStreamException, ServletException, ParserConfigurationException, SAXException, SecurityServiceException {
FilterConfig filterConfig = mock(FilterConfig.class);
LoginFilter loginFilter = new LoginFilter();
loginFilter.setSessionFactory(sessionFactory);
ddf.security.service.SecurityManager securityManager = mock(ddf.security.service.SecurityManager.class);
loginFilter.setSecurityManager(securityManager);
loginFilter.setSignaturePropertiesFile("signature.properties");
try {
loginFilter.init(filterConfig);
} catch (ServletException e) {
fail(e.getMessage());
}
HttpServletRequest servletRequest = new TestHttpServletRequest();
HttpServletResponse servletResponse = mock(HttpServletResponse.class);
FilterChain filterChain = mock(FilterChain.class);
SecurityToken securityToken = new SecurityToken();
Element thisToken = readDocument("/good_saml.xml").getDocumentElement();
securityToken.setToken(thisToken);
SAMLAuthenticationToken samlToken = new SAMLAuthenticationToken(null, securityToken, "karaf");
HandlerResult result = new HandlerResult(HandlerResult.Status.COMPLETED, samlToken);
servletRequest.setAttribute("ddf.security.token", result);
loginFilter.doFilter(servletRequest, servletResponse, filterChain);
}
use of org.codice.ddf.security.handler.api.HandlerResult in project ddf by codice.
the class GuestHandler method getAuthToken.
/**
* Returns BSTAuthenticationToken for the HttpServletRequest
*
* @param request http request to obtain attributes from and to pass into any local filter chains required
* @return BSTAuthenticationToken
*/
private BaseAuthenticationToken getAuthToken(HttpServletRequest request, HttpServletResponse response, FilterChain chain) {
//check for basic auth first
String realm = (String) request.getAttribute(ContextPolicy.ACTIVE_REALM);
BasicAuthenticationHandler basicAuthenticationHandler = new BasicAuthenticationHandler();
HandlerResult handlerResult = basicAuthenticationHandler.getNormalizedToken(request, response, chain, false);
if (handlerResult.getStatus().equals(HandlerResult.Status.COMPLETED)) {
return handlerResult.getToken();
}
//if basic fails, check for PKI
PKIHandler pkiHandler = new PKIHandler();
pkiHandler.setTokenFactory(tokenFactory);
try {
handlerResult = pkiHandler.getNormalizedToken(request, response, chain, false);
if (handlerResult.getStatus().equals(HandlerResult.Status.COMPLETED)) {
return handlerResult.getToken();
}
} catch (ServletException e) {
LOGGER.info("Encountered an exception while checking for PKI auth info.", e);
}
return new GuestAuthenticationToken(realm, request.getRemoteAddr());
}
use of org.codice.ddf.security.handler.api.HandlerResult in project ddf by codice.
the class GuestHandler method getNormalizedToken.
/**
* This method takes a guest request and attaches a username token
* to the HTTP request to allow access. The method also allows the user to
* sign-in and authenticate.
*
* @param request http request to obtain attributes from and to pass into any local filter chains required
* @param response http response to return http responses or redirects
* @param chain original filter chain (should not be called from your handler)
* @param resolve flag with true implying that credentials should be obtained, false implying return if no credentials are found.
* @return HandlerResult
*/
@Override
public HandlerResult getNormalizedToken(ServletRequest request, ServletResponse response, FilterChain chain, boolean resolve) {
HandlerResult result = new HandlerResult();
String realm = (String) request.getAttribute(ContextPolicy.ACTIVE_REALM);
// For guest - if credentials were provided, return them, if not, then return guest credentials
BaseAuthenticationToken authToken = getAuthToken((HttpServletRequest) request, (HttpServletResponse) response, chain);
result.setSource(realm + "-GuestHandler");
result.setStatus(HandlerResult.Status.COMPLETED);
result.setToken(authToken);
return result;
}
use of org.codice.ddf.security.handler.api.HandlerResult in project ddf by codice.
the class GuestHandlerTest method testGetNormalizedToken.
/**
* This test ensures the proper functionality of GuestHandler's method,
* getNormalizedToken().
*/
@Test
public void testGetNormalizedToken() throws WSSecurityException {
GuestHandler handler = new GuestHandler();
PKIAuthenticationTokenFactory tokenFactory = new PKIAuthenticationTokenFactory();
handler.setTokenFactory(tokenFactory);
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain chain = mock(FilterChain.class);
/**
* Note that the parameters are insignificant as GuestHandler
* does not use them.
*/
HandlerResult result = handler.getNormalizedToken(request, response, chain, true);
assertNotNull(result);
assertEquals(HandlerResult.Status.COMPLETED, result.getStatus());
assertTrue(result.getToken() instanceof GuestAuthenticationToken);
assertEquals("Guest", result.getToken().getCredentials());
assertEquals(null, result.getToken().getRealm());
assertEquals("null-GuestHandler", result.getSource());
}
use of org.codice.ddf.security.handler.api.HandlerResult in project ddf by codice.
the class WebSSOFilterTest method testDoFilterWhiteListed.
@Test
public void testDoFilterWhiteListed() throws IOException, ServletException {
ContextPolicy testPolicy = mock(ContextPolicy.class);
when(testPolicy.getRealm()).thenReturn("TestRealm");
ContextPolicyManager policyManager = mock(ContextPolicyManager.class);
when(policyManager.getContextPolicy(anyString())).thenReturn(testPolicy);
when(policyManager.isWhiteListed(anyString())).thenReturn(true);
WebSSOFilter filter = new WebSSOFilter();
// set handlers
AuthenticationHandler handler1 = mock(AuthenticationHandler.class);
HandlerResult noActionResult = mock(HandlerResult.class);
when(noActionResult.getStatus()).thenReturn(Status.NO_ACTION);
HandlerResult completedResult = mock(HandlerResult.class);
when(completedResult.getStatus()).thenReturn(Status.COMPLETED);
when(completedResult.getToken()).thenReturn(null);
when(handler1.getNormalizedToken(any(ServletRequest.class), any(ServletResponse.class), any(FilterChain.class), eq(true))).thenReturn(completedResult);
when(handler1.getNormalizedToken(any(ServletRequest.class), any(ServletResponse.class), any(FilterChain.class), eq(false))).thenReturn(noActionResult);
filter.setHandlerList(Collections.singletonList(handler1));
filter.setContextPolicyManager(policyManager);
FilterChain filterChain = mock(FilterChain.class);
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getContextPath()).thenReturn(MOCK_CONTEXT);
HttpServletResponse response = mock(HttpServletResponse.class);
filter.doFilter(request, response, filterChain);
verify(request, times(1)).setAttribute(ContextPolicy.ACTIVE_REALM, "TestRealm");
verify(request, times(1)).setAttribute(ContextPolicy.NO_AUTH_POLICY, true);
verify(filterChain).doFilter(request, response);
verify(handler1, never()).getNormalizedToken(any(HttpServletRequest.class), any(HttpServletResponse.class), any(FilterChain.class), anyBoolean());
}
Aggregations