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.");
}
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);
}
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);
}
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);
}
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);
}
Aggregations