Search in sources :

Example 16 with HandlerResult

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);
}
Also used : FilterChain(javax.servlet.FilterChain) Element(org.w3c.dom.Element) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) SAMLAuthenticationToken(org.codice.ddf.security.handler.api.SAMLAuthenticationToken) ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) FilterConfig(javax.servlet.FilterConfig) SecurityManager(ddf.security.service.SecurityManager) Test(org.junit.Test)

Example 17 with HandlerResult

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());
}
Also used : ServletException(javax.servlet.ServletException) GuestAuthenticationToken(org.codice.ddf.security.handler.api.GuestAuthenticationToken) PKIHandler(org.codice.ddf.security.handler.pki.PKIHandler) BasicAuthenticationHandler(org.codice.ddf.security.handler.basic.BasicAuthenticationHandler) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult)

Example 18 with HandlerResult

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;
}
Also used : BaseAuthenticationToken(org.codice.ddf.security.handler.api.BaseAuthenticationToken) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult)

Example 19 with HandlerResult

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GuestAuthenticationToken(org.codice.ddf.security.handler.api.GuestAuthenticationToken) PKIAuthenticationTokenFactory(org.codice.ddf.security.handler.api.PKIAuthenticationTokenFactory) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) Test(org.junit.Test)

Example 20 with HandlerResult

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());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthenticationHandler(org.codice.ddf.security.handler.api.AuthenticationHandler) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) ContextPolicy(org.codice.ddf.security.policy.context.ContextPolicy) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) Test(org.junit.Test)

Aggregations

HandlerResult (org.codice.ddf.security.handler.api.HandlerResult)51 HttpServletRequest (javax.servlet.http.HttpServletRequest)33 HttpServletResponse (javax.servlet.http.HttpServletResponse)33 Test (org.junit.Test)33 FilterChain (javax.servlet.FilterChain)24 ServletException (javax.servlet.ServletException)10 IOException (java.io.IOException)8 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)8 ProxyFilterChain (org.codice.ddf.security.handler.cas.filter.ProxyFilterChain)6 Element (org.w3c.dom.Element)6 BaseAuthenticationToken (org.codice.ddf.security.handler.api.BaseAuthenticationToken)5 SAMLAuthenticationToken (org.codice.ddf.security.handler.api.SAMLAuthenticationToken)5 ServletRequest (javax.servlet.ServletRequest)4 ServletResponse (javax.servlet.ServletResponse)4 HttpSession (javax.servlet.http.HttpSession)4 AuthenticationHandler (org.codice.ddf.security.handler.api.AuthenticationHandler)4 Subject (ddf.security.Subject)3 SecurityTokenHolder (ddf.security.common.SecurityTokenHolder)3 SecurityManager (ddf.security.service.SecurityManager)3 FilterConfig (javax.servlet.FilterConfig)3