1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.PrintWriter;
6 import java.util.Calendar;
7 import java.util.HashMap;
9 import java.util.Properties;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpSession;
17 import org.cacert.gigi.database.DatabaseConnection;
18 import org.cacert.gigi.email.EmailProvider;
19 import org.cacert.gigi.output.Menu;
20 import org.cacert.gigi.output.MenuItem;
21 import org.cacert.gigi.output.Outputable;
22 import org.cacert.gigi.output.Template;
23 import org.cacert.gigi.output.Form.CSRFError;
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.MailCertificateAdd;
31 import org.cacert.gigi.pages.account.MailCertificates;
32 import org.cacert.gigi.pages.account.MailOverview;
33 import org.cacert.gigi.pages.account.MyDetails;
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("/login", new LoginPage("CACert - Login"));
54 pages.put("/", new MainPage("CACert - Home"));
55 pages.put("/secure", new TestSecure());
56 pages.put(Verify.PATH, new Verify());
57 pages.put(AssurePage.PATH + "/*", new AssurePage());
58 pages.put(MailCertificates.PATH + "/*", new MailCertificates());
59 pages.put(MyDetails.PATH, new MyDetails());
60 pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
61 pages.put(RegisterPage.PATH, new RegisterPage());
62 pages.put(MailCertificateAdd.PATH, new MailCertificateAdd());
63 pages.put(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
64 baseTemplate = new Template(new InputStreamReader(Gigi.class.getResourceAsStream("Gigi.templ")));
65 m = new Menu("Certificates", "cert", new MenuItem(MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("",
66 "Client Certificates"), new MenuItem("", "Domains"), new MenuItem("", "Server Certificates"));
72 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
75 // if (req.getHeader("Origin") != null) {
76 // resp.getWriter().println("No cross domain access allowed.");
79 HttpSession hs = req.getSession();
80 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
82 hs.setAttribute(LOGGEDIN, null);
85 resp.sendRedirect("/");
89 final Page p = getPage(req.getPathInfo());
92 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
93 String request = req.getPathInfo();
94 request = request.split("\\?")[0];
95 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
96 resp.sendRedirect("/login");
99 if (p.beforeTemplate(req, resp)) {
102 HashMap<String, Object> vars = new HashMap<String, Object>();
104 resp.setContentType("text/html; charset=utf-8");
105 Outputable content = new Outputable() {
108 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
110 if (req.getMethod().equals("POST")) {
115 } catch (IOException e) {
117 } catch (CSRFError err) {
119 resp.sendError(500, "CSRF invalid");
120 } catch (IOException e) {
128 vars.put("title", p.getTitle());
129 vars.put("static", ServerConstants.getStaticHostNamePort());
130 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
131 vars.put("content", content);
132 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
134 resp.sendError(404, "Page not found.");
139 private Page getPage(String pathInfo) {
140 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
141 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
143 Page page = pages.get(pathInfo);
147 page = pages.get(pathInfo + "/*");
151 int idx = pathInfo.lastIndexOf('/');
152 pathInfo = pathInfo.substring(0, idx);
154 page = pages.get(pathInfo + "/*");
162 public static void addXSSHeaders(HttpServletResponse hsr) {
163 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://"
164 + ServerConstants.getSecureHostNamePort());
165 hsr.addHeader("Access-Control-Max-Age", "60");
167 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
168 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
172 private static String defaultCSP = null;
174 private static String getDefaultCSP() {
175 if (defaultCSP == null) {
176 StringBuffer csp = new StringBuffer();
177 csp.append("default-src 'none';");
178 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
179 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
180 csp.append(";media-src 'none'; object-src 'none';");
181 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
182 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
183 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://"
184 + ServerConstants.getWwwHostNamePort());
185 csp.append("report-url https://api.cacert.org/security/csp/report");
186 defaultCSP = csp.toString();