01: package edu.yale.its.tp.cas.servlet;
02:
03: import java.io.*;
04: import javax.servlet.*;
05: import javax.servlet.http.*;
06: import edu.yale.its.tp.cas.ticket.*;
07:
08: /**
09: * Lets users explicitly log out from the Central Authentication Servlet.
10: */
11: public class Logout extends HttpServlet {
12:
13: //*********************************************************************
14: // Constants
15:
16: private static final String TGC_ID = "CASTGC";
17: private static final String SERVICE = "service";
18:
19: //*********************************************************************
20: // Private state
21:
22: private ServletContext app;
23: private GrantorCache tgcCache;
24: private String logoutPage;
25: private String redirect;
26:
27: //*********************************************************************
28: // Initialization
29:
30: public void init(ServletConfig config) throws ServletException {
31: // retrieve the context and the caches
32: app = config.getServletContext();
33: tgcCache = (GrantorCache) app.getAttribute("tgcCache");
34:
35: // retrieve a relative URL for the login form
36: logoutPage = app
37: .getInitParameter("edu.yale.its.tp.cas.logoutPage");
38: redirect = app.getInitParameter("edu.yale.its.tp.cas.redirect");
39: if ((logoutPage == null) || (redirect == null))
40: throw new ServletException(
41: "need edu.yale.its.tp.cas.logoutPage and redirect");
42: }
43:
44: //*********************************************************************
45: // Request handling
46:
47: public void doGet(HttpServletRequest request,
48: HttpServletResponse response) throws ServletException,
49: IOException {
50:
51: // avoid caching (in the stupidly numerous ways we must)
52: response.setHeader("pragma", "no-cache");
53: response.setHeader("Cache-Control", "no-cache");
54: response.setHeader("Cache-Control", "no-store");
55: response.setDateHeader("Expires", 0);
56:
57: // see if the user sent us a valid TGC
58: Cookie[] cookies = request.getCookies();
59: if (cookies != null) {
60: for (int i = 0; i < cookies.length; i++) {
61: if (cookies[i].getName().equals(TGC_ID)) {
62: TicketGrantingTicket t = (TicketGrantingTicket) tgcCache
63: .getTicket(cookies[i].getValue());
64: if (t == null)
65: continue;
66:
67: // ticket found!
68: tgcCache.deleteTicket(cookies[i].getValue());
69: destroyTgc(request, response);
70: }
71: }
72: }
73:
74: // get the optional return SERVICE
75: if (request.getParameter(SERVICE) != null) {
76: // redirect to the calling service
77: request.setAttribute("serviceId", request
78: .getParameter(SERVICE));
79: app.getRequestDispatcher(redirect).forward(request,
80: response);
81: } else {
82: // forward to the UI to reassure the user
83: app.getRequestDispatcher(logoutPage).forward(request,
84: response);
85: }
86: }
87:
88: /** Destroys the browser's TGC. */
89: private void destroyTgc(HttpServletRequest request,
90: HttpServletResponse response) {
91: Cookie tgcOverwrite = new Cookie(TGC_ID, "destroyed");
92: tgcOverwrite.setPath(request.getContextPath());
93: tgcOverwrite.setMaxAge(0);
94: tgcOverwrite.setSecure(true);
95: response.addCookie(tgcOverwrite);
96: }
97:
98: }
|