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.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;
40 public class Gigi extends HttpServlet {
42 public static final String LOGGEDIN = "loggedin";
44 public static final String USER = "user";
46 private static final long serialVersionUID = -6386785421902852904L;
48 private Template baseTemplate;
50 private HashMap<String, Page> pages = new HashMap<String, Page>();
54 public Gigi(Properties conf) {
55 EmailProvider.init(conf);
56 DatabaseConnection.init(conf);
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"));
81 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
83 // if (req.getHeader("Origin") != null) {
84 // resp.getWriter().println("No cross domain access allowed.");
87 HttpSession hs = req.getSession();
88 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
90 hs.setAttribute(LOGGEDIN, null);
93 resp.sendRedirect("/");
97 final Page p = getPage(req.getPathInfo());
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");
107 if (p.beforeTemplate(req, resp)) {
110 HashMap<String, Object> vars = new HashMap<String, Object>();
112 resp.setContentType("text/html; charset=utf-8");
113 Outputable content = new Outputable() {
116 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
118 if (req.getMethod().equals("POST")) {
119 if (req.getQueryString() != null) {
126 } catch (CSRFException err) {
128 resp.sendError(500, "CSRF invalid");
129 } catch (IOException e) {
132 } catch (IOException e) {
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);
145 resp.sendError(404, "Page not found.");
150 private Page getPage(String pathInfo) {
151 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
152 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
154 Page page = pages.get(pathInfo);
158 page = pages.get(pathInfo + "/*");
162 int idx = pathInfo.lastIndexOf('/');
163 pathInfo = pathInfo.substring(0, idx);
165 page = pages.get(pathInfo + "/*");
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");
177 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
178 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
182 private static String defaultCSP = null;
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();