Search in sources :

Example 6 with ViewState

use of org.springframework.webflow.engine.ViewState in project cas by apereo.

the class AbstractCasWebflowConfigurer method createViewState.

@Override
public ViewState createViewState(final Flow flow, final String id, final Expression expression, final BinderConfiguration binder) {
    try {
        if (containsFlowState(flow, id)) {
            LOGGER.debug("Flow [{}] already contains a definition for state id [{}]", flow.getId(), id);
            return getTransitionableState(flow, id, ViewState.class);
        }
        final ViewFactory viewFactory = this.flowBuilderServices.getViewFactoryCreator().createViewFactory(expression, this.flowBuilderServices.getExpressionParser(), this.flowBuilderServices.getConversionService(), binder, this.flowBuilderServices.getValidator(), this.flowBuilderServices.getValidationHintResolver());
        final ViewState viewState = new ViewState(flow, id, viewFactory);
        LOGGER.debug("Added view state [{}]", viewState.getId());
        return viewState;
    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
    }
    return null;
}
Also used : ViewFactory(org.springframework.webflow.execution.ViewFactory) ActionExecutingViewFactory(org.springframework.webflow.engine.support.ActionExecutingViewFactory) ViewState(org.springframework.webflow.engine.ViewState)

Example 7 with ViewState

use of org.springframework.webflow.engine.ViewState in project cas by apereo.

the class AbstractMultifactorTrustedDeviceWebflowConfigurer method registerMultifactorTrustedAuthentication.

/**
 * Register multifactor trusted authentication into webflow.
 *
 * @param flowDefinitionRegistry the flow definition registry
 */
protected void registerMultifactorTrustedAuthentication(final FlowDefinitionRegistry flowDefinitionRegistry) {
    validateFlowDefinitionConfiguration(flowDefinitionRegistry);
    LOGGER.debug("Flow definitions found in the registry are [{}]", (Object[]) flowDefinitionRegistry.getFlowDefinitionIds());
    final String flowId = Arrays.stream(flowDefinitionRegistry.getFlowDefinitionIds()).findFirst().get();
    LOGGER.debug("Processing flow definition [{}]", flowId);
    final Flow flow = (Flow) flowDefinitionRegistry.getFlowDefinition(flowId);
    // Set the verify action
    final ActionState state = getState(flow, CasWebflowConstants.STATE_ID_INIT_LOGIN_FORM, ActionState.class);
    final Transition transition = (Transition) state.getTransition(CasWebflowConstants.TRANSITION_ID_SUCCESS);
    final String targetStateId = transition.getTargetStateId();
    transition.setTargetStateResolver(new DefaultTargetStateResolver(CasWebflowConstants.STATE_ID_VERIFY_TRUSTED_DEVICE));
    final ActionState verifyAction = createActionState(flow, CasWebflowConstants.STATE_ID_VERIFY_TRUSTED_DEVICE, createEvaluateAction(MFA_VERIFY_TRUST_ACTION_BEAN_ID));
    // handle device registration
    if (enableDeviceRegistration) {
        createTransitionForState(verifyAction, CasWebflowConstants.TRANSITION_ID_YES, CasWebflowConstants.STATE_ID_FINISH_MFA_TRUSTED_AUTH);
    } else {
        createTransitionForState(verifyAction, CasWebflowConstants.TRANSITION_ID_YES, CasWebflowConstants.STATE_ID_REAL_SUBMIT);
    }
    createTransitionForState(verifyAction, CasWebflowConstants.TRANSITION_ID_NO, targetStateId);
    createDecisionState(flow, CasWebflowConstants.DECISION_STATE_REQUIRE_REGISTRATION, isDeviceRegistrationRequired(), CasWebflowConstants.VIEW_ID_REGISTER_DEVICE, CasWebflowConstants.STATE_ID_REAL_SUBMIT);
    final ActionState submit = getState(flow, CasWebflowConstants.STATE_ID_REAL_SUBMIT, ActionState.class);
    final Transition success = (Transition) submit.getTransition(CasWebflowConstants.TRANSITION_ID_SUCCESS);
    if (enableDeviceRegistration) {
        success.setTargetStateResolver(new DefaultTargetStateResolver(CasWebflowConstants.VIEW_ID_REGISTER_DEVICE));
    } else {
        success.setTargetStateResolver(new DefaultTargetStateResolver(CasWebflowConstants.STATE_ID_REGISTER_TRUSTED_DEVICE));
    }
    final ViewState viewRegister = createViewState(flow, CasWebflowConstants.VIEW_ID_REGISTER_DEVICE, "casMfaRegisterDeviceView");
    final Transition viewRegisterTransition = createTransition(CasWebflowConstants.TRANSITION_ID_SUBMIT, CasWebflowConstants.STATE_ID_REGISTER_TRUSTED_DEVICE);
    viewRegister.getTransitionSet().add(viewRegisterTransition);
    final ActionState registerAction = createActionState(flow, CasWebflowConstants.STATE_ID_REGISTER_TRUSTED_DEVICE, createEvaluateAction(MFA_SET_TRUST_ACTION_BEAN_ID));
    createStateDefaultTransition(registerAction, CasWebflowConstants.STATE_ID_SUCCESS);
    if (submit.getActionList().size() == 0) {
        throw new IllegalArgumentException("There are no actions defined for the final submission event of " + flowId);
    }
    final Action act = submit.getActionList().iterator().next();
    final ActionState finishMfaTrustedAuth = createActionState(flow, CasWebflowConstants.STATE_ID_FINISH_MFA_TRUSTED_AUTH, act);
    final Transition finishedTransition = createTransition(CasWebflowConstants.TRANSITION_ID_SUCCESS, CasWebflowConstants.STATE_ID_SUCCESS);
    finishMfaTrustedAuth.getTransitionSet().add(finishedTransition);
    createStateDefaultTransition(finishMfaTrustedAuth, CasWebflowConstants.STATE_ID_SUCCESS);
}
Also used : Action(org.springframework.webflow.execution.Action) Transition(org.springframework.webflow.engine.Transition) DefaultTargetStateResolver(org.springframework.webflow.engine.support.DefaultTargetStateResolver) ViewState(org.springframework.webflow.engine.ViewState) ActionState(org.springframework.webflow.engine.ActionState) Flow(org.springframework.webflow.engine.Flow)

Example 8 with ViewState

use of org.springframework.webflow.engine.ViewState in project cas by apereo.

the class SpringWebflowEndpoint method getStateDetails.

private static Map<String, Object> getStateDetails(final Flow flowDefinition, final String st) {
    val state = (State) flowDefinition.getState(st);
    val stateMap = new LinkedHashMap<String, Object>();
    if (!state.getAttributes().asMap().isEmpty()) {
        stateMap.put("attributes", CollectionUtils.wrap(state.getAttributes()));
    }
    if (StringUtils.isNotBlank(state.getCaption())) {
        stateMap.put("caption", state.getCaption());
    }
    var acts = StreamSupport.stream(state.getEntryActionList().spliterator(), false).map(SpringWebflowEndpoint::convertActionToString).collect(Collectors.toList());
    if (!acts.isEmpty()) {
        stateMap.put("entryActions", acts);
    }
    if (state instanceof ActionState) {
        acts = StreamSupport.stream(((ActionState) state).getActionList().spliterator(), false).map(SpringWebflowEndpoint::convertActionToString).collect(Collectors.toList());
        if (!acts.isEmpty()) {
            stateMap.put("actionList", acts);
        }
    }
    if (state instanceof EndState) {
        stateMap.put("isEndState", Boolean.TRUE);
    }
    if (state.isViewState()) {
        val viewState = (ViewState) state;
        stateMap.put("isViewState", state.isViewState());
        stateMap.put("isRedirect", viewState.getRedirect());
        acts = StreamSupport.stream(state.getEntryActionList().spliterator(), false).map(Object::toString).collect(Collectors.toList());
        if (!acts.isEmpty()) {
            stateMap.put("renderActions", viewState.getRenderActionList());
        }
        acts = Arrays.stream(viewState.getVariables()).map(variable -> variable.getName() + " -> " + variable.getValueFactory().toString()).collect(Collectors.toList());
        if (!acts.isEmpty()) {
            stateMap.put("viewVariables", acts);
        }
        val field = ReflectionUtils.findField(viewState.getViewFactory().getClass(), "viewId");
        if (field != null) {
            ReflectionUtils.makeAccessible(field);
            val exp = (Expression) ReflectionUtils.getField(field, viewState.getViewFactory());
            stateMap.put("viewId", StringUtils.defaultIfBlank(Objects.requireNonNull(exp).getExpressionString(), exp.getValue(null).toString()));
        } else if (viewState.getViewFactory() instanceof ActionExecutingViewFactory) {
            val factory = (ActionExecutingViewFactory) viewState.getViewFactory();
            if (factory.getAction() instanceof ExternalRedirectAction) {
                val redirect = (ExternalRedirectAction) factory.getAction();
                val uri = ReflectionUtils.findField(redirect.getClass(), "resourceUri");
                ReflectionUtils.makeAccessible(Objects.requireNonNull(uri));
                val exp = (Expression) ReflectionUtils.getField(uri, redirect);
                stateMap.put("viewId", "externalRedirect -> #{" + Objects.requireNonNull(exp).getExpressionString() + '}');
            } else {
                stateMap.put("viewId", factory.getAction().toString());
            }
        } else {
            LOGGER.info("Field viewId cannot be located on view state [{}]", state);
        }
    }
    if (state instanceof TransitionableState) {
        val stDef = (TransitionableState) state;
        acts = StreamSupport.stream(stDef.getExitActionList().spliterator(), false).map(Object::toString).collect(Collectors.toList());
        if (!acts.isEmpty()) {
            stateMap.put("exitActions", acts);
        }
        acts = Arrays.stream(stDef.getTransitions()).map(tr -> tr.getId() + " -> " + tr.getTargetStateId()).collect(Collectors.toList());
        if (!acts.isEmpty()) {
            stateMap.put("transitions", acts);
        }
    }
    return stateMap;
}
Also used : lombok.val(lombok.val) ViewState(org.springframework.webflow.engine.ViewState) TransitionableState(org.springframework.webflow.engine.TransitionableState) ExternalRedirectAction(org.springframework.webflow.action.ExternalRedirectAction) LinkedHashMap(java.util.LinkedHashMap) Expression(org.springframework.binding.expression.Expression) ActionState(org.springframework.webflow.engine.ActionState) TransitionableState(org.springframework.webflow.engine.TransitionableState) State(org.springframework.webflow.engine.State) EndState(org.springframework.webflow.engine.EndState) ViewState(org.springframework.webflow.engine.ViewState) ActionExecutingViewFactory(org.springframework.webflow.engine.support.ActionExecutingViewFactory) ActionState(org.springframework.webflow.engine.ActionState) EndState(org.springframework.webflow.engine.EndState)

Example 9 with ViewState

use of org.springframework.webflow.engine.ViewState in project cas by apereo.

the class AccountManagementWebflowConfigurerTests method verifyOperation.

@Test
public void verifyOperation() {
    assertFalse(casWebflowExecutionPlan.getWebflowConfigurers().isEmpty());
    val flow = (Flow) this.loginFlowDefinitionRegistry.getFlowDefinition(CasWebflowConfigurer.FLOW_ID_LOGIN);
    assertNotNull(flow);
    assertTrue(flow.containsState(CasWebflowConstants.STATE_ID_VIEW_ACCOUNT_SIGNUP));
    assertTrue(flow.containsState(CasWebflowConstants.STATE_ID_SUBMIT_ACCOUNT_REGISTRATION));
    assertTrue(flow.containsState(CasWebflowConstants.STATE_ID_SENT_ACCOUNT_SIGNUP_INFO));
    assertTrue(flow.containsState(CasWebflowConstants.STATE_ID_ACCOUNT_REGISTRATION_SUBFLOW));
    val subflow = (SubflowState) flow.getState(CasWebflowConstants.STATE_ID_ACCOUNT_REGISTRATION_SUBFLOW);
    assertNotNull(subflow);
    val regFlow = (Flow) loginFlowDefinitionRegistry.getFlowDefinition(AccountManagementWebflowConfigurer.FLOW_ID_ACCOUNT_REGISTRATION);
    val context = new MockRequestControlContext(regFlow);
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val completeState = (ViewState) regFlow.getState(CasWebflowConstants.STATE_ID_COMPLETE_ACCOUNT_REGISTRATION);
    completeState.enter(context);
    assertNotNull(WebUtils.getPasswordPolicyPattern(context));
    assertEquals(2, AccountRegistrationUtils.getAccountRegistrationSecurityQuestionsCount(context));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) SubflowState(org.springframework.webflow.engine.SubflowState) ViewState(org.springframework.webflow.engine.ViewState) MockRequestControlContext(org.springframework.webflow.test.MockRequestControlContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext) Flow(org.springframework.webflow.engine.Flow) Test(org.junit.jupiter.api.Test)

Example 10 with ViewState

use of org.springframework.webflow.engine.ViewState in project cas by apereo.

the class PrepareMultifactorProviderSelectionActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    val flowSession = new MockFlowSession(new Flow(CasWebflowConfigurer.FLOW_ID_LOGIN));
    flowSession.setState(new ViewState(flowSession.getDefinitionInternal(), "viewState", mock(ViewFactory.class)));
    val exec = new MockFlowExecutionContext(flowSession);
    val context = new MockRequestContext(exec);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val chain = new DefaultChainingMultifactorAuthenticationProvider(new DefaultMultifactorAuthenticationFailureModeEvaluator(casProperties));
    val provider = new TestMultifactorAuthenticationProvider();
    provider.setBypassEvaluator(new DefaultChainingMultifactorAuthenticationBypassProvider());
    chain.addMultifactorAuthenticationProvider(provider);
    val attributes = new LocalAttributeMap(RegisteredService.class.getName(), RegisteredServiceTestUtils.getRegisteredService());
    attributes.put(MultifactorAuthenticationProvider.class.getName(), chain);
    val event = new EventFactorySupport().event(this, ChainingMultifactorAuthenticationProvider.DEFAULT_IDENTIFIER, attributes);
    context.setCurrentEvent(event);
    assertNull(action.execute(context));
    assertNotNull(WebUtils.getSelectableMultifactorAuthenticationProviders(context));
}
Also used : lombok.val(lombok.val) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) LocalAttributeMap(org.springframework.webflow.core.collection.LocalAttributeMap) RegisteredService(org.apereo.cas.services.RegisteredService) MockFlowSession(org.springframework.webflow.test.MockFlowSession) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ViewState(org.springframework.webflow.engine.ViewState) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MultifactorAuthenticationProvider(org.apereo.cas.authentication.MultifactorAuthenticationProvider) DefaultChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.DefaultChainingMultifactorAuthenticationProvider) TestMultifactorAuthenticationProvider(org.apereo.cas.authentication.mfa.TestMultifactorAuthenticationProvider) ChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.ChainingMultifactorAuthenticationProvider) MockServletContext(org.springframework.mock.web.MockServletContext) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) Flow(org.springframework.webflow.engine.Flow) DefaultMultifactorAuthenticationFailureModeEvaluator(org.apereo.cas.authentication.DefaultMultifactorAuthenticationFailureModeEvaluator) MockFlowExecutionContext(org.springframework.webflow.test.MockFlowExecutionContext) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) DefaultChainingMultifactorAuthenticationProvider(org.apereo.cas.authentication.DefaultChainingMultifactorAuthenticationProvider) DefaultChainingMultifactorAuthenticationBypassProvider(org.apereo.cas.authentication.bypass.DefaultChainingMultifactorAuthenticationBypassProvider) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

ViewState (org.springframework.webflow.engine.ViewState)20 Flow (org.springframework.webflow.engine.Flow)13 lombok.val (lombok.val)9 Test (org.junit.jupiter.api.Test)7 ActionState (org.springframework.webflow.engine.ActionState)7 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)6 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)6 MockServletContext (org.springframework.mock.web.MockServletContext)5 Transition (org.springframework.webflow.engine.Transition)4 Action (org.springframework.webflow.execution.Action)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 ActionExecutingViewFactory (org.springframework.webflow.engine.support.ActionExecutingViewFactory)3 ViewFactory (org.springframework.webflow.execution.ViewFactory)3 MockRequestContext (org.springframework.webflow.test.MockRequestContext)3 MockRequestControlContext (org.springframework.webflow.test.MockRequestControlContext)3 Expression (org.springframework.binding.expression.Expression)2 BinderConfiguration (org.springframework.webflow.engine.builder.BinderConfiguration)2 DefaultTargetStateResolver (org.springframework.webflow.engine.support.DefaultTargetStateResolver)2 MockFlowExecutionContext (org.springframework.webflow.test.MockFlowExecutionContext)2