Java Code Examples for org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext#isReturning()

The following examples show how to use org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext#isReturning() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultAuthenticationRequestHandler.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * Executes the authentication flow
 *
 * @param request
 * @param response
 * @throws FrameworkException
 */
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
                   AuthenticationContext context) throws FrameworkException {

    if (log.isDebugEnabled()) {
        log.debug("In authentication flow");
    }

    if (context.isReturning()) {
        // if "Deny" or "Cancel" pressed on the login page.
        if (request.getParameter(FrameworkConstants.RequestParams.DENY) != null) {
            handleDenyFromLoginPage(request, response, context);
            return;
        }

        // handle remember-me option from the login page
        handleRememberMeOptionFromLoginPage(request, context);
    }

    int currentStep = context.getCurrentStep();

    // if this is the start of the authentication flow
    if (currentStep == 0) {
        handleSequenceStart(request, response, context);
    }

    SequenceConfig seqConfig = context.getSequenceConfig();
    List<AuthenticatorConfig> reqPathAuthenticators = seqConfig.getReqPathAuthenticators();

    try {
        UserStorePreferenceOrderSupplier<List<String>> userStorePreferenceOrderSupplier =
                FrameworkUtils.getUserStorePreferenceOrderSupplier(context, null);
        if (userStorePreferenceOrderSupplier != null) {
            // Add the user store preference supplier to the container UserMgtContext.
            UserMgtContext userMgtContext = new UserMgtContext();
            userMgtContext.setUserStorePreferenceOrderSupplier(userStorePreferenceOrderSupplier);
            UserCoreUtil.setUserMgtContextInThreadLocal(userMgtContext);
        }

        // if SP has request path authenticators configured and this is start of
        // the flow
        if (reqPathAuthenticators != null && !reqPathAuthenticators.isEmpty() && currentStep == 0) {
            // call request path sequence handler
            FrameworkUtils.getRequestPathBasedSequenceHandler().handle(request, response, context);
        }

        // if no request path authenticators or handler returned cannot handle
        if (!context.getSequenceConfig().isCompleted()
                || (reqPathAuthenticators == null || reqPathAuthenticators.isEmpty())) {
            // To keep track of whether particular request goes through the step based sequence handler.
            context.setProperty(FrameworkConstants.STEP_BASED_SEQUENCE_HANDLER_TRIGGERED, true);

            // call step based sequence handler
            FrameworkUtils.getStepBasedSequenceHandler().handle(request, response, context);
        }
    } finally {
        UserCoreUtil.removeUserMgtContextInThreadLocal();
    }

    // handle post authentication
    handlePostAuthentication(request, response, context);
    // if flow completed, send response back
    if (canConcludeFlow(context)) {
        concludeFlow(request, response, context);
    }
}
 
Example 2
Source File: DefaultRequestCoordinator.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(HttpServletRequest request, HttpServletResponse response) throws IOException {

    try {
        AuthenticationContext context;
        AuthenticationRequestCacheEntry authRequest = null;
        String sessionDataKey = request.getParameter("sessionDataKey");

        boolean returning = false;
        // Check whether this is the start of the authentication flow.
        // 'type' parameter should be present if so. This parameter contains
        // the request type (e.g. samlsso) set by the calling servlet.
        // TODO: use a different mechanism to determine the flow start.
        if (request.getParameter("type") != null) {
            // Retrieve AuthenticationRequestCache Entry which is stored stored from servlet.
            if (sessionDataKey != null) {
                if (log.isDebugEnabled()) {
                    log.debug("retrieving authentication request from cache..");
                }

                authRequest = getAuthenticationRequest(request, sessionDataKey);

                if (authRequest == null) {
                    // authRequest cannot be retrieved from cache. Cache
                    throw new FrameworkException("Invalid authentication request. Session data key : " + sessionDataKey);
                }

            } else if (!Boolean.parseBoolean(request.getParameter(FrameworkConstants.LOGOUT))) {

                // sessionDataKey is null and not a logout request
                if (log.isDebugEnabled()) {
                    log.debug("Session data key is null in the request and not a logout request.");
                }

                FrameworkUtils.sendToRetryPage(request, response);
            }

            // if there is a cache entry, wrap the original request with params in cache entry
            if (authRequest != null) {
                request = FrameworkUtils.getCommonAuthReqWithParams(request, authRequest);
                FrameworkUtils.removeAuthenticationRequestFromCache(sessionDataKey);
            }
            context = initializeFlow(request, response);
        } else {
            returning = true;
            context = FrameworkUtils.getContextData(request);
        }

        if (context != null) {
            context.setReturning(returning);

            // if this is the flow start, store the original request in the context
            if (!context.isReturning() && authRequest != null) {
                context.setAuthenticationRequest(authRequest.getAuthenticationRequest());
            }


            if (!context.isLogoutRequest()) {
                FrameworkUtils.getAuthenticationRequestHandler().handle(request, response,
                                                                        context);
            } else {
                FrameworkUtils.getLogoutRequestHandler().handle(request, response, context);
            }
        } else {
            if (log.isDebugEnabled()) {
                String key = request.getParameter("sessionDataKey");
                if (key == null) {
                    log.debug("Session data key is null in the request");
                } else {
                    log.debug("Session data key  :  " + key);
                }
            }
            log.error("Context does not exist. Probably due to invalidated cache");
            FrameworkUtils.sendToRetryPage(request, response);
        }
    } catch (Throwable e) {
        log.error("Exception in Authentication Framework", e);
        FrameworkUtils.sendToRetryPage(request, response);
    }
}
 
Example 3
Source File: DefaultAuthenticationRequestHandler.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Executes the authentication flow
 *
 * @param request
 * @param response
 * @throws FrameworkException
 */
@Override
public void handle(HttpServletRequest request, HttpServletResponse response,
                   AuthenticationContext context) throws FrameworkException {

    if (log.isDebugEnabled()) {
        log.debug("In authentication flow");
    }

    if (context.isReturning()) {
        // if "Deny" or "Cancel" pressed on the login page.
        if (request.getParameter(FrameworkConstants.RequestParams.DENY) != null) {
            handleDenyFromLoginPage(request, response, context);
            return;
        }

        // handle remember-me option from the login page
        handleRememberMeOptionFromLoginPage(request, context);
    }

    int currentStep = context.getCurrentStep();

    // if this is the start of the authentication flow
    if (currentStep == 0) {
        handleSequenceStart(request, response, context);
    }

    SequenceConfig seqConfig = context.getSequenceConfig();
    List<AuthenticatorConfig> reqPathAuthenticators = seqConfig.getReqPathAuthenticators();

    // if SP has request path authenticators configured and this is start of
    // the flow
    if (reqPathAuthenticators != null && !reqPathAuthenticators.isEmpty() && currentStep == 0) {
        // call request path sequence handler
        FrameworkUtils.getRequestPathBasedSequenceHandler().handle(request, response, context);
    }

    // if no request path authenticators or handler returned cannot handle
    if (!context.getSequenceConfig().isCompleted()
        || (reqPathAuthenticators == null || reqPathAuthenticators.isEmpty())) {
        // call step based sequence handler
        FrameworkUtils.getStepBasedSequenceHandler().handle(request, response, context);
    }

    // if flow completed, send response back
    if (context.getSequenceConfig().isCompleted()) {
        concludeFlow(request, response, context);
    } else { // redirecting outside
        FrameworkUtils.addAuthenticationContextToCache(context.getContextIdentifier(), context);
    }
}