Execute Javascript when the login is successful

50 views
Skip to first unread message

Fernando Gomez

unread,
Aug 9, 2018, 1:36:54 PM8/9/18
to CAS Community

Hello, I have the following problem, in my implementation of CAS SSO, I can login for: Facebook, Google and Email, I need to do is that when the login is successful in any of the entry methods, I can run a script, i.e. If I login with Facebook and is a successful, I must then execute a script that takes statistics through Facebook, the same for google and the same with Email, my question is how can I, know when the login is successful, to do a condition that allows me to execute my script?

Ray Bon

unread,
Aug 9, 2018, 3:54:07 PM8/9/18
to cas-...@apereo.org
Fernando,

You could process the log files to know when a log in was successful.
Or are you interested in having javascript run on the user's browser that collects data about the service?

Ray

On Thu, 2018-08-09 at 10:36 -0700, Fernando Gomez wrote:

Hello, I have the following problem, in my implementation of CAS SSO, I can login for: Facebook, Google and Email, I need to do is that when the login is successful in any of the entry methods, I can run a script, i.e. If I login with Facebook and is a successful, I must then execute a script that takes statistics through Facebook, the same for google and the same with Email, my question is how can I, know when the login is successful, to do a condition that allows me to execute my script?

-- 
Ray Bon
Programmer analyst
Development Services, University Systems
2507218831 | CLE 019 | rb...@uvic.ca

Fernando Gomez

unread,
Aug 9, 2018, 4:06:27 PM8/9/18
to CAS Community

Thanks Ray, I'm interested in having javascript executed in the user's browser that collects data about the service

Andy Ng

unread,
Aug 9, 2018, 10:06:18 PM8/9/18
to CAS Community
Hi Fernando,

I previously also tried to execute some JavaScript after user login successful and before redirected back to service, and I successfully created an example.

However, I didn't need to know whether it is Facebook, Google, or Email, unlike your requirement. I just need to execute some JS for all users.

I will give you my example, but you might want to research more on how to determine the Delegate Authentication client (i.e. whether is FB, Google or email).

So, the following is what I changed (CAS 5.3.0) [It is still a demo testing code, I am not responsible if there is any security / performance issue]:

===================================================================================================================================
1. ExampleCasWebflowContextConfiguration

@Configuration("exampleCasWebflowContextConfiguration")
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class ExampleCasWebflowContextConfiguration{
    @Autowired
FlowBuilderServices builder;
   
    @Autowired
    FlowDefinitionRegistry loginFlowRegistry;
    

    @Autowired
    FlowDefinitionRegistry logoutFlowRegistry;
    
    @Autowired
    private CasConfigurationProperties casProperties;    
    

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public CasWebflowConfigurer defaultWebflowConfigurer() {
        final ExampleDefaultWebflowConfigurer c = new ExampleDefaultWebflowConfigurer (builder, loginFlowRegistry, applicationContext, casProperties);
        c.setLogoutFlowDefinitionRegistry(logoutFlowRegistry);
        c.initialize();
        return c;
    }    
    

    @Bean
    @RefreshScope
    public Action redirectToServiceAction() {
        return new ExampleRedirectToServiceAction(responseBuilderLocator);
    }
}



===================================================================================================================================
2. ExampleDefaultWebflowConfigurer
public class ExampleDefaultWebflowConfigurer extends DefaultLoginWebflowConfigurer{
    
public ExampleDefaultWebflowConfigurer(FlowBuilderServices flowBuilderServices,
FlowDefinitionRegistry flowDefinitionRegistry, ApplicationContext applicationContext,
CasConfigurationProperties casProperties) {
super(flowBuilderServices, flowDefinitionRegistry, applicationContext, casProperties);
}

    /**
     * Create redirect end state.
     * 
     * @param flow the flow
     */
    @Override
    protected void createRedirectEndState(final Flow flow) {
        createEndState(flow, CasWebflowConstants.STATE_ID_REDIRECT_VIEW, "casGenericRedirectView");
        //createEndState(flow, CasWebflowConstants.STATE_ID_REDIRECT_VIEW, "requestScope.url", true);
    }
}



===================================================================================================================================
3. ExampleRedirectToServiceAction

public class ExampleRedirectToServiceAction extends RedirectToServiceAction {
public ExampleRedirectToServiceAction(ResponseBuilderLocator responseBuilderLocator) {
super(responseBuilderLocator);
}


    protected Event finalizeResponseEvent(final RequestContext requestContext, final WebApplicationService service,
                                          final Response response) {
         String client = ........; //Here you need to research on how you can receive the client (e.g. Google Facebook)

        requestContext.getRequestScope().put("client", client);
   
    Event defaultFinalizeResponseEvent = super.finalizeResponseEvent(requestContext, service, response);
    return defaultFinalizeResponseEvent;
    }

}



===================================================================================================================================
4. src/main/resources/templates/casGenericRedirectView.html

<html>
<head>
<script>

/*<![CDATA[*/
var url = /*[[${url}]]*/ null; //This is the url to return to the user after login success
var client = /*[[${client}]]*/ null; //This is your client

//You do your javascript execution here......
/*]]>*/


location.replace(url); // proceed to redirect user to service
// you might also want to redirect user when JavaScript is not enable, you will need to research more on that and put in this file as well
</script>
</head>


===================================================================================================================================
5. src/main/resoruces/META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.www.ExampleCasWebflowContextConfiguration

===================================================================================================================================
With this change, you will be able to execute JavaScript after user login success and before they are redirected to their service.

Cheers!
- Andy

Fernando Gomez

unread,
Aug 15, 2018, 1:22:44 PM8/15/18
to CAS Community

Hi Andy, I'm really grateful for your answer, please, could you tell me where I put what you mention to me?

Andy Ng

unread,
Aug 15, 2018, 9:33:22 PM8/15/18
to CAS Community
Hi,

Are you using cas maven overlay (https://github.com/apereo/cas-overlay-template/tree/5.3)? 

If so, for the java class, you should create a new file with those names, and put it in src/main/java

For the package name use your own defined package name.

For others, I already specified the location to you.

If you still have problem, please specified what you tried and see if we can help you from there.

Cheers!
- Andy
Reply all
Reply to author
Forward
0 new messages