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