]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
Move l10n-stuff to own package.
[gigi.git] / src / org / cacert / gigi / Gigi.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Calendar;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Properties;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.email.EmailProvider;
18 import org.cacert.gigi.localisation.Language;
19 import org.cacert.gigi.output.Form.CSRFException;
20 import org.cacert.gigi.output.Menu;
21 import org.cacert.gigi.output.MenuItem;
22 import org.cacert.gigi.output.Outputable;
23 import org.cacert.gigi.output.template.Template;
24 import org.cacert.gigi.pages.LoginPage;
25 import org.cacert.gigi.pages.MainPage;
26 import org.cacert.gigi.pages.Page;
27 import org.cacert.gigi.pages.TestSecure;
28 import org.cacert.gigi.pages.Verify;
29 import org.cacert.gigi.pages.account.ChangePasswordPage;
30 import org.cacert.gigi.pages.account.DomainOverview;
31 import org.cacert.gigi.pages.account.CertificateAdd;
32 import org.cacert.gigi.pages.account.Certificates;
33 import org.cacert.gigi.pages.account.MailOverview;
34 import org.cacert.gigi.pages.account.MyDetails;
35 import org.cacert.gigi.pages.error.PageNotFound;
36 import org.cacert.gigi.pages.main.RegisterPage;
37 import org.cacert.gigi.pages.wot.AssurePage;
38 import org.cacert.gigi.util.ServerConstants;
39
40 public class Gigi extends HttpServlet {
41
42     public static final String LOGGEDIN = "loggedin";
43
44     public static final String USER = "user";
45
46     private static final long serialVersionUID = -6386785421902852904L;
47
48     private Template baseTemplate;
49
50     private HashMap<String, Page> pages = new HashMap<String, Page>();
51
52     Menu m;
53
54     public Gigi(Properties conf) {
55         EmailProvider.init(conf);
56         DatabaseConnection.init(conf);
57     }
58
59     @Override
60     public void init() throws ServletException {
61         pages.put("/error", new PageNotFound());
62         pages.put("/login", new LoginPage("CACert - Login"));
63         pages.put("/", new MainPage("CACert - Home"));
64         pages.put("/secure", new TestSecure());
65         pages.put(Verify.PATH, new Verify());
66         pages.put(AssurePage.PATH + "/*", new AssurePage());
67         pages.put(Certificates.PATH + "/*", new Certificates());
68         pages.put(MyDetails.PATH, new MyDetails());
69         pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
70         pages.put(RegisterPage.PATH, new RegisterPage());
71         pages.put(CertificateAdd.PATH, new CertificateAdd());
72         pages.put(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
73         pages.put(DomainOverview.PATH, new DomainOverview("Domains"));
74         baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
75         m = new Menu("Certificates", "cert", new MenuItem(MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("", "Client Certificates"), new MenuItem("", "Domains"), new MenuItem("", "Server Certificates"));
76         super.init();
77
78     }
79
80     @Override
81     protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
82         addXSSHeaders(resp);
83         // if (req.getHeader("Origin") != null) {
84         // resp.getWriter().println("No cross domain access allowed.");
85         // return;
86         // }
87         HttpSession hs = req.getSession();
88         if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
89             if (hs != null) {
90                 hs.setAttribute(LOGGEDIN, null);
91                 hs.invalidate();
92             }
93             resp.sendRedirect("/");
94             return;
95         }
96
97         final Page p = getPage(req.getPathInfo());
98         if (p != null) {
99
100             if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
101                 String request = req.getPathInfo();
102                 request = request.split("\\?")[0];
103                 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
104                 resp.sendRedirect("/login");
105                 return;
106             }
107             if (p.beforeTemplate(req, resp)) {
108                 return;
109             }
110             HashMap<String, Object> vars = new HashMap<String, Object>();
111
112             resp.setContentType("text/html; charset=utf-8");
113             Outputable content = new Outputable() {
114
115                 @Override
116                 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
117                     try {
118                         if (req.getMethod().equals("POST")) {
119                             if (req.getQueryString() != null) {
120                                 return;
121                             }
122                             p.doPost(req, resp);
123                         } else {
124                             p.doGet(req, resp);
125                         }
126                     } catch (CSRFException err) {
127                         try {
128                             resp.sendError(500, "CSRF invalid");
129                         } catch (IOException e) {
130                             e.printStackTrace();
131                         }
132                     } catch (IOException e) {
133                         e.printStackTrace();
134                     }
135
136                 }
137             };
138             vars.put("menu", m);
139             vars.put("title", p.getTitle());
140             vars.put("static", ServerConstants.getStaticHostNamePort());
141             vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
142             vars.put("content", content);
143             baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
144         } else {
145             resp.sendError(404, "Page not found.");
146         }
147
148     }
149
150     private Page getPage(String pathInfo) {
151         if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
152             pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
153         }
154         Page page = pages.get(pathInfo);
155         if (page != null) {
156             return page;
157         }
158         page = pages.get(pathInfo + "/*");
159         if (page != null) {
160             return page;
161         }
162         int idx = pathInfo.lastIndexOf('/');
163         pathInfo = pathInfo.substring(0, idx);
164
165         page = pages.get(pathInfo + "/*");
166         if (page != null) {
167             return page;
168         }
169         return null;
170
171     }
172
173     public static void addXSSHeaders(HttpServletResponse hsr) {
174         hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort());
175         hsr.addHeader("Access-Control-Max-Age", "60");
176
177         hsr.addHeader("Content-Security-Policy", getDefaultCSP());
178         hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
179
180     }
181
182     private static String defaultCSP = null;
183
184     private static String getDefaultCSP() {
185         if (defaultCSP == null) {
186             StringBuffer csp = new StringBuffer();
187             csp.append("default-src 'none';");
188             csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
189             csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
190             csp.append(";media-src 'none'; object-src 'none';");
191             csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
192             csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
193             csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
194             csp.append("report-url https://api.cacert.org/security/csp/report");
195             defaultCSP = csp.toString();
196         }
197         return defaultCSP;
198     }
199 }