Search in sources :

Example 6 with PrincipalHolder

use of ddf.security.common.PrincipalHolder in project ddf by codice.

the class SessionManagementServiceImpl method getExpiry.

@Override
public String getExpiry(HttpServletRequest request) {
    long timeLeft = 0;
    if (sessionFactory == null) {
        return Long.toString(timeLeft);
    }
    HttpSession session = sessionFactory.getOrCreateSession(request);
    if (session == null) {
        return Long.toString(timeLeft);
    }
    Object principalHolder = session.getAttribute(SecurityConstants.SECURITY_TOKEN_KEY);
    if (!(principalHolder instanceof PrincipalHolder)) {
        return Long.toString(timeLeft);
    }
    timeLeft = session.getMaxInactiveInterval() * 1000L;
    return Long.toString(timeLeft);
}
Also used : HttpSession(javax.servlet.http.HttpSession) PrincipalHolder(ddf.security.common.PrincipalHolder)

Example 7 with PrincipalHolder

use of ddf.security.common.PrincipalHolder in project ddf by codice.

the class WebSSOFilter method checkForPreviousResultOnSession.

private HandlerResult checkForPreviousResultOnSession(HttpServletRequest httpRequest, String ip) {
    String requestedSessionId = httpRequest.getRequestedSessionId();
    if (requestedSessionId == null) {
        LOGGER.trace("No HTTP Session - returning with no results");
        return null;
    }
    HttpSession session = httpRequest.getSession(false);
    if (session == null) {
        // has not yet been created for them.
        if (sessionFactory == null) {
            throw new SessionException("Unable to verify user's session.");
        }
        session = sessionFactory.getOrCreateSession(httpRequest);
    }
    // See if principals exist for the requested session id
    HandlerResult result = null;
    PrincipalHolder principalHolder = (PrincipalHolder) session.getAttribute(SecurityConstants.SECURITY_TOKEN_KEY);
    if (principalHolder != null && principalHolder.getPrincipals() != null) {
        Collection<SecurityAssertion> assertions = principalHolder.getPrincipals().byType(SecurityAssertion.class);
        SessionToken sessionToken = null;
        if (!assertions.isEmpty()) {
            sessionToken = new SessionToken(principalHolder.getPrincipals(), session.getId(), ip);
        }
        if (sessionToken != null) {
            result = new HandlerResultImpl();
            result.setToken(sessionToken);
            result.setStatus(HandlerResult.Status.COMPLETED);
        } else {
            principalHolder.remove();
        }
    } else {
        securityLogger.audit("Request contained invalid or expired session id [{}]", Hashing.sha256().hashString(requestedSessionId, StandardCharsets.UTF_8).toString());
        LOGGER.trace("Request contained invalid or expired session - returning with no results");
    }
    return result;
}
Also used : SessionToken(org.codice.ddf.security.handler.SessionToken) HttpSession(javax.servlet.http.HttpSession) HandlerResultImpl(org.codice.ddf.security.handler.HandlerResultImpl) SessionException(org.apache.shiro.session.SessionException) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) SecurityAssertion(ddf.security.assertion.SecurityAssertion) PrincipalHolder(ddf.security.common.PrincipalHolder)

Example 8 with PrincipalHolder

use of ddf.security.common.PrincipalHolder in project ddf by codice.

the class OidcLogoutActionProviderTest method setup.

@Before
public void setup() {
    oidcLogoutActionBuilder = mock(OidcLogoutActionBuilder.class);
    OidcHandlerConfiguration handlerConfiguration = mock(OidcHandlerConfiguration.class);
    when(handlerConfiguration.getOidcLogoutActionBuilder()).thenReturn(oidcLogoutActionBuilder);
    oidcLogoutActionProvider = new OidcLogoutActionProvider(handlerConfiguration);
    oidcLogoutActionProvider.setSubjectOperations(new SubjectUtils());
    request = mock(HttpServletRequest.class);
    response = mock(HttpServletResponse.class);
    subject = mock(Subject.class);
    HttpSession session = mock(HttpSession.class);
    PrincipalHolder principalHolderMock = mock(PrincipalHolder.class);
    SimplePrincipalCollection principalCollection = new SimplePrincipalCollection();
    SecurityAssertion securityAssertion = mock(SecurityAssertion.class);
    OidcProfile profile = mock(OidcProfile.class);
    when(securityAssertion.getToken()).thenReturn(profile);
    when(securityAssertion.getTokenType()).thenReturn(SecurityAssertionJwt.JWT_TOKEN_TYPE);
    when(subject.getPrincipals()).thenReturn(principalCollection);
    when(principalHolderMock.getPrincipals()).thenReturn(principalCollection);
    principalCollection.add(securityAssertion, "oidc");
    when(session.getAttribute(SecurityConstants.SECURITY_TOKEN_KEY)).thenReturn(principalHolderMock);
    when(request.getSession(false)).thenReturn(session);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) OidcHandlerConfiguration(org.codice.ddf.security.handler.api.OidcHandlerConfiguration) SubjectUtils(ddf.security.service.impl.SubjectUtils) OidcLogoutActionBuilder(org.pac4j.oidc.logout.OidcLogoutActionBuilder) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) SimplePrincipalCollection(org.apache.shiro.subject.SimplePrincipalCollection) OidcProfile(org.pac4j.oidc.profile.OidcProfile) SecurityAssertion(ddf.security.assertion.SecurityAssertion) PrincipalHolder(ddf.security.common.PrincipalHolder) Subject(ddf.security.Subject) Before(org.junit.Before)

Example 9 with PrincipalHolder

use of ddf.security.common.PrincipalHolder in project ddf by codice.

the class LoginFilterTest method testValidReference.

@Test
public void testValidReference() throws Exception {
    HandlerResult result = new HandlerResultImpl(HandlerResult.Status.COMPLETED, referenceTokenMock);
    when(requestMock.getAttribute(AUTHENTICATION_TOKEN_KEY)).thenReturn(result);
    PrincipalHolder principalHolder = new PrincipalHolder();
    principalHolder.setPrincipals(principalCollectionMock);
    when(sessionMock.getAttribute(SECURITY_TOKEN_KEY)).thenReturn(principalHolder);
    when(securityManagerMock.getSubject(referenceTokenMock)).thenReturn(subject);
    loginFilter.doFilter(requestMock, responseMock, filterChainMock);
    verify(filterChainMock, times(1)).doFilter(any(), any());
}
Also used : HandlerResultImpl(org.codice.ddf.security.handler.HandlerResultImpl) HandlerResult(org.codice.ddf.security.handler.api.HandlerResult) PrincipalHolder(ddf.security.common.PrincipalHolder) Test(org.junit.Test)

Example 10 with PrincipalHolder

use of ddf.security.common.PrincipalHolder 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)

Aggregations

PrincipalHolder (ddf.security.common.PrincipalHolder)17 HttpSession (javax.servlet.http.HttpSession)12 Test (org.junit.Test)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 SecurityAssertion (ddf.security.assertion.SecurityAssertion)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 PrincipalCollection (org.apache.shiro.subject.PrincipalCollection)4 SimplePrincipalCollection (org.apache.shiro.subject.SimplePrincipalCollection)4 HandlerResult (org.codice.ddf.security.handler.api.HandlerResult)4 Before (org.junit.Before)3 SessionFactory (ddf.security.http.SessionFactory)2 X509Certificate (java.security.cert.X509Certificate)2 ServletRequest (javax.servlet.ServletRequest)2 ServletResponse (javax.servlet.ServletResponse)2 SecurityFilterChain (org.codice.ddf.platform.filter.SecurityFilterChain)2 BaseAuthenticationToken (org.codice.ddf.security.handler.BaseAuthenticationToken)2 HandlerResultImpl (org.codice.ddf.security.handler.HandlerResultImpl)2 SessionToken (org.codice.ddf.security.handler.SessionToken)2 AuthenticationHandler (org.codice.ddf.security.handler.api.AuthenticationHandler)2 ContextPolicy (org.codice.ddf.security.policy.context.ContextPolicy)2