Skip to main content

Digital Transformation

Implementing Cookie Based TAI for the Developer

As identified in another post Full login filter testing for local developer environments, the requirement may arise that developer environments need to fully test implicitLoginFilter without deploying code to another environment.  This post walks thru the code and setup of a form based TAI.

Create a new class which implements TrustAssociationInterceptor. In this class we will pull 1 property from the WAS configurations of the interceptor. The name of this property is “cookieName” and will contain the name of the cookie whose value will be the ID for the user to be logged in.  In my implementation I have set this property equal to “TAIUserID”.

To set the cookie for this TAI I have used the Web Developer Plugin for Firefox, however you may also use the html code below.  Make sure that the domain specified for the cookie matches the domain which portal is accessed.

createTAICookie.html

<script type=”text/javascript”>
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? “” : “; expires=”+exdate.toUTCString());
document.cookie=c_name + “=” + c_value;
}

setCookie(‘TAIUserID’,’wpsadmin’,1);
window.location = “http://localhost:10039/wps/myportal”;
</script>

CODE:

/**
 * @author Charles Mahoney, Perficient Inc.
 */
public class CookieBasedTAI implements TrustAssociationInterceptor {
	private static String cookie = null;
	private static Logger logger = Logger.getLogger(CookieBasedTAI.class.getName());

	@Override
	public void cleanup() {	}

	@Override
	public String getType() {return String.format("%s version %s", this.getClass().getName(), this.getVersion());}

	@Override
	public String getVersion() {return "1.0";}

	@Override
	public int initialize(Properties props) throws WebTrustAssociationFailedException {
		logger.entering(this.getClass().getName(), "CookieBasedTAI.initialize()");

		// read properties from configuration in WAS
		this.cookie = props.getProperty("cookieName");

		logger.exiting(this.getClass().getName(), "CookieBasedTAI.initialize()");

		if(StringUtils.isBlank(this.cookie)){
			return 1;
		}
		return 0;
	}

	@Override
	public boolean isTargetInterceptor(HttpServletRequest req) throws WebTrustAssociationException {
		logger.info("CookieBasedTAI.isTargetInterceptor()");
		for (Cookie c : req.getCookies()) {
			if (c.getName().equals(this.cookie)){
				logger.info(this.cookie + " cookie exists");
				return true;
			}
		}
		logger.info(this.cookie + " cookie does not exist");
		return false;
	}

	@Override
	public TAIResult negotiateValidateandEstablishTrust(HttpServletRequest req, HttpServletResponse res) throws WebTrustAssociationFailedException {
		logger.entering(this.getClass().getName(), "CookieBasedTAI.negotiate...()");
		for (Cookie c : req.getCookies()) {
			if (c.getName().equals(this.cookie) && StringUtils.isNotBlank(c.getValue())) {
				return TAIResult.create(HttpServletResponse.SC_OK, c.getValue());
			}
		}

		logger.info("Not Authenticated");
		return TAIResult.create(HttpServletResponse.SC_UNAUTHORIZED);
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Charles Mahoney

More from this Author

Follow Us