Search in sources :

Example 1 with ContextPolicyManager

use of org.codice.ddf.security.policy.context.ContextPolicyManager in project ddf by codice.

the class WebSSOFilterTest method testDoFilterWhiteListed.

@Test
public void testDoFilterWhiteListed() throws IOException, AuthenticationException {
    ContextPolicy testPolicy = mock(ContextPolicy.class);
    ContextPolicyManager policyManager = mock(ContextPolicyManager.class);
    when(policyManager.getContextPolicy(anyString())).thenReturn(testPolicy);
    when(policyManager.isWhiteListed(anyString())).thenReturn(true);
    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 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.setHandlerList(Collections.singletonList(handler1));
    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(request, times(1)).setAttribute(ContextPolicy.NO_AUTH_POLICY, true);
    verify(filterChain).doFilter(request, response);
    verify(handler1, never()).getNormalizedToken(any(HttpServletRequest.class), any(HttpServletResponse.class), any(SecurityFilterChain.class), anyBoolean());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthenticationHandler(org.codice.ddf.security.handler.api.AuthenticationHandler) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) ContextPolicy(org.codice.ddf.security.policy.context.ContextPolicy) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) Test(org.junit.Test)

Example 2 with ContextPolicyManager

use of org.codice.ddf.security.policy.context.ContextPolicyManager in project ddf by codice.

the class WebSSOFilterTest method testDoFilterReturnsGuestTokenWhenNoHandlersRegisteredAndGuestAccessEnabled.

@Test
public void testDoFilterReturnsGuestTokenWhenNoHandlersRegisteredAndGuestAccessEnabled() throws IOException, AuthenticationException {
    ContextPolicyManager policyManager = mock(ContextPolicyManager.class);
    when(policyManager.isWhiteListed(MOCK_CONTEXT)).thenReturn(false);
    when(policyManager.getGuestAccess()).thenReturn(true);
    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);
    ArgumentCaptor<HandlerResult> handlerResult = ArgumentCaptor.forClass(HandlerResult.class);
    verify(request).setAttribute(eq(DDF_AUTHENTICATION_TOKEN), handlerResult.capture());
    assertTrue(handlerResult.getValue().getToken() instanceof GuestAuthenticationToken);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) GuestAuthenticationToken(org.codice.ddf.security.handler.GuestAuthenticationToken) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) Test(org.junit.Test)

Example 3 with ContextPolicyManager

use of org.codice.ddf.security.policy.context.ContextPolicyManager in project ddf by codice.

the class WebSSOFilterTest method testDoFilterSessionStorageDisabled.

@Test
public void testDoFilterSessionStorageDisabled() throws Exception {
    PrincipalCollection principalCollectionMock = mock(PrincipalCollection.class);
    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);
    HttpServletResponse responseMock = mock(HttpServletResponse.class);
    ContextPolicyManager policyManager = mock(ContextPolicyManager.class);
    when(policyManager.getSessionAccess()).thenReturn(false);
    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(0)).getAttribute(SECURITY_TOKEN_KEY);
    verify(handlerMock, times(1)).getNormalizedToken(any(), any(), any(), anyBoolean());
    verify(requestMock, times(1)).setAttribute(eq(AUTHENTICATION_TOKEN_KEY), any());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletRequest(javax.servlet.ServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) HttpSession(javax.servlet.http.HttpSession) PrincipalCollection(org.apache.shiro.subject.PrincipalCollection) HttpServletResponse(javax.servlet.http.HttpServletResponse) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) ContextPolicy(org.codice.ddf.security.policy.context.ContextPolicy) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) BaseAuthenticationToken(org.codice.ddf.security.handler.BaseAuthenticationToken) AuthenticationHandler(org.codice.ddf.security.handler.api.AuthenticationHandler) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) PrincipalHolder(ddf.security.common.PrincipalHolder) Test(org.junit.Test)

Example 4 with ContextPolicyManager

use of org.codice.ddf.security.policy.context.ContextPolicyManager in project ddf by codice.

the class AuthorizationFilterTest method testNoSubject.

@Test
public void testNoSubject() {
    ContextPolicyManager contextPolicyManager = new TestPolicyManager();
    contextPolicyManager.setContextPolicy(PATH, getMockContextPolicy());
    AuthorizationFilter loginFilter = new AuthorizationFilter(contextPolicyManager);
    loginFilter.setSecurityLogger(mock(SecurityLogger.class));
    loginFilter.init();
    HttpServletRequest servletRequest = getMockServletRequest();
    HttpServletResponse servletResponse = mock(HttpServletResponse.class);
    SecurityFilterChain filterChain = (request, response) -> fail("Should not have called doFilter without a valid Subject");
    try {
        loginFilter.doFilter(servletRequest, servletResponse, filterChain);
    } catch (IOException | AuthenticationException e) {
        fail(e.getMessage());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ContextPolicy(org.codice.ddf.security.policy.context.ContextPolicy) KeyValuePermissionImpl(ddf.security.permission.impl.KeyValuePermissionImpl) HashMap(java.util.HashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityConstants(ddf.security.SecurityConstants) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) Before(org.junit.Before) SecurityLogger(ddf.security.audit.SecurityLogger) CollectionPermission(ddf.security.permission.CollectionPermission) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) Subject(ddf.security.Subject) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) CollectionPermissionImpl(ddf.security.permission.impl.CollectionPermissionImpl) ThreadContext(org.apache.shiro.util.ThreadContext) AuthenticationException(org.codice.ddf.platform.filter.AuthenticationException) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) AuthenticationException(org.codice.ddf.platform.filter.AuthenticationException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) SecurityLogger(ddf.security.audit.SecurityLogger) Test(org.junit.Test)

Example 5 with ContextPolicyManager

use of org.codice.ddf.security.policy.context.ContextPolicyManager in project ddf by codice.

the class AuthorizationFilterTest method testUnAuthorizedSubject.

@Test
public void testUnAuthorizedSubject() {
    ContextPolicyManager contextPolicyManager = new TestPolicyManager();
    contextPolicyManager.setContextPolicy(PATH, getMockContextPolicy());
    AuthorizationFilter loginFilter = new AuthorizationFilter(contextPolicyManager);
    loginFilter.setSecurityLogger(mock(SecurityLogger.class));
    loginFilter.init();
    Subject subject = mock(Subject.class);
    when(subject.isPermitted(any(CollectionPermission.class))).thenReturn(false);
    ThreadContext.bind(subject);
    HttpServletRequest servletRequest = getMockServletRequest();
    HttpServletResponse servletResponse = mock(HttpServletResponse.class);
    SecurityFilterChain filterChain = (request, response) -> fail("Should not have called doFilter without a valid Subject");
    try {
        loginFilter.doFilter(servletRequest, servletResponse, filterChain);
    } catch (IOException | AuthenticationException e) {
        fail(e.getMessage());
    }
    ThreadContext.unbindSubject();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ContextPolicy(org.codice.ddf.security.policy.context.ContextPolicy) KeyValuePermissionImpl(ddf.security.permission.impl.KeyValuePermissionImpl) HashMap(java.util.HashMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityConstants(ddf.security.SecurityConstants) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) Before(org.junit.Before) SecurityLogger(ddf.security.audit.SecurityLogger) CollectionPermission(ddf.security.permission.CollectionPermission) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) Collection(java.util.Collection) HttpServletResponse(javax.servlet.http.HttpServletResponse) Subject(ddf.security.Subject) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) CollectionPermissionImpl(ddf.security.permission.impl.CollectionPermissionImpl) ThreadContext(org.apache.shiro.util.ThreadContext) AuthenticationException(org.codice.ddf.platform.filter.AuthenticationException) Collections(java.util.Collections) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) AuthenticationException(org.codice.ddf.platform.filter.AuthenticationException) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Subject(ddf.security.Subject) ContextPolicyManager(org.codice.ddf.security.policy.context.ContextPolicyManager) HttpServletRequest(javax.servlet.http.HttpServletRequest) SecurityFilterChain(org.codice.ddf.platform.filter.SecurityFilterChain) CollectionPermission(ddf.security.permission.CollectionPermission) SecurityLogger(ddf.security.audit.SecurityLogger) Test(org.junit.Test)

Aggregations

ContextPolicyManager (org.codice.ddf.security.policy.context.ContextPolicyManager)16 ContextPolicy (org.codice.ddf.security.policy.context.ContextPolicy)13 Test (org.junit.Test)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 SecurityFilterChain (org.codice.ddf.platform.filter.SecurityFilterChain)11 SecurityLogger (ddf.security.audit.SecurityLogger)6 AuthenticationException (org.codice.ddf.platform.filter.AuthenticationException)6 AuthenticationHandler (org.codice.ddf.security.handler.api.AuthenticationHandler)6 HandlerResult (org.codice.ddf.security.handler.api.HandlerResult)6 Collection (java.util.Collection)5 Map (java.util.Map)5 ServletRequest (javax.servlet.ServletRequest)5 ServletResponse (javax.servlet.ServletResponse)5 SecurityConstants (ddf.security.SecurityConstants)4 Subject (ddf.security.Subject)4 CollectionPermission (ddf.security.permission.CollectionPermission)4 CollectionPermissionImpl (ddf.security.permission.impl.CollectionPermissionImpl)4 KeyValuePermissionImpl (ddf.security.permission.impl.KeyValuePermissionImpl)4 IOException (java.io.IOException)4