001: /*
002: * Copyright 2007 The Kuali Foundation.
003: *
004: * Licensed under the Educational Community License, Version 1.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.opensource.org/licenses/ecl1.php
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package edu.yale.its.tp.cas.servlet;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020: import java.util.Iterator;
021:
022: import javax.servlet.ServletConfig;
023: import javax.servlet.ServletContext;
024: import javax.servlet.ServletException;
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpServletResponse;
027:
028: import edu.yale.its.tp.cas.ticket.ProxyTicket;
029: import edu.yale.its.tp.cas.ticket.ServiceTicket;
030: import edu.yale.its.tp.cas.ticket.ServiceTicketCache;
031:
032: /**
033: * Handles PT validations and sub-PGT acquisitions for the Central Authentication Service. Subclassed (versus collapsed into
034: * superclass) in order to provided rigid segmentation, at runtime, between caches, even though they share an implementation.
035: */
036: public class ProxyValidate extends ServiceValidate {
037:
038: // *********************************************************************
039: // Private state
040:
041: private ServletContext app;
042: private String serviceValidate; // URL
043:
044: // *********************************************************************
045: // Initialization
046:
047: public void init(ServletConfig config) throws ServletException {
048: // let our superclass handle initialization
049: super .init(config);
050:
051: // replace the ST cache with the PT cache
052: stCache = (ServiceTicketCache) config.getServletContext()
053: .getAttribute("ptCache");
054:
055: // read relevant parameters
056: app = config.getServletContext();
057: serviceValidate = app
058: .getInitParameter("edu.yale.its.tp.cas.serviceValidate");
059: if (serviceValidate == null)
060: throw new ServletException(
061: "need edu.yale.its.tp.cas.serviceValidate");
062: }
063:
064: // *********************************************************************
065: // Request handling
066:
067: public void doGet(HttpServletRequest request,
068: HttpServletResponse response) throws IOException,
069: ServletException {
070: /*
071: * Interesting approach: if we have a service ticket (one that starts with "ST"), we forward to the actual serviceValidate
072: * URL, which happens to be implemented by our superclass. Otherwise, we invoke the superclass directly, which lets
073: * modifications we've made in init() take effect.
074: */
075: String ticketString = request.getParameter("ticket");
076: if (ticketString != null && ticketString.startsWith("ST"))
077: app.getRequestDispatcher(serviceValidate).forward(request,
078: response);
079: else
080: super .doGet(request, response);
081: }
082:
083: // *********************************************************************
084: // Response-handling methods
085:
086: /** Sends a validation success message to the given PrintWriter. */
087: protected void validationSuccess(PrintWriter out, ServiceTicket st,
088: String pgtIOU) {
089: // downcast the ticket
090: if (!(st instanceof ProxyTicket)) {
091: throw new IllegalArgumentException(
092: "can't take generic ServiceTicket; need ProxyTicket");
093: }
094: ProxyTicket pt = (ProxyTicket) st;
095:
096: // send the response
097: out
098: .println("<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>");
099: out.println(" <cas:authenticationSuccess>");
100: out
101: .println(" <cas:user>" + pt.getUsername()
102: + "</cas:user>");
103: if (pgtIOU != null && !pgtIOU.equals("")) {
104: out.println(" <cas:proxyGrantingTicket>" + pgtIOU
105: + "</cas:proxyGrantingTicket>");
106: }
107: out.println(" <cas:proxies>");
108: {
109: Iterator proxies = pt.getProxies().iterator();
110: while (proxies.hasNext())
111: out.println(" <cas:proxy>" + proxies.next()
112: + "</cas:proxy>");
113: }
114: out.println(" </cas:proxies>");
115: out.println(" </cas:authenticationSuccess>");
116: out.println("</cas:serviceResponse>");
117: }
118: }
|