Search in sources :

Example 51 with HandlerResult

use of org.codice.ddf.security.handler.api.HandlerResult in project ddf by codice.

the class LoginFilterTest method testValidUsernameToken.

@Test
public void testValidUsernameToken() 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.init(filterConfig);
    HttpServletRequest servletRequest = mock(HttpServletRequest.class);
    HttpServletResponse servletResponse = mock(HttpServletResponse.class);
    FilterChain filterChain = mock(FilterChain.class);
    UPAuthenticationToken token = new UPAuthenticationToken("foo", "bar");
    HandlerResult result = new HandlerResult(HandlerResult.Status.COMPLETED, token);
    when(servletRequest.getAttribute("ddf.security.token")).thenReturn(result);
    HttpSession session = mock(HttpSession.class);
    when(servletRequest.getSession(true)).thenReturn(session);
    when(session.getAttribute(SecurityConstants.SAML_ASSERTION)).thenReturn(new SecurityTokenHolder());
    when(sessionFactory.getOrCreateSession(servletRequest)).thenReturn(session);
    Subject subject = mock(Subject.class, RETURNS_DEEP_STUBS);
    when(securityManager.getSubject(token)).thenReturn(subject);
    SecurityAssertion assertion = mock(SecurityAssertion.class);
    SecurityToken securityToken = mock(SecurityToken.class);
    when(assertion.getSecurityToken()).thenReturn(securityToken);
    when(subject.getPrincipals().asList()).thenReturn(Arrays.asList(assertion));
    when(securityToken.getToken()).thenReturn(readDocument("/good_saml.xml").getDocumentElement());
    loginFilter.doFilter(servletRequest, servletResponse, filterChain);
}
Also used : HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) SecurityAssertion(ddf.security.assertion.SecurityAssertion) Subject(ddf.security.Subject) HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) SecurityTokenHolder(ddf.security.common.SecurityTokenHolder) UPAuthenticationToken(org.codice.ddf.security.handler.api.UPAuthenticationToken) FilterConfig(javax.servlet.FilterConfig) SecurityManager(ddf.security.service.SecurityManager) Test(org.junit.Test)

Example 52 with HandlerResult

use of org.codice.ddf.security.handler.api.HandlerResult 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 53 with HandlerResult

use of org.codice.ddf.security.handler.api.HandlerResult 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 54 with HandlerResult

use of org.codice.ddf.security.handler.api.HandlerResult 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)

Example 55 with HandlerResult

use of org.codice.ddf.security.handler.api.HandlerResult in project ddf by codice.

the class CasHandlerTest method testNoPrincipalResolve.

/**
     * Tests that the handler properly returns a REDIRECTED result if the assertion is not in the
     * session and resolve is true.
     *
     * @throws ServletException
     * @throws IOException
     */
@Test
public void testNoPrincipalResolve() throws ServletException, IOException {
    CasHandler handler = createHandler();
    Filter testFilter = mock(Filter.class);
    handler.setProxyFilter(new ProxyFilter(Arrays.asList(testFilter)));
    HandlerResult result = handler.getNormalizedToken(createServletRequest(false), mock(HttpServletResponse.class), new ProxyFilterChain(null), true);
    assertEquals(HandlerResult.Status.REDIRECTED, result.getStatus());
    // verify that the filter was called once
    verify(testFilter).doFilter(any(ServletRequest.class), any(ServletResponse.class), any(FilterChain.class));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) ProxyFilter(org.codice.ddf.security.handler.cas.filter.ProxyFilter) AbstractCasFilter(org.jasig.cas.client.util.AbstractCasFilter) Filter(javax.servlet.Filter) ProxyFilter(org.codice.ddf.security.handler.cas.filter.ProxyFilter) FilterChain(javax.servlet.FilterChain) ProxyFilterChain(org.codice.ddf.security.handler.cas.filter.ProxyFilterChain) 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)74 HttpServletRequest (javax.servlet.http.HttpServletRequest)44 Test (org.junit.Test)44 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 HandlerResultImpl (org.codice.ddf.security.handler.HandlerResultImpl)17 SecurityFilterChain (org.codice.ddf.platform.filter.SecurityFilterChain)15 FilterChain (javax.servlet.FilterChain)13 IOException (java.io.IOException)11 ServletException (javax.servlet.ServletException)8 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)8 HttpSession (javax.servlet.http.HttpSession)7 AuthenticationHandler (org.codice.ddf.security.handler.api.AuthenticationHandler)7 Element (org.w3c.dom.Element)7 ServletRequest (javax.servlet.ServletRequest)6 ServletResponse (javax.servlet.ServletResponse)6 ContextPolicyManager (org.codice.ddf.security.policy.context.ContextPolicyManager)6 AuthenticationException (org.codice.ddf.platform.filter.AuthenticationException)5 BaseAuthenticationToken (org.codice.ddf.security.handler.api.BaseAuthenticationToken)5 SAMLAuthenticationToken (org.codice.ddf.security.handler.api.SAMLAuthenticationToken)5 ProxyFilterChain (org.codice.ddf.security.handler.cas.filter.ProxyFilterChain)5