Search in sources :

Example 61 with HandlerResult

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

the class PKIHandlerTest method testGetNormalizedTokenFailsWhenCrlFails.

/**
 * Tests that the PKIHandler returns REDIRECTED when the cert fails to pass the CRL check
 */
@Test
public void testGetNormalizedTokenFailsWhenCrlFails() throws Exception {
    PKIHandler handler = getPKIHandlerWithMockedCrl(false);
    HttpServletRequest request = mock(HttpServletRequest.class);
    HttpServletResponse response = mock(HttpServletResponse.class);
    SecurityFilterChain chain = mock(SecurityFilterChain.class);
    when(request.getAttribute(("javax.servlet.request.X509Certificate"))).thenReturn(getTestCerts());
    // should return REDIRECTED
    HandlerResult handlerResult = handler.getNormalizedToken(request, response, chain, true);
    assertThat(handlerResult.getStatus(), equalTo(HandlerResult.Status.REDIRECTED));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) Test(org.junit.Test)

Example 62 with HandlerResult

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

the class IdpHandlerTest method testGetNormalizedToken.

@Test
public void testGetNormalizedToken() throws Exception {
    when(httpRequest.getHeader("User-Agent")).thenReturn(BROWSER_USER_AGENT);
    HandlerResult handlerResult = idpHandler.getNormalizedToken(httpRequest, httpResponse, null, false);
    assertThat("Expected a non null handlerRequest", handlerResult, is(notNullValue(HandlerResult.class)));
    assertThat(handlerResult.getStatus(), equalTo(HandlerResult.Status.REDIRECTED));
}
Also used : HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) Test(org.junit.Test)

Example 63 with HandlerResult

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

the class IdpHandlerTest method testGetNormalizedTokenLegacyClient.

@Test
public void testGetNormalizedTokenLegacyClient() throws Exception {
    HandlerResult handlerResult = idpHandler.getNormalizedToken(httpRequest, httpResponse, null, false);
    assertThat("Expected a non null handlerRequest", handlerResult, is(notNullValue(HandlerResult.class)));
    assertThat(handlerResult.getStatus(), equalTo(HandlerResult.Status.NO_ACTION));
}
Also used : HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) Test(org.junit.Test)

Example 64 with HandlerResult

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

the class PKIHandler method getNormalizedToken.

/**
 * Handler implementing PKI authentication. Returns the {@link
 * org.codice.ddf.security.handler.api.HandlerResult} containing a BinarySecurityToken if the
 * operation was successful.
 *
 * @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 result of handling this request - status and optional tokens
 */
@Override
public HandlerResult getNormalizedToken(ServletRequest request, ServletResponse response, SecurityFilterChain chain, boolean resolve) {
    HandlerResult handlerResult = new HandlerResultImpl(HandlerResult.Status.NO_ACTION, null);
    handlerResult.setSource(SOURCE);
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String path = httpRequest.getServletPath();
    LOGGER.debug("Doing PKI authentication and authorization for path {}", path);
    // doesn't matter what the resolve flag is set to, we do the same action
    X509Certificate[] certs = (X509Certificate[]) request.getAttribute("javax.servlet.request.X509Certificate");
    AuthenticationToken token = tokenFactory.fromCertificates(certs, request.getRemoteAddr());
    HttpServletResponse httpResponse = response instanceof HttpServletResponse ? (HttpServletResponse) response : null;
    // The httpResponse was null, return no action and try to process with other handlers
    if (httpResponse == null && resolve) {
        LOGGER.debug("HTTP Response was null for request {}", path);
        return handlerResult;
    }
    // No auth info was extracted, return NO_ACTION
    if (token == null) {
        return handlerResult;
    }
    // WebSSOFilter
    if (crlChecker == null) {
        crlChecker = new CrlChecker(securityLogger);
    }
    if (crlChecker.passesCrlCheck(certs) && ocspService.passesOcspCheck(certs)) {
        handlerResult.setToken(token);
        handlerResult.setStatus(HandlerResult.Status.COMPLETED);
    } else {
        if (httpResponse == null) {
            LOGGER.error("Error returning revoked certificate request because the HTTP response object is invalid.");
        } else {
            try {
                httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN, "Your certificate is revoked.");
                httpResponse.flushBuffer();
                LOGGER.info("The certificate used to complete the request has been revoked.");
            } catch (Exception e) {
                LOGGER.error("Error returning revoked certificate request.");
            }
        }
        handlerResult.setStatus(HandlerResult.Status.REDIRECTED);
    }
    return handlerResult;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) HandlerResultImpl(org.codice.ddf.security.handler.HandlerResultImpl) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) X509Certificate(java.security.cert.X509Certificate)

Example 65 with HandlerResult

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

the class LoginFilterTest method testInvalidReference.

@Test
public void testInvalidReference() throws Exception {
    HandlerResult result = new HandlerResultImpl(HandlerResult.Status.COMPLETED, referenceTokenMock);
    when(requestMock.getAttribute(AUTHENTICATION_TOKEN_KEY)).thenReturn(result);
    when(sessionMock.getAttribute(SECURITY_TOKEN_KEY)).thenReturn(badPrincipalHolderMock);
    loginFilter.doFilter(requestMock, responseMock, FAIL_FILTER_CHAIN);
    verify(requestMock, times(0)).setAttribute(any(), any());
}
Also used : HandlerResultImpl(org.codice.ddf.security.handler.HandlerResultImpl) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) 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