Search in sources :

Example 31 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class SessionExtenderProcessorTest method testProcessWithSessionKey.

@Test(expectedExceptions = NullPointerException.class)
public void testProcessWithSessionKey() throws Exception {
    mockStatic(SessionContextCache.class);
    SessionExtenderRequest sessionExtenderRequest = mock(SessionExtenderRequest.class);
    SessionContextCache sessionContextCache = mock(SessionContextCache.class);
    SessionContextCacheKey sessionContextCacheKey = mock(SessionContextCacheKey.class);
    SessionContextCacheEntry sessionContextCacheEntry = mock(SessionContextCacheEntry.class);
    SessionContext sessionContext = mock(SessionContext.class);
    whenNew(SessionContextCacheKey.class).withArguments(anyString()).thenReturn(sessionContextCacheKey);
    when(sessionExtenderRequest.getTenantDomain()).thenReturn(TENANT_DOMAIN);
    when(sessionExtenderRequest.getSessionKey()).thenReturn(IDP_SESSION_KEY);
    when(SessionContextCache.getInstance()).thenReturn(sessionContextCache);
    when(sessionContextCache.getSessionContextCacheEntry(anyObject(), anyString())).thenReturn(sessionContextCacheEntry);
    when(sessionContextCacheEntry.getContext()).thenReturn(sessionContext);
    SessionExtenderResponse.SessionExtenderResponseBuilder responseBuilder = (SessionExtenderResponse.SessionExtenderResponseBuilder) sessionExtenderProcessor.process(sessionExtenderRequest);
    SessionExtenderResponse response = responseBuilder.build();
    assertNotNull(response.getTraceId(), "Error creating successful response.");
}
Also used : SessionExtenderResponse(org.wso2.carbon.identity.application.authentication.framework.session.extender.response.SessionExtenderResponse) SessionContextCacheEntry(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheEntry) SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) SessionContextCacheKey(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheKey) SessionContextCache(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCache) SessionExtenderRequest(org.wso2.carbon.identity.application.authentication.framework.session.extender.request.SessionExtenderRequest) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 32 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class FrameworkUtilsTest method testGetSessionContextFromCacheNullCacheEntry.

@Test
public void testGetSessionContextFromCacheNullCacheEntry() {
    setMockedSessionContextCache();
    SessionContext sessionContext = FrameworkUtils.getSessionContextFromCache(DUMMY_CACHE_KEY, SUPER_TENANT_DOMAIN_NAME);
    assertNull(sessionContext);
}
Also used : SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 33 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class FrameworkUtilsTest method testGetSessionContextFromCacheNotExpiredSession.

@Test
public void testGetSessionContextFromCacheNotExpiredSession() throws FrameworkException {
    cacheEntry.setContext(context);
    setMockedSessionContextCache();
    when(mockedSessionContextCache.getSessionContextCacheEntry(cacheKey, "abc")).thenReturn(cacheEntry);
    when(mockedSessionContextCache.isSessionExpired(any(SessionContextCacheKey.class), any(SessionContextCacheEntry.class))).thenReturn(false);
    SessionContext sessionContext = FrameworkUtils.getSessionContextFromCache(request, authenticationContext, DUMMY_CACHE_KEY);
    assertEquals(sessionContext, context);
}
Also used : SessionContextCacheEntry(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheEntry) SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) SessionContextCacheKey(org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheKey) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 34 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class DefaultAuthenticationRequestHandler method handleInboundSessionCreate.

private void handleInboundSessionCreate(String requestType, String sessionContextKey, SessionContext sessionContext, HttpServletRequest request, HttpServletResponse response, AuthenticationContext context) {
    SessionContextMgtListener sessionContextMgtListener = FrameworkServiceDataHolder.getInstance().getSessionContextMgtListener(requestType);
    if (sessionContextMgtListener == null) {
        return;
    }
    Map<String, String> inboundProperties = sessionContextMgtListener.onPreCreateSession(sessionContextKey, request, response, context);
    inboundProperties.forEach(sessionContext::addProperty);
}
Also used : SessionContextMgtListener(org.wso2.carbon.identity.application.authentication.framework.listener.SessionContextMgtListener)

Example 35 with SessionContext

use of org.wso2.carbon.identity.application.authentication.framework.context.SessionContext in project carbon-identity-framework by wso2.

the class DefaultRequestCoordinator method findPreviousAuthenticatedSession.

protected void findPreviousAuthenticatedSession(HttpServletRequest request, AuthenticationContext context) throws FrameworkException {
    List<String> acrRequested = getAcrRequested(request);
    if (acrRequested != null) {
        for (String acr : acrRequested) {
            context.addRequestedAcr(acr);
        }
    }
    // Get service provider chain
    SequenceConfig effectiveSequence = getSequenceConfig(context, request.getParameterMap());
    if (acrRequested != null) {
        for (String acr : acrRequested) {
            effectiveSequence.addRequestedAcr(acr);
        }
    }
    Cookie cookie = FrameworkUtils.getAuthCookie(request);
    // if cookie exists user has previously authenticated
    if (cookie != null) {
        if (log.isDebugEnabled()) {
            log.debug(FrameworkConstants.COMMONAUTH_COOKIE + " cookie is available with the value: " + cookie.getValue());
        }
        String sessionContextKey = DigestUtils.sha256Hex(cookie.getValue());
        SessionContext sessionContext = null;
        // get the authentication details from the cache
        try {
            // Starting tenant-flow as tenant domain is retrieved downstream from the carbon-context to get the
            // tenant wise session expiry time
            FrameworkUtils.startTenantFlow(context.getTenantDomain());
            sessionContext = FrameworkUtils.getSessionContextFromCache(request, context, sessionContextKey);
        } finally {
            FrameworkUtils.endTenantFlow();
        }
        if (sessionContext != null) {
            context.setSessionIdentifier(sessionContextKey);
            String appName = effectiveSequence.getApplicationConfig().getApplicationName();
            if (log.isDebugEnabled()) {
                log.debug("Service Provider is: " + appName);
            }
            SequenceConfig previousAuthenticatedSeq = sessionContext.getAuthenticatedSequences().get(appName);
            if (previousAuthenticatedSeq != null) {
                if (log.isDebugEnabled()) {
                    log.debug("A previously authenticated sequence found for the SP: " + appName);
                }
                context.setPreviousSessionFound(true);
                effectiveSequence.setStepMap(new HashMap<>(previousAuthenticatedSeq.getStepMap()));
                effectiveSequence.setReqPathAuthenticators(new ArrayList<>(previousAuthenticatedSeq.getReqPathAuthenticators()));
                effectiveSequence.setAuthenticatedUser(previousAuthenticatedSeq.getAuthenticatedUser());
                effectiveSequence.setAuthenticatedIdPs(previousAuthenticatedSeq.getAuthenticatedIdPs());
                effectiveSequence.setAuthenticatedReqPathAuthenticator(previousAuthenticatedSeq.getAuthenticatedReqPathAuthenticator());
                AuthenticatedUser authenticatedUser = previousAuthenticatedSeq.getAuthenticatedUser();
                if (authenticatedUser != null) {
                    if (isUserAllowedToLogin(authenticatedUser)) {
                        String authenticatedUserTenantDomain = authenticatedUser.getTenantDomain();
                        // set the user for the current authentication/logout flow
                        context.setSubject(authenticatedUser);
                        if (log.isDebugEnabled()) {
                            log.debug("Already authenticated by username: " + authenticatedUser.getAuthenticatedSubjectIdentifier());
                        }
                        if (authenticatedUserTenantDomain != null) {
                            // set the user tenant domain for the current authentication/logout flow
                            context.setProperty(USER_TENANT_DOMAIN, authenticatedUserTenantDomain);
                            if (log.isDebugEnabled()) {
                                log.debug("Authenticated user tenant domain: " + authenticatedUserTenantDomain);
                            }
                        }
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug(String.format("User %s is not allowed to authenticate from previous session.", authenticatedUser.toString()));
                        }
                        context.setPreviousSessionFound(false);
                        FrameworkUtils.removeSessionContextFromCache(sessionContextKey, context.getLoginTenantDomain());
                        sessionContext.setAuthenticatedIdPs(new HashMap<String, AuthenticatedIdPData>());
                    }
                }
                // This is done to reflect the changes done in SP to the sequence config. So, the requested claim
                // updates, authentication step updates will be reflected.
                refreshAppConfig(effectiveSequence, request.getParameter(FrameworkConstants.RequestParams.ISSUER), context.getRequestType(), context.getTenantDomain());
                context.setAuthenticatedIdPsOfApp(sessionContext.getAuthenticatedIdPsOfApp(appName));
            }
            context.setPreviousAuthenticatedIdPs(sessionContext.getAuthenticatedIdPs());
            context.setProperty(FrameworkConstants.RUNTIME_CLAIMS, sessionContext.getProperty(FrameworkConstants.RUNTIME_CLAIMS));
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Failed to find the SessionContext from the cache. Possible cache timeout.");
            }
        }
    }
    context.setServiceProviderName(effectiveSequence.getApplicationConfig().getApplicationName());
    // set the sequence for the current authentication/logout flow
    context.setSequenceConfig(effectiveSequence);
}
Also used : Cookie(javax.servlet.http.Cookie) SessionContext(org.wso2.carbon.identity.application.authentication.framework.context.SessionContext) SequenceConfig(org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) AuthenticatedIdPData(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedIdPData)

Aggregations

SessionContext (org.wso2.carbon.identity.application.authentication.framework.context.SessionContext)25 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)14 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 Test (org.testng.annotations.Test)7 SessionContextCacheKey (org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheKey)7 HashMap (java.util.HashMap)6 BeforeTest (org.testng.annotations.BeforeTest)6 SessionContextCacheEntry (org.wso2.carbon.identity.application.authentication.framework.cache.SessionContextCacheEntry)6 SequenceConfig (org.wso2.carbon.identity.application.authentication.framework.config.model.SequenceConfig)6 Event (org.wso2.carbon.identity.event.event.Event)6 JSONObject (org.json.JSONObject)5 AuthenticationDataPublisher (org.wso2.carbon.identity.application.authentication.framework.AuthenticationDataPublisher)4 FrameworkException (org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException)4 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)4 ArrayList (java.util.ArrayList)3 Cookie (javax.servlet.http.Cookie)3 AuthHistory (org.wso2.carbon.identity.application.authentication.framework.context.AuthHistory)3 AuthenticationContext (org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext)3 Map (java.util.Map)2 AfterTest (org.testng.annotations.AfterTest)2