use of org.codice.ddf.security.handler.api.AuthenticationHandler in project ddf by codice.
the class WebSSOFilterTest method testInit.
@Test
public void testInit() {
final Logger logger = (Logger) LoggerFactory.getLogger(WebSSOFilter.class);
logger.setLevel(Level.DEBUG);
AuthenticationHandler handlerMock = mock(AuthenticationHandler.class);
when(handlerMock.getAuthenticationType()).thenReturn("basic");
ContextPolicyManager policyManager = mock(ContextPolicyManager.class);
when(policyManager.getSessionAccess()).thenReturn(true);
WebSSOFilter webSSOFilter = new WebSSOFilter();
webSSOFilter.setHandlerList(Collections.singletonList(handlerMock));
webSSOFilter.setContextPolicyManager(policyManager);
webSSOFilter.init();
logger.setLevel(Level.OFF);
}
use of org.codice.ddf.security.handler.api.AuthenticationHandler in project ddf by codice.
the class WebSSOFilterTest method testDoFilterResolvingOnSecondCall.
@Test
public void testDoFilterResolvingOnSecondCall() throws IOException, AuthenticationException {
ContextPolicy testPolicy = mock(ContextPolicy.class);
when(testPolicy.getAuthenticationMethods()).thenReturn(Collections.singletonList("basic"));
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);
when(handler1.getAuthenticationType()).thenReturn("basic");
HandlerResult noActionResult = mock(HandlerResult.class);
when(noActionResult.getStatus()).thenReturn(Status.NO_ACTION);
HandlerResult completedResult = mock(HandlerResult.class);
when(completedResult.getStatus()).thenReturn(Status.COMPLETED);
when(completedResult.getToken()).thenReturn(null);
when(handler1.getNormalizedToken(any(ServletRequest.class), any(ServletResponse.class), any(SecurityFilterChain.class), eq(true))).thenReturn(completedResult);
when(handler1.getNormalizedToken(any(ServletRequest.class), any(ServletResponse.class), any(SecurityFilterChain.class), eq(false))).thenReturn(noActionResult);
filter.setContextPolicyManager(policyManager);
filter.setHandlerList(Collections.singletonList(handler1));
filter.setSecurityLogger(mock(SecurityLogger.class));
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) {
}
verify(handler1, times(2)).getNormalizedToken(any(HttpServletRequest.class), any(HttpServletResponse.class), any(SecurityFilterChain.class), anyBoolean());
// 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.security.handler.api.AuthenticationHandler in project ddf by codice.
the class WebSSOFilterTest method testDoFilterGetResultFromSession.
@Test
public void testDoFilterGetResultFromSession() throws Exception {
PrincipalCollection principalCollectionMock = mock(PrincipalCollection.class);
when(principalCollectionMock.byType(any())).thenReturn(Collections.singletonList("principal"));
PrincipalHolder principalHolderMock = mock(PrincipalHolder.class);
when(principalHolderMock.getPrincipals()).thenReturn(principalCollectionMock);
HttpSession sessionMock = mock(HttpSession.class);
when(sessionMock.getAttribute(SECURITY_TOKEN_KEY)).thenReturn(principalHolderMock);
HttpServletRequest requestMock = mock(HttpServletRequest.class);
when(requestMock.getSession(any(Boolean.class))).thenReturn(sessionMock);
when(requestMock.getRequestURI()).thenReturn(MOCK_CONTEXT);
when(requestMock.getRequestedSessionId()).thenReturn("JSESSIONID");
HttpServletResponse responseMock = mock(HttpServletResponse.class);
ContextPolicyManager policyManager = mock(ContextPolicyManager.class);
when(policyManager.getSessionAccess()).thenReturn(true);
when(policyManager.isWhiteListed(MOCK_CONTEXT)).thenReturn(false);
ContextPolicy testPolicy = mock(ContextPolicy.class);
when(testPolicy.getAuthenticationMethods()).thenReturn(Collections.singletonList("basic"));
when(policyManager.getContextPolicy(MOCK_CONTEXT)).thenReturn(testPolicy);
AuthenticationHandler handlerMock = mock(AuthenticationHandler.class);
when(handlerMock.getAuthenticationType()).thenReturn("basic");
HandlerResult completedResult = mock(HandlerResult.class);
when(completedResult.getStatus()).thenReturn(Status.COMPLETED);
when(completedResult.getToken()).thenReturn(mock(BaseAuthenticationToken.class));
when(handlerMock.getNormalizedToken(any(ServletRequest.class), any(ServletResponse.class), any(SecurityFilterChain.class), anyBoolean())).thenReturn(completedResult);
SecurityFilterChain filterChain = mock(SecurityFilterChain.class);
WebSSOFilter filter = new WebSSOFilter();
filter.setContextPolicyManager(policyManager);
filter.setHandlerList(Collections.singletonList(handlerMock));
filter.doFilter(requestMock, responseMock, filterChain);
verify(sessionMock, times(1)).getAttribute(SECURITY_TOKEN_KEY);
verify(handlerMock, times(0)).getNormalizedToken(any(), any(), any(), anyBoolean());
verify(requestMock, times(1)).setAttribute(eq(AUTHENTICATION_TOKEN_KEY), any());
}
use of org.codice.ddf.security.handler.api.AuthenticationHandler in project ddf by codice.
the class WebSSOFilter method getHandlerList.
private List<AuthenticationHandler> getHandlerList(String path) {
List<AuthenticationHandler> handlers = new ArrayList<>();
String handlerAuthMethod;
if (contextPolicyManager != null) {
ContextPolicy policy = contextPolicyManager.getContextPolicy(path);
if (policy != null) {
Collection<String> authMethods = policy.getAuthenticationMethods();
for (String authMethod : authMethods) {
for (AuthenticationHandler handler : this.handlerList) {
handlerAuthMethod = handler.getAuthenticationType();
LOGGER.trace("Handler auth method: {} - desired auth method {}", handlerAuthMethod, authMethod);
if (handler.getAuthenticationType().equalsIgnoreCase(authMethod)) {
handlers.add(handler);
}
}
}
}
} else {
// if no manager, get a list of all the handlers.
handlers.addAll(this.handlerList);
}
LOGGER.trace("Returning {} handlers that support desired auth methods for path {}", handlers.size(), path);
return handlers;
}
Aggregations