Search in sources :

Example 1 with SecurityFilter

use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.

the class JettyAuthenticatorTest method testInitializeSecurityFilter.

@Test
public void testInitializeSecurityFilter() throws IOException, ServerAuthException, AuthenticationException {
    // given
    final HttpSession httpSession = mock(HttpSession.class);
    final Request servletRequest = mock(Request.class);
    final ServletContext servletContext = mock(ServletContext.class);
    final Dictionary dictionary = new Hashtable();
    final String filterNameValue = "my-test-filter";
    dictionary.put("osgi.http.whiteboard.filter.name", filterNameValue);
    final String param1Key = "param1Key";
    final String param1Value = "param1Value";
    dictionary.put("init." + param1Key, param1Value);
    final String param2Key = "param2Key";
    final String param2Value = "param2Value";
    dictionary.put("init." + param2Key, param2Value);
    final SecurityFilter securityFilter = registerSecurityFilter(dictionary);
    // when
    jettyAuthenticator.validateRequest(servletRequest, mock(ServletResponse.class), false);
    // then
    verify(securityFilter).init();
}
Also used : Dictionary(java.util.Dictionary) ServletResponse(javax.servlet.ServletResponse) HttpSession(javax.servlet.http.HttpSession) Hashtable(java.util.Hashtable) Request(org.eclipse.jetty.server.Request) ServletRequest(javax.servlet.ServletRequest) SecurityFilter(org.codice.ddf.platform.filter.SecurityFilter) ServletContext(javax.servlet.ServletContext) Test(org.junit.Test)

Example 2 with SecurityFilter

use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.

the class JettyAuthenticatorTest method testDoFilterAfterInitDelegateServletFilterWithNullFilterConfig.

/**
 * The {@link javax.servlet.Filter#init(FilterConfig)} javadoc does not specify that the {@link
 * FilterConfig} argument may not be null. This test confirms that there are no errors when
 * initializing the {@link SecurityFilter}s and filtering if this situation occurs.
 */
@Test
public void testDoFilterAfterInitDelegateServletFilterWithNullFilterConfig() throws IOException, ServerAuthException, AuthenticationException {
    // given
    jettyAuthenticator.setConfiguration(null);
    final SecurityFilter securityFilter = registerSecurityFilter(new Hashtable());
    final ServletRequest servletRequest = mock(ServletRequest.class);
    final ServletResponse servletResponse = mock(ServletResponse.class);
    // when
    jettyAuthenticator.validateRequest(servletRequest, servletResponse, false);
    // then
    final InOrder inOrder = Mockito.inOrder(securityFilter);
    inOrder.verify(securityFilter).init();
    inOrder.verify(securityFilter).doFilter(eq(servletRequest), eq(servletResponse), any(SecurityFilterChain.class));
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) InOrder(org.mockito.InOrder) Hashtable(java.util.Hashtable) SecurityFilter(org.codice.ddf.platform.filter.SecurityFilter) Test(org.junit.Test)

Example 3 with SecurityFilter

use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.

the class JettyAuthenticatorTest method testRemoveSecurityFilter.

@Test
public void testRemoveSecurityFilter() {
    // given
    final SecurityFilter securityFilter = mock(SecurityFilter.class);
    final MockServiceReference securityFilterServiceReference = new MockServiceReference();
    securityFilterServiceReference.setProperties(new Hashtable());
    when(bundleContext.getService(securityFilterServiceReference)).thenReturn(securityFilter);
    registeredSecurityFilterServiceReferences.add(securityFilterServiceReference);
    // when
    jettyAuthenticator.removeSecurityFilter(securityFilterServiceReference);
    // then
    verify(securityFilter).destroy();
}
Also used : MockServiceReference(org.springframework.osgi.mock.MockServiceReference) Hashtable(java.util.Hashtable) SecurityFilter(org.codice.ddf.platform.filter.SecurityFilter) Test(org.junit.Test)

Example 4 with SecurityFilter

use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.

the class JettyAuthenticatorTest method testDoFilterWithSecurityFilter.

@Test
public void testDoFilterWithSecurityFilter() throws IOException, ServerAuthException, AuthenticationException {
    // given
    final SecurityFilter securityFilter = registerSecurityFilter(new Hashtable());
    final ServletRequest servletRequest = mock(ServletRequest.class);
    final ServletResponse servletResponse = mock(ServletResponse.class);
    // when
    jettyAuthenticator.validateRequest(servletRequest, servletResponse, false);
    // then
    final InOrder inOrder = Mockito.inOrder(securityFilter);
    inOrder.verify(securityFilter).doFilter(eq(servletRequest), eq(servletResponse), any(SecurityFilterChain.class));
}
Also used : ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) InOrder(org.mockito.InOrder) Hashtable(java.util.Hashtable) SecurityFilter(org.codice.ddf.platform.filter.SecurityFilter) Test(org.junit.Test)

Example 5 with SecurityFilter

use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.

the class JettyAuthenticatorTest method registerSecurityFilter.

private SecurityFilter registerSecurityFilter(Dictionary serviceProperties) throws IOException, AuthenticationException {
    final SecurityFilter securityFilter = mock(SecurityFilter.class);
    Mockito.doAnswer(invocation -> {
        Object[] args = invocation.getArguments();
        ((SecurityFilterChain) args[2]).doFilter(((ServletRequest) args[0]), ((ServletResponse) args[1]));
        return null;
    }).when(securityFilter).doFilter(any(ServletRequest.class), any(ServletResponse.class), any(SecurityFilterChain.class));
    final MockServiceReference securityFilterServiceReference = new MockServiceReference();
    securityFilterServiceReference.setProperties(serviceProperties);
    when(bundleContext.getService(securityFilterServiceReference)).thenReturn(securityFilter);
    registeredSecurityFilterServiceReferences.add(securityFilterServiceReference);
    return securityFilter;
}
Also used : SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) ServletRequest(javax.servlet.ServletRequest) ServletResponse(javax.servlet.ServletResponse) MockServiceReference(org.springframework.osgi.mock.MockServiceReference) SecurityFilter(org.codice.ddf.platform.filter.SecurityFilter)

Aggregations

SecurityFilter (org.codice.ddf.platform.filter.SecurityFilter)15 ServletRequest (javax.servlet.ServletRequest)11 ServletResponse (javax.servlet.ServletResponse)11 Test (org.junit.Test)11 Hashtable (java.util.Hashtable)7 Dictionary (java.util.Dictionary)4 SecurityFilterChain (org.codice.ddf.platform.filter.SecurityFilterChain)4 InOrder (org.mockito.InOrder)3 ServletContext (javax.servlet.ServletContext)2 HttpSession (javax.servlet.http.HttpSession)2 Request (org.eclipse.jetty.server.Request)2 MockServiceReference (org.springframework.osgi.mock.MockServiceReference)2 Subject (ddf.security.Subject)1 IOException (java.io.IOException)1 AuthenticationChallengeException (org.codice.ddf.platform.filter.AuthenticationChallengeException)1 AuthenticationException (org.codice.ddf.platform.filter.AuthenticationException)1 ServerAuthException (org.eclipse.jetty.security.ServerAuthException)1 Authentication (org.eclipse.jetty.server.Authentication)1 UserIdentity (org.eclipse.jetty.server.UserIdentity)1 BundleContext (org.osgi.framework.BundleContext)1