use of org.codice.ddf.platform.filter.SecurityFilterChain in project ddf by codice.
the class BasicAuthenticationHandlerTest method testGetNormalizedTokenNoResolveCompleted.
/**
* This test case handles the scenario in which the credentials are not to be obtained (i.e.
* resolve flag is not set) and the UsernameTokenType was successfully created from the HTTP
* request.
*/
@Test
public void testGetNormalizedTokenNoResolveCompleted() throws Exception {
BasicAuthenticationHandler handler = new BasicAuthenticationHandler();
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
SecurityFilterChain chain = mock(SecurityFilterChain.class);
when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn("Basic " + Base64.getEncoder().encodeToString(CREDENTIALS.getBytes()));
HandlerResult result = handler.getNormalizedToken(request, response, chain, false);
assertNotNull(result);
assertEquals(HandlerResult.Status.COMPLETED, result.getStatus());
assertEquals("admin", getAttributeValue(result.getToken(), USERNAME_ATTR));
}
use of org.codice.ddf.platform.filter.SecurityFilterChain in project ddf by codice.
the class BasicAuthenticationHandlerTest method testGetNormalizedTokenResolveCompleted.
/**
* This test case handles the scenario in which the credentials should be obtained (i.e. resolve
* flag is set) and UsernameTokenType was created from the HTTP request.
*/
@Test
public void testGetNormalizedTokenResolveCompleted() throws Exception {
BasicAuthenticationHandler handler = new BasicAuthenticationHandler();
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
SecurityFilterChain chain = mock(SecurityFilterChain.class);
when(request.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn("Basic " + Base64.getEncoder().encodeToString(CREDENTIALS.getBytes()));
HandlerResult result = handler.getNormalizedToken(request, response, chain, true);
assertNotNull(result);
assertEquals(HandlerResult.Status.COMPLETED, result.getStatus());
assertEquals("admin", getAttributeValue(result.getToken(), USERNAME_ATTR));
}
use of org.codice.ddf.platform.filter.SecurityFilterChain 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;
}
use of org.codice.ddf.platform.filter.SecurityFilterChain in project ddf by codice.
the class WebSSOFilterTest method testDoFilterWithRedirected.
@Test
public void testDoFilterWithRedirected() throws AuthenticationException, IOException {
ContextPolicy testPolicy = mock(ContextPolicy.class);
ContextPolicyManager policyManager = mock(ContextPolicyManager.class);
when(policyManager.getContextPolicy(MOCK_CONTEXT)).thenReturn(testPolicy);
when(policyManager.isWhiteListed(MOCK_CONTEXT)).thenReturn(false);
when(policyManager.getSessionAccess()).thenReturn(false);
WebSSOFilter filter = new WebSSOFilter();
// set handlers
AuthenticationHandler handler1 = mock(AuthenticationHandler.class);
HandlerResult noActionResult = mock(HandlerResult.class);
when(noActionResult.getStatus()).thenReturn(Status.NO_ACTION);
HandlerResult redirectedResult = mock(HandlerResult.class);
when(redirectedResult.getStatus()).thenReturn(Status.REDIRECTED);
when(redirectedResult.getToken()).thenReturn(null);
when(handler1.getNormalizedToken(any(ServletRequest.class), any(ServletResponse.class), any(SecurityFilterChain.class), eq(false))).thenReturn(noActionResult);
when(handler1.getNormalizedToken(any(ServletRequest.class), any(ServletResponse.class), any(SecurityFilterChain.class), eq(true))).thenReturn(redirectedResult);
filter.setContextPolicyManager(policyManager);
filter.setHandlerList(Collections.singletonList(handler1));
SecurityFilterChain filterChain = mock(SecurityFilterChain.class);
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getRequestURI()).thenReturn(MOCK_CONTEXT);
HttpServletResponse response = mock(HttpServletResponse.class);
try {
filter.doFilter(request, response, filterChain);
} catch (AuthenticationException e) {
}
// the next filter should NOT be called
verify(filterChain, never()).doFilter(request, response);
verify(request, never()).setAttribute(eq(DDF_AUTHENTICATION_TOKEN), any(HandlerResult.class));
}
use of org.codice.ddf.platform.filter.SecurityFilterChain in project ddf by codice.
the class WebSSOFilterTest method testDoFilterReturnsStatusCode503WhenNoHandlersRegisteredAndGuestAccessDisabled.
@Test
public void testDoFilterReturnsStatusCode503WhenNoHandlersRegisteredAndGuestAccessDisabled() throws IOException, AuthenticationException {
ContextPolicyManager policyManager = mock(ContextPolicyManager.class);
when(policyManager.isWhiteListed(MOCK_CONTEXT)).thenReturn(false);
when(policyManager.getGuestAccess()).thenReturn(false);
when(policyManager.getSessionAccess()).thenReturn(true);
WebSSOFilter filter = new WebSSOFilter();
filter.setContextPolicyManager(policyManager);
SecurityFilterChain filterChain = mock(SecurityFilterChain.class);
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getRequestURI()).thenReturn(MOCK_CONTEXT);
HttpServletResponse response = mock(HttpServletResponse.class);
filter.doFilter(request, response, filterChain);
verify(response).setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
verify(response).flushBuffer();
verify(filterChain, never()).doFilter(request, response);
}
Aggregations