just cleaning up for the night
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / hs / HandleServlet.java
1 package edu.internet2.middleware.shibboleth.hs;
2
3 import java.io.*;
4 import java.text.*;
5 import java.util.*;
6 import javax.servlet.*;
7 import javax.servlet.http.*;
8 import edu.internet2.middleware.shibboleth.*;
9 import edu.internet2.middleware.shibboleth.common.*;
10 import org.opensaml.*;
11
12 public class HandleServlet extends HttpServlet {
13
14     private HandleRepositoryFactory hrf;
15     private long ticketExp; 
16     private HandleServiceSAML hsSAML;
17
18     public void init(ServletConfig conf)
19         throws ServletException
20     {
21         super.init(conf);
22         getInitParams();
23
24         try {
25             hsSAML = new HandleServiceSAML( getInitParameter("domain"), 
26                                             getInitParameter("AAurl"),
27                                             getInitParameter("HSname"),
28                                             getInitParameter("KSpath"),
29                                             getInitParameter("KSpass"),
30                                             getInitParameter("KSkeyalias"),
31                                             getInitParameter("KSkeypass"),
32                                             getInitParameter("certalias") );
33             hrf = HandleRepositoryFactory.getInstance
34                 ( Constants.POLICY_CLUBSHIB, this );
35         }
36         catch (SAMLException ex) {
37             throw new ServletException( "Error initializing SAML libraries: " + ex );
38         }
39         catch (java.security.KeyStoreException ex) {
40             throw new ServletException( "Error initializing private KeyStore: " + ex );
41         }
42         
43         catch (HandleException ex) {
44             throw new ServletException( "Error initializing Handle Service: " +ex );
45         }
46         catch (Exception ex) {
47             throw new ServletException( "Error initializing private KeyStore: " +ex );
48         }
49         if (hsSAML == null) {
50             throw new ServletException( "Error initializing SAML libraries: No Profile created." );
51         }  
52     }
53     
54     private void getInitParams() throws ServletException {
55
56         String ticket = getInitParameter("ticket");
57         if (ticket == null) {
58             ticket = "1400000";
59         }
60         ticketExp = Long.parseLong(ticket);
61         if ( getInitParameter("domain") == null || 
62              getInitParameter("domain").equals("")) {
63             throw new ServletException("Cannot find host domain in init parameters");
64         }
65         if ( getInitParameter("AAurl") == null || 
66              getInitParameter("AAurl").equals("")) {
67             throw new ServletException("Cannot find host Attribute Authority location in init parameters");
68         }
69         if ( getInitParameter("HSname") == null || 
70              getInitParameter("HSname").equals("")) {
71             throw new ServletException("Cannot find Handle Service name in init parameters");
72         }
73         if ( getInitParameter("KSpath") == null || 
74              getInitParameter("KSpath").equals("")) {
75             throw new ServletException("Cannot find path to KeyStore file in init parameters");
76         }
77         if ( getInitParameter("KSpass") == null || 
78              getInitParameter("KSpass").equals("")) {
79             throw new ServletException("Cannot find password to KeyStore in init parameters");
80         }
81         if ( getInitParameter("KSkeyalias") == null || 
82              getInitParameter("KSkeyalias").equals("")) {
83             throw new ServletException("Cannot find private key alias to KeyStore in init parameters");
84         }
85         if ( getInitParameter("KSkeypass") == null || 
86              getInitParameter("KSkeypass").equals("")) {
87             throw new ServletException("Cannot find private key password to Keystore in init parameters");
88         }
89         if ( getInitParameter("certalias") == null || 
90              getInitParameter("certalias").equals("")) {
91             throw new ServletException("Cannot find certificate alias in init parameters");
92         }
93
94
95     }
96
97     public void doGet(HttpServletRequest req, 
98                       HttpServletResponse res)
99         throws ServletException, IOException
100     {
101
102
103
104         HandleEntry he = null;
105
106         try {
107             checkRequestParams(req);
108
109             req.setAttribute("shire", req.getParameter("shire"));
110             req.setAttribute("target", req.getParameter("target"));
111
112             he = new HandleEntry( req.getRemoteUser(), req.getAuthType(), 
113                                   ticketExp );
114             hrf.insertHandleEntry( he );
115             
116             byte[] buf = hsSAML.prepare
117                 ( he.getHandle(), req.getParameter("shire"), 
118                   req.getRemoteAddr(), he.getAuthType(), 
119                   new Date(he.getAuthInstant()));
120
121             createForm( req, res, buf );
122         }
123         catch (HandleException ex) {
124             System.out.println(ex);
125             handleError( req, res, ex );
126         }
127
128     }
129     
130     private void createForm( HttpServletRequest req, 
131                              HttpServletResponse res,
132                              byte[] buf )  
133         throws HandleException {
134         try {
135
136             res.setContentType("text/html");
137             PrintWriter out = res.getWriter();
138             out.println("<HTML><HEAD><TITLE>Handle Service</TITLE></HEAD>");
139             out.println("<BODY onLoad=\"document.forms[0].submit()\">");
140             out.println("<p><form name=\"shib\" " + "action=\"" +
141                         req.getParameter("shire")+"\" method=\"POST\">");
142             out.println("<input type=\"hidden\" name=\"TARGET\"" +
143                         " value=\"" + req.getParameter("target") + "\">");
144             out.println("<input type=\"hidden\" name=\"SAMLAssertion\""+
145                         "value=\"" + buf + "\">");
146             out.println("<input type=\"submit\" value=\"Transmit\">");
147             out.println("</form>");
148             
149             /**
150              * uncomment the following to implement 
151              * forwarding to hs.jsp for submission
152              * 
153             //Hardcoded to ASCII to ensure Base64 encoding compatibility
154             req.setAttribute("assertion", new String(buf, "ASCII"));
155             RequestDispatcher rd = req.getRequestDispatcher("/hs.jsp");
156             rd.forward(req, res);
157             */
158         } catch (IOException ex) {
159             throw new HandleException
160                 ("IO interruption while displaying Handle Service UI." + ex);
161         } 
162         /*
163           catch (ServletException ex) {
164             throw new HandleException
165                 ("Problem displaying Handle Service UI." + ex);
166         }
167         */
168     }
169
170     private void handleError( HttpServletRequest req, 
171                              HttpServletResponse res,
172                              Exception e )  
173         throws ServletException, IOException {
174
175         req.setAttribute("errorText", e.toString());
176         req.setAttribute("requestURL", req.getRequestURI().toString());
177         RequestDispatcher rd = req.getRequestDispatcher("/hserror.jsp");
178         
179         rd.forward(req, res);
180         
181     }
182
183                      
184     private void checkRequestParams( HttpServletRequest req )
185         throws HandleException {
186
187         if ( req.getParameter("target") == null 
188              || req.getParameter("target").equals("")) {
189             throw new HandleException("Invalid data from SHIRE: no target URL received.");
190         }
191         if ((req.getParameter("shire") == null)
192             || (req.getParameter("shire").equals(""))) {
193             throw new HandleException("Invalid data from SHIRE: No acceptance URL received.");
194         }
195         if ((req.getRemoteUser() == null)
196             || (req.getRemoteUser().equals(""))) {
197             throw new HandleException("Unable to authenticate remote user");
198         }
199         if ((req.getAuthType() == null) || (req.getAuthType().equals(""))) {
200             throw new HandleException("Unable to obtain authentication type of user.");
201         }
202         if ((req.getRemoteAddr() == null)
203             || (req.getRemoteAddr().equals(""))) {
204             throw new HandleException("Unable to obtain client address.");
205         }    
206     }
207 }
208     
209