package org.apereo.cas.web.flow;

import org.springframework.webflow.definition.registry.FlowDefinitionRegistry;
import org.springframework.webflow.engine.ActionState;
import org.springframework.webflow.engine.Flow;
import org.springframework.webflow.engine.builder.support.FlowBuilderServices;

/**
 * The {@link SpengoWebflowConfigurer} is responsible for
 * adjusting the CAS webflow context for spnego integration.
 *
 * @author Misagh Moayyed
 * @since 4.2
 */
public class SpengoWebflowConfigurer extends AbstractCasWebflowConfigurer {

    private static final String SPNEGO = "spnego";
    private static final String START_SPNEGO_AUTHENTICATE = "startSpnegoAuthenticate";
    private static final String SPNEGO_NEGOTIATE = "negociateSpnego";
    private static final String EVALUATE_SPNEGO_CLIENT = "evaluateClientRequest";

    public SpengoWebflowConfigurer(final FlowBuilderServices flowBuilderServices, final FlowDefinitionRegistry loginFlowDefinitionRegistry) {
        super(flowBuilderServices, loginFlowDefinitionRegistry);
    }

    @Override
    protected void doInitialize() throws Exception {
        final Flow flow = getLoginFlow();
        if (flow != null) {
            final ActionState actionState = createActionState(flow, START_SPNEGO_AUTHENTICATE, createEvaluateAction(SPNEGO_NEGOTIATE));
            actionState.getTransitionSet().add(createTransition(CasWebflowConstants.TRANSITION_ID_SUCCESS, SPNEGO));
            actionState.getTransitionSet().add(createTransition(CasWebflowConstants.TRANSITION_ID_ERROR,
                    CasWebflowConstants.STATE_ID_VIEW_LOGIN_FORM));

            final ActionState spnego = createActionState(flow, SPNEGO, createEvaluateAction(SPNEGO));
            spnego.getTransitionSet().add(createTransition(CasWebflowConstants.TRANSITION_ID_SUCCESS,
                    CasWebflowConstants.TRANSITION_ID_SEND_TICKET_GRANTING_TICKET));
            spnego.getTransitionSet().add(createTransition(CasWebflowConstants.TRANSITION_ID_ERROR,
                    CasWebflowConstants.STATE_ID_VIEW_LOGIN_FORM));
            spnego.getTransitionSet().add(createTransition(CasWebflowConstants.TRANSITION_ID_AUTHENTICATION_FAILURE,
                    CasWebflowConstants.STATE_ID_VIEW_LOGIN_FORM));
            spnego.getExitActionList().add(createEvaluateAction("clearWebflowCredentialsAction"));

            registerMultifactorProvidersStateTransitionsIntoWebflow(spnego);

            final ActionState evaluateClientRequest = createActionState(flow, EVALUATE_SPNEGO_CLIENT,
                    createEvaluateAction(casProperties.getAuthn().getSpnego().getHostNameClientActionStrategy()));
            evaluateClientRequest.getTransitionSet().add(createTransition(CasWebflowConstants.TRANSITION_ID_YES, START_SPNEGO_AUTHENTICATE));
            evaluateClientRequest.getTransitionSet().add(createTransition(CasWebflowConstants.TRANSITION_ID_NO, getStartState(flow)));
        }
    }
}

@@ -209,16 +209,73 @@
 e.Flow;%0A
+import org.springframework.webflow.engine.TransitionSet;%0A
 import o
@@ -1235,24 +1235,340 @@
 null) %7B%0A    
+        createStartSpnegoAction(flow);%0A            createEvaluateSpnegoClientAction(flow);%0A%0A            final ActionState spnego = createSpnegoActionState(flow);%0A            registerMultifactorProvidersStateTransitionsIntoWebflow(spnego);%0A        %7D%0A    %7D%0A%0A    private void createStartSpnegoAction(final Flow flow) %7B%0A
         fina
@@ -1687,36 +1687,32 @@
 IATE));%0A        
-    
 actionState.getT
@@ -1804,28 +1804,24 @@
 ));%0A        
-    
 actionState.
@@ -1900,36 +1900,16 @@
 D_ERROR,
-%0A                   
  CasWebf
@@ -1945,29 +1945,98 @@
 GIN_FORM));%0A
-%0A
     
+%7D%0A%0A    private ActionState createSpnegoActionState(final Flow flow) %7B%0A
         fina
@@ -2121,35 +2121,65 @@
 NEGO));%0A        
-   
+final TransitionSet transitions =
  spnego.getTrans
@@ -2180,32 +2180,53 @@
 tTransitionSet()
+;%0A        transitions
 .add(createTrans
@@ -2273,36 +2273,16 @@
 SUCCESS,
-%0A                   
  CasWebf
@@ -2343,39 +2343,25 @@
 ));%0A        
-    spnego.getT
+t
 ransitionSet
@@ -2349,37 +2349,33 @@
       transition
-Set()
+s
 .add(createTrans
@@ -2420,36 +2420,16 @@
 D_ERROR,
-%0A                   
  CasWebf
@@ -2481,23 +2481,9 @@
     
-    spnego.getT
+t
 rans
@@ -2479,37 +2479,33 @@
       transition
-Set()
+s
 .add(createTrans
@@ -2567,36 +2567,16 @@
 FAILURE,
-%0A                   
  CasWebf
@@ -2624,20 +2624,16 @@
         
-  
-  
 spnego.g
@@ -2711,25 +2711,24 @@
 ion%22));%0A
-%0A
         
     regi
@@ -2723,82 +2723,99 @@
     
-    registerMultifactorProvidersStateTransitionsIntoWebflow(spnego);%0A%0A    
+return spnego;%0A    %7D%0A%0A    private void createEvaluateSpnegoClientAction(final Flow flow) %7B%0A
     
@@ -2916,36 +2916,32 @@
                 
- 
-   
 createEvaluateAc
@@ -3015,28 +3015,24 @@
 rategy()));%0A
-    
         eval
@@ -3153,28 +3153,24 @@
 ENTICATE));%0A
-    
         eval
@@ -3288,26 +3288,16 @@
 low)));%0A
-        %7D%0A
     %7D%0A%7D%0A

Clean up spnego config