Using the basic Java CAS Client objects

CAS Java Objects

You may also authenticate users "manually" using the CAS Java objects. In this case, you would instantiate a new ServiceTicketValidator or ProxyTicketValidator. Notice that in the example below, the page already expects to receive a ticket parameter (this is the servlet that CAS returned to after the user logged in). If this servlet was accessed directly by a user, it would need to check that the request parameter, ticket, was not null. If it was null, the servlet would need to redirect to the CAS login page manually.

ServiceTicketValidator:

import edu.yale.its.tp.cas.client.*;

...
String user = null;
String errorCode = null;
String errorMessage = null;
String xmlResponse = null;

/* instantiate a new ServiceTicketValidator */
ServiceTicketValidator sv = new ServiceTicketValidator();

/* set its parameters */
sv.setCasValidateUrl("https://secure.its.yale.edu/cas/serviceValidate");
sv.setService(urlOfThisService);
sv.setServiceTicket(request.getParameter("ticket"));

/*
 * If we want to be able to acquire proxy tickets (requires callback servlet to be set up 
 * in web.xml - see below)
 */

String urlOfProxyCallbackServlet = "https://portal.yale.edu/CasProxyServlet";

sv.setProxyCallbackUrl(urlOfProxyCallbackServlet);

/* contact CAS and validate */
sv.validate();

/* if we want to look at the raw response, we can use getResponse() */
xmlResponse = sv.getResponse();

/* read the response */

// Yes, this method is misspelled in this way 
// in the ServiceTicketValidator implementation. 
// Sorry.
if(sv.isAuthenticationSuccesful()) {
    user = sv.getUser();
} else {
    errorCode = sv.getErrorCode();
    errorMessage = sv.getErrorMessage();
    /* handle the error */
}

/* The user is now authenticated. */

/* If we did set the proxy callback url, we can get proxy tickets with: */


String urlOfTargetService = "http://hkg2.its.yale.edu/someApp/portalFeed";

String proxyTicket =
    edu.yale.its.tp.cas.proxy.ProxyTicketReceptor.getProxyTicket(
        sv.getPgtIou(),urlOfTargetService);

ProxyTicketValidator

The proxy ticket validator is almost identical, except it allows you to validate service tickets or proxy tickets. This class contains one additional method, getProxyList(), which accesses the list of URLs through which the authentication was proxied.

import edu.yale.its.tp.cas.client.*;

...

String user = null;
String errorCode = null;
String errorMessage = null;
String xmlResponse = null;
List proxyList = null;

/* instantiate a new ProxyTicketValidator */
ProxyTicketValidator pv = new ProxyTicketValidator(); 

/* set its parameters */
pv.setCasValidateUrl("https://secure.its.yale.edu/cas/proxyValidate");
pv.setService(urlOfThisService);
pv.setServiceTicket(request.getParameter("ticket")); 

/*
 * If we want to be able to acquire proxy tickets (requires callback servlet to be set up 
 * in web.xml -- see below)
 */

String urlOfProxyCallbackServlet = "https://portal.yale.edu/CasProxyServlet";

pv.setProxyCallbackUrl(urlOfProxyCallbackServlet); 

/* contact CAS and validate */
pv.validate();

/* if we want to look at the raw response, we can use getResponse() */
xmlResponse = pv.getResponse(); 

/* read the response */
// Yes, this method is misspelled in this way 
// in the ServiceTicketValidator implementation. 
// Sorry.
if(pv.isAuthenticationSuccesful()) {
    user = pv.getUser();
    proxyList = pv.getProxyList();
} else {
    errorCode = pv.getErrorCode();
    errorMessage = pv.getErrorMessage();
    /* handle the error */
}

/* The user is now authenticated. */

/* If we did set the proxy callback url, we can get proxy tickets with this method call: 
 */

String urlOfTargetService = "http://hkg2.its.yale.edu/someApp/portalFeed";

String proxyTicket =
    edu.yale.its.tp.cas.proxy.ProxyTicketReceptor.getProxyTicket(
        pv.getPgtIou(),urlOfTargetService);