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.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.main.RegisterPage;
34 import org.cacert.gigi.pages.wot.AssurePage;
35 import org.cacert.gigi.util.ServerConstants;
37 public class Gigi extends HttpServlet {
38 public static final String LOGGEDIN = "loggedin";
39 public static final String USER = "user";
40 private static final long serialVersionUID = -6386785421902852904L;
41 private Template baseTemplate;
42 private HashMap<String, Page> pages = new HashMap<String, Page>();
45 public Gigi(Properties conf) {
46 EmailProvider.init(conf);
47 DatabaseConnection.init(conf);
51 public void init() throws ServletException {
52 pages.put("/login", new LoginPage("CACert - Login"));
53 pages.put("/", new MainPage("CACert - Home"));
54 pages.put("/secure", new TestSecure());
55 pages.put(Verify.PATH, new Verify());
56 pages.put(AssurePage.PATH + "/*", new AssurePage());
57 pages.put(MailCertificates.PATH + "/*", new MailCertificates());
58 pages.put(MyDetails.PATH, new MyDetails());
59 pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
60 pages.put(RegisterPage.PATH, new RegisterPage());
61 pages.put(MailCertificateAdd.PATH, new MailCertificateAdd());
62 pages.put(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
63 baseTemplate = new Template(new InputStreamReader(Gigi.class.getResourceAsStream("Gigi.templ")));
64 m = new Menu("Certificates", "cert", new MenuItem(MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("",
65 "Client Certificates"), new MenuItem("", "Domains"), new MenuItem("", "Server Certificates"));
71 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
74 // if (req.getHeader("Origin") != null) {
75 // resp.getWriter().println("No cross domain access allowed.");
78 HttpSession hs = req.getSession();
79 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
81 hs.setAttribute(LOGGEDIN, null);
84 resp.sendRedirect("/");
88 final Page p = getPage(req.getPathInfo());
91 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
92 String request = req.getPathInfo();
93 request = request.split("\\?")[0];
94 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
95 resp.sendRedirect("/login");
98 if (p.beforeTemplate(req, resp)) {
101 HashMap<String, Object> vars = new HashMap<String, Object>();
103 resp.setContentType("text/html; charset=utf-8");
104 Outputable content = new Outputable() {
107 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
109 if (req.getMethod().equals("POST")) {
114 } catch (IOException e) {
121 vars.put("title", p.getTitle());
122 vars.put("static", ServerConstants.getStaticHostNamePort());
123 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
124 vars.put("content", content);
125 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
127 resp.sendError(404, "Page not found.");
132 private Page getPage(String pathInfo) {
133 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
134 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
136 Page page = pages.get(pathInfo);
140 page = pages.get(pathInfo + "/*");
144 int idx = pathInfo.lastIndexOf('/');
145 pathInfo = pathInfo.substring(0, idx);
147 page = pages.get(pathInfo + "/*");
155 public static void addXSSHeaders(HttpServletResponse hsr) {
156 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://"
157 + ServerConstants.getSecureHostNamePort());
158 hsr.addHeader("Access-Control-Max-Age", "60");
160 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
161 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
165 private static String defaultCSP = null;
167 private static String getDefaultCSP() {
168 if (defaultCSP == null) {
169 StringBuffer csp = new StringBuffer();
170 csp.append("default-src 'none';");
171 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
172 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
173 csp.append(";media-src 'none'; object-src 'none';");
174 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
175 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
176 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://"
177 + ServerConstants.getWwwHostNamePort());
178 csp.append("report-url https://api.cacert.org/security/csp/report");
179 defaultCSP = csp.toString();