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