Search in sources :

Example 1 with ProxyFilterChain

use of org.codice.ddf.security.handler.cas.filter.ProxyFilterChain in project ddf by codice.

the class CasHandlerTest method testNoPrincipalNoResolve.

/**
     * Tests that the handler properly returns a NO_ACTION result if no assertion is available in
     * the request and resolve is false.
     *
     * @throws ServletException
     */
@Test
public void testNoPrincipalNoResolve() throws ServletException {
    CasHandler handler = createHandler();
    HandlerResult result = handler.getNormalizedToken(createServletRequest(false), mock(HttpServletResponse.class), new ProxyFilterChain(null), false);
    // NO_ACTION due to resolve being false
    assertEquals(HandlerResult.Status.NO_ACTION, result.getStatus());
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) ProxyFilterChain(org.codice.ddf.security.handler.cas.filter.ProxyFilterChain) Test(org.junit.Test)

Example 2 with ProxyFilterChain

use of org.codice.ddf.security.handler.cas.filter.ProxyFilterChain in project ddf by codice.

the class CasHandler method getNormalizedToken.

@Override
public HandlerResult getNormalizedToken(ServletRequest request, ServletResponse response, FilterChain chain, boolean resolve) throws ServletException {
    // Default to NO_ACTION and set the source as this handler
    HandlerResult handlerResult = new HandlerResult(HandlerResult.Status.NO_ACTION, null);
    handlerResult.setSource(realm + "-" + SOURCE);
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String path = httpRequest.getServletPath();
    LOGGER.debug("Doing CAS authentication and authorization for path {}", path);
    // if the request contains the principal, return it
    Assertion assertion = getAssertion(httpRequest);
    try {
        if (resolve && assertion == null) {
            proxyFilter.doFilter(request, response, new ProxyFilterChain(null));
        }
    } catch (IOException e) {
        throw new ServletException(e);
    }
    if (assertion != null) {
        LOGGER.debug("Found previous CAS attribute, using that same session.");
        CASAuthenticationToken token = getAuthenticationToken(assertion);
        if (token != null) {
            handlerResult.setToken(token);
            handlerResult.setStatus(HandlerResult.Status.COMPLETED);
            //update cache with new information
            LOGGER.debug("Adding new CAS assertion for session {}", httpRequest.getSession(false).getId());
            httpRequest.getSession(false).setAttribute(AbstractCasFilter.CONST_CAS_ASSERTION, assertion);
            LOGGER.debug("Successfully set authentication token, returning result with token.");
        } else {
            LOGGER.debug("Could not create authentication token, returning NO_ACTION result.");
        }
    } else {
        if (resolve) {
            LOGGER.debug("Calling cas authentication and validation filters to perform redirects.");
            handlerResult.setStatus(HandlerResult.Status.REDIRECTED);
        } else {
            LOGGER.debug("No cas authentication information found and resolve is not enabled, returning NO_ACTION.");
        }
    }
    return handlerResult;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) Assertion(org.jasig.cas.client.validation.Assertion) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) IOException(java.io.IOException) ProxyFilterChain(org.codice.ddf.security.handler.cas.filter.ProxyFilterChain)

Example 3 with ProxyFilterChain

use of org.codice.ddf.security.handler.cas.filter.ProxyFilterChain in project ddf by codice.

the class CasHandlerTest method testPrincipalResolve.

/**
     * Tests that the handler properly returns a COMPLETED result if the assertion is in the
     * session and resolve is true.
     *
     * @throws ServletException
     * @throws IOException
     */
@Test
public void testPrincipalResolve() throws ServletException, IOException {
    CasHandler handler = createHandler();
    HandlerResult result = handler.getNormalizedToken(createServletRequest(true), mock(HttpServletResponse.class), new ProxyFilterChain(null), true);
    assertEquals(HandlerResult.Status.COMPLETED, result.getStatus());
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) ProxyFilterChain(org.codice.ddf.security.handler.cas.filter.ProxyFilterChain) Test(org.junit.Test)

Example 4 with ProxyFilterChain

use of org.codice.ddf.security.handler.cas.filter.ProxyFilterChain in project ddf by codice.

the class CasHandlerTest method testCachedPrincipalResolve.

/**
     * Tests that the handler properly returns a COMPLETED result from having a cached session that
     * contains the CAS assertion.
     *
     * @throws ServletException
     * @throws IOException
     */
@Test
public void testCachedPrincipalResolve() throws ServletException, IOException {
    CasHandler handler = createHandler();
    HttpServletRequest servletRequest = createServletRequest(true);
    HttpSession session = servletRequest.getSession();
    HandlerResult result = handler.getNormalizedToken(servletRequest, mock(HttpServletResponse.class), new ProxyFilterChain(null), true);
    assertEquals(HandlerResult.Status.COMPLETED, result.getStatus());
    // now check for caching sessions
    servletRequest = createServletRequest(false);
    when(servletRequest.getSession()).thenReturn(session);
    when(servletRequest.getSession(any(Boolean.class))).thenReturn(session);
    result = handler.getNormalizedToken(servletRequest, mock(HttpServletResponse.class), new ProxyFilterChain(null), true);
    assertEquals(HandlerResult.Status.COMPLETED, result.getStatus());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) ProxyFilterChain(org.codice.ddf.security.handler.cas.filter.ProxyFilterChain) Test(org.junit.Test)

Example 5 with ProxyFilterChain

use of org.codice.ddf.security.handler.cas.filter.ProxyFilterChain in project ddf by codice.

the class CasHandlerTest method testPrincipalNoResolve.

/**
     * Tests that the handler properly returns a COMPLETED result if the assertion is in the session.
     *
     * @throws ServletException
     */
@Test
public void testPrincipalNoResolve() throws ServletException {
    CasHandler handler = createHandler();
    HandlerResult result = handler.getNormalizedToken(createServletRequest(true), mock(HttpServletResponse.class), new ProxyFilterChain(null), false);
    assertEquals(HandlerResult.Status.COMPLETED, result.getStatus());
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) ProxyFilterChain(org.codice.ddf.security.handler.cas.filter.ProxyFilterChain) Test(org.junit.Test)

Aggregations

HandlerResult (org.codice.ddf.security.handler.api.HandlerResult)6 ProxyFilterChain (org.codice.ddf.security.handler.cas.filter.ProxyFilterChain)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Test (org.junit.Test)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 IOException (java.io.IOException)1 Filter (javax.servlet.Filter)1 FilterChain (javax.servlet.FilterChain)1 ServletException (javax.servlet.ServletException)1 ServletRequest (javax.servlet.ServletRequest)1 ServletResponse (javax.servlet.ServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 ProxyFilter (org.codice.ddf.security.handler.cas.filter.ProxyFilter)1 AbstractCasFilter (org.jasig.cas.client.util.AbstractCasFilter)1 Assertion (org.jasig.cas.client.validation.Assertion)1