1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Calendar;
6 import java.util.HashMap;
8 import java.util.Properties;
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;
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.email.EmailProvider;
18 import org.cacert.gigi.output.Menu;
19 import org.cacert.gigi.output.MenuItem;
20 import org.cacert.gigi.output.Outputable;
21 import org.cacert.gigi.output.Form.CSRFException;
22 import org.cacert.gigi.output.template.Template;
23 import org.cacert.gigi.pages.LoginPage;
24 import org.cacert.gigi.pages.MainPage;
25 import org.cacert.gigi.pages.Page;
26 import org.cacert.gigi.pages.TestSecure;
27 import org.cacert.gigi.pages.Verify;
28 import org.cacert.gigi.pages.account.ChangePasswordPage;
29 import org.cacert.gigi.pages.account.MailCertificateAdd;
30 import org.cacert.gigi.pages.account.MailCertificates;
31 import org.cacert.gigi.pages.account.MailOverview;
32 import org.cacert.gigi.pages.account.MyDetails;
33 import org.cacert.gigi.pages.error.PageNotFound;
34 import org.cacert.gigi.pages.main.RegisterPage;
35 import org.cacert.gigi.pages.wot.AssurePage;
36 import org.cacert.gigi.util.ServerConstants;
38 public class Gigi extends HttpServlet {
39 public static final String LOGGEDIN = "loggedin";
40 public static final String USER = "user";
41 private static final long serialVersionUID = -6386785421902852904L;
42 private Template baseTemplate;
43 private HashMap<String, Page> pages = new HashMap<String, Page>();
46 public Gigi(Properties conf) {
47 EmailProvider.init(conf);
48 DatabaseConnection.init(conf);
52 public void init() throws ServletException {
53 pages.put("/error", new PageNotFound());
54 pages.put("/login", new LoginPage("CACert - Login"));
55 pages.put("/", new MainPage("CACert - Home"));
56 pages.put("/secure", new TestSecure());
57 pages.put(Verify.PATH, new Verify());
58 pages.put(AssurePage.PATH + "/*", new AssurePage());
59 pages.put(MailCertificates.PATH + "/*", new MailCertificates());
60 pages.put(MyDetails.PATH, new MyDetails());
61 pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
62 pages.put(RegisterPage.PATH, new RegisterPage());
63 pages.put(MailCertificateAdd.PATH, new MailCertificateAdd());
64 pages.put(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
65 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
66 m = new Menu("Certificates", "cert", new MenuItem(MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("",
67 "Client Certificates"), new MenuItem("", "Domains"), new MenuItem("", "Server Certificates"));
73 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
76 // if (req.getHeader("Origin") != null) {
77 // resp.getWriter().println("No cross domain access allowed.");
80 HttpSession hs = req.getSession();
81 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
83 hs.setAttribute(LOGGEDIN, null);
86 resp.sendRedirect("/");
90 final Page p = getPage(req.getPathInfo());
93 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
94 String request = req.getPathInfo();
95 request = request.split("\\?")[0];
96 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
97 resp.sendRedirect("/login");
100 if (p.beforeTemplate(req, resp)) {
103 HashMap<String, Object> vars = new HashMap<String, Object>();
105 resp.setContentType("text/html; charset=utf-8");
106 Outputable content = new Outputable() {
109 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
111 if (req.getMethod().equals("POST")) {
116 } catch (CSRFException err) {
118 resp.sendError(500, "CSRF invalid");
119 } catch (IOException e) {
122 } catch (IOException e) {
129 vars.put("title", p.getTitle());
130 vars.put("static", ServerConstants.getStaticHostNamePort());
131 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
132 vars.put("content", content);
133 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
135 resp.sendError(404, "Page not found.");
140 private Page getPage(String pathInfo) {
141 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
142 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
144 Page page = pages.get(pathInfo);
148 page = pages.get(pathInfo + "/*");
152 int idx = pathInfo.lastIndexOf('/');
153 pathInfo = pathInfo.substring(0, idx);
155 page = pages.get(pathInfo + "/*");
163 public static void addXSSHeaders(HttpServletResponse hsr) {
164 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://"
165 + ServerConstants.getSecureHostNamePort());
166 hsr.addHeader("Access-Control-Max-Age", "60");
168 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
169 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
173 private static String defaultCSP = null;
175 private static String getDefaultCSP() {
176 if (defaultCSP == null) {
177 StringBuffer csp = new StringBuffer();
178 csp.append("default-src 'none';");
179 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
180 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
181 csp.append(";media-src 'none'; object-src 'none';");
182 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
183 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
184 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://"
185 + ServerConstants.getWwwHostNamePort());
186 csp.append("report-url https://api.cacert.org/security/csp/report");
187 defaultCSP = csp.toString();