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