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.MailAdd;
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);
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(
63 "My email addresses"));
64 pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
65 baseTemplate = new Template(new InputStreamReader(
66 Gigi.class.getResourceAsStream("Gigi.templ")));
67 m = new Menu("Certificates", "cert", new MenuItem(
68 MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("",
69 "Client Certificates"), new MenuItem("", "Domains"),
70 new MenuItem("", "Server Certificates"));
75 protected void service(final HttpServletRequest req,
76 final HttpServletResponse resp) throws ServletException,
79 if (req.getHeader("Origin") != null) {
80 resp.getWriter().println("No cross domain access allowed.");
83 HttpSession hs = req.getSession();
84 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
86 hs.setAttribute(LOGGEDIN, null);
89 resp.sendRedirect("/");
93 final Page p = getPage(req.getPathInfo());
96 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
97 String request = req.getPathInfo();
98 request = request.split("\\?")[0];
99 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
100 resp.sendRedirect("/login");
103 if (p.beforeTemplate(req, resp)) {
106 HashMap<String, Object> vars = new HashMap<String, Object>();
108 resp.setContentType("text/html; charset=utf-8");
109 Outputable content = new Outputable() {
112 public void output(PrintWriter out, Language l,
113 Map<String, Object> vars) {
115 if (req.getMethod().equals("POST")) {
120 } catch (IOException e) {
127 vars.put("title", p.getTitle());
128 vars.put("static", ServerConstants.getStaticHostNamePort());
129 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
130 vars.put("content", content);
131 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
133 resp.sendError(404, "Page not found.");
137 private Page getPage(String pathInfo) {
138 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
139 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
141 Page page = pages.get(pathInfo);
145 page = pages.get(pathInfo + "/*");
149 int idx = pathInfo.lastIndexOf('/');
150 pathInfo = pathInfo.substring(0, idx);
152 page = pages.get(pathInfo + "/*");
160 public static void addXSSHeaders(HttpServletResponse hsr) {
161 hsr.addHeader("Access-Control-Allow-Origin", "https://"
162 + ServerConstants.getWwwHostNamePort() + " https://"
163 + ServerConstants.getSecureHostNamePort());
164 hsr.addHeader("Access-Control-Max-Age", "60");
166 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
167 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
170 private static String defaultCSP = null;
171 private static String getDefaultCSP() {
172 if (defaultCSP == null) {
173 StringBuffer csp = new StringBuffer();
174 csp.append("default-src 'none';");
175 csp.append("font-src https://"
176 + ServerConstants.getStaticHostNamePort());
177 csp.append(";img-src https://"
178 + ServerConstants.getStaticHostNamePort());
179 csp.append(";media-src 'none'; object-src 'none';");
180 csp.append("script-src https://"
181 + ServerConstants.getStaticHostNamePort());
182 csp.append(";style-src https://"
183 + ServerConstants.getStaticHostNamePort());
184 csp.append(";form-action https://"
185 + ServerConstants.getSecureHostNamePort() + " https://"
186 + ServerConstants.getWwwHostNamePort());
187 csp.append("report-url https://api.cacert.org/security/csp/report");
188 defaultCSP = csp.toString();