use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.
the class SecurityFilterChainTest method createMockSecurityFilter.
private SecurityFilter createMockSecurityFilter(final String name) throws IOException, AuthenticationException {
SecurityFilter mockFilter = mock(SecurityFilter.class);
Mockito.when(mockFilter.toString()).thenReturn(name);
Mockito.doAnswer(invocation -> {
Object[] args = invocation.getArguments();
LOGGER.debug("{} was called.", name);
((org.codice.ddf.platform.filter.SecurityFilterChain) args[2]).doFilter(((ServletRequest) args[0]), ((ServletResponse) args[1]));
return null;
}).when(mockFilter).doFilter(any(ServletRequest.class), any(ServletResponse.class), any(org.codice.ddf.platform.filter.SecurityFilterChain.class));
return mockFilter;
}
use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.
the class SecurityFilterChainTest method testAddFilterAfterDo.
/**
* Tests that an exception is thrown if a new filter is attempted to be added after the filter has
* been run.
*
* @throws IOException
* @throws ServletException
*/
@Test(expected = IllegalStateException.class)
public void testAddFilterAfterDo() throws IOException, AuthenticationException {
SecurityFilterChain proxyChain = new SecurityFilterChain();
SecurityFilter filter1 = mock(SecurityFilter.class);
proxyChain.doFilter(mock(ServletRequest.class), mock(ServletResponse.class));
proxyChain.addSecurityFilter(filter1);
}
use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.
the class SecurityFilterChainTest method testDoFilter.
/**
* Tests that all of the filters are properly called.
*
* @throws ServletException
* @throws IOException
*/
@Test
public void testDoFilter() throws IOException, ServletException, AuthenticationException {
SecurityFilterChain proxyChain = new SecurityFilterChain();
SecurityFilter filter1 = createMockSecurityFilter("filter1");
SecurityFilter filter2 = createMockSecurityFilter("filter2");
SecurityFilter filter3 = createMockSecurityFilter("filter3");
ServletRequest request = mock(ServletRequest.class);
ServletResponse response = mock(ServletResponse.class);
proxyChain.addSecurityFilter(filter1);
proxyChain.addSecurityFilter(filter2);
proxyChain.addSecurityFilter(filter3);
proxyChain.doFilter(request, response);
// Verify that all of the filters were called once.
verify(filter1).doFilter(request, response, proxyChain);
verify(filter2).doFilter(request, response, proxyChain);
verify(filter3).doFilter(request, response, proxyChain);
}
use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.
the class JettyAuthenticator method validateRequest.
@Override
public Authentication validateRequest(ServletRequest servletRequest, ServletResponse servletResponse, boolean mandatory) throws ServerAuthException {
TreeSet<ServiceReference<SecurityFilter>> sortedSecurityFilterServiceReferences = null;
final BundleContext bundleContext = getContext();
if (bundleContext == null) {
throw new ServerAuthException("Unable to get BundleContext. No servlet SecurityFilters can be applied. Blocking the request processing.");
}
try {
sortedSecurityFilterServiceReferences = new TreeSet<>(bundleContext.getServiceReferences(SecurityFilter.class, null));
} catch (InvalidSyntaxException ise) {
LOGGER.debug("Should never get this exception as there is no filter being passed.");
}
if (!CollectionUtils.isEmpty(sortedSecurityFilterServiceReferences)) {
LOGGER.debug("Found {} filter(s), now filtering...", sortedSecurityFilterServiceReferences.size());
final SecurityFilterChain chain = new SecurityFilterChain();
// run in order of highest to lowest service ranking.
for (ServiceReference<SecurityFilter> securityFilterServiceReference : sortedSecurityFilterServiceReferences) {
final SecurityFilter securityFilter = bundleContext.getService(securityFilterServiceReference);
if (!hasBeenInitialized(securityFilterServiceReference, bundleContext)) {
initializeSecurityFilter(bundleContext, securityFilterServiceReference, securityFilter);
}
chain.addSecurityFilter(securityFilter);
}
try {
chain.doFilter(servletRequest, servletResponse);
} catch (IOException e) {
throw new ServerAuthException("Unable to process security filter. Blocking the request processing.");
} catch (AuthenticationChallengeException e) {
return new Authentication.Challenge() {
};
} catch (AuthenticationException e) {
return new Authentication.Failure() {
};
}
} else {
LOGGER.debug("Did not find any SecurityFilters. Send auth failure...");
return new Authentication.Failure() {
};
}
Subject subject = (Subject) servletRequest.getAttribute(SecurityConstants.SECURITY_SUBJECT);
UserIdentity userIdentity = new JettyUserIdentity(getSecuritySubject(subject));
return new JettyAuthenticatedUser(userIdentity);
}
use of org.codice.ddf.platform.filter.SecurityFilter in project ddf by codice.
the class SecurityFilterChain method doFilter.
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) throws IOException, AuthenticationException {
if (iterator == null) {
iterator = filters.iterator();
}
if (iterator.hasNext()) {
SecurityFilter filter = iterator.next();
LOGGER.debug("Calling filter {}.doFilter({}, {}, {})", filter.getClass().getName(), servletRequest, servletResponse, this);
filter.doFilter(servletRequest, servletResponse, this);
}
}
Aggregations