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.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);
50 public void init() throws ServletException {
51 pages.put("/login", new LoginPage("CACert - Login"));
52 pages.put("/", new MainPage("CACert - Home"));
53 pages.put("/secure", new TestSecure());
54 pages.put(Verify.PATH, new Verify());
55 pages.put(AssurePage.PATH + "/*", new AssurePage());
56 pages.put(MailCertificates.PATH, new MailCertificates());
57 pages.put(MyDetails.PATH, new MyDetails());
58 pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
59 pages.put(RegisterPage.PATH, new RegisterPage());
60 pages.put(MailOverview.DEFAULT_PATH, new MailOverview(
61 "My email addresses"));
62 pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
63 baseTemplate = new Template(new InputStreamReader(
64 Gigi.class.getResourceAsStream("Gigi.templ")));
65 m = new Menu("Certificates", "cert", new MenuItem(
66 MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("",
67 "Client Certificates"), new MenuItem("", "Domains"),
68 new MenuItem("", "Server Certificates"));
73 protected void service(final HttpServletRequest req,
74 final HttpServletResponse resp) throws ServletException,
77 if (req.getHeader("Origin") != null) {
78 resp.getWriter().println("No cross domain access allowed.");
81 HttpSession hs = req.getSession();
82 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
84 hs.setAttribute(LOGGEDIN, null);
87 resp.sendRedirect("/");
91 final Page p = getPage(req.getPathInfo());
94 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
95 String request = req.getPathInfo();
96 request = request.split("\\?")[0];
97 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
98 resp.sendRedirect("/login");
101 if (p.beforeTemplate(req, resp)) {
104 HashMap<String, Object> vars = new HashMap<String, Object>();
106 resp.setContentType("text/html; charset=utf-8");
107 Outputable content = new Outputable() {
110 public void output(PrintWriter out, Language l,
111 Map<String, Object> vars) {
113 if (req.getMethod().equals("POST")) {
118 } catch (IOException e) {
125 vars.put("title", p.getTitle());
126 vars.put("static", ServerConstants.getStaticHostNamePort());
127 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
128 vars.put("content", content);
129 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
131 resp.sendError(404, "Page not found.");
135 private Page getPage(String pathInfo) {
136 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
137 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
139 Page page = pages.get(pathInfo);
143 page = pages.get(pathInfo + "/*");
147 int idx = pathInfo.lastIndexOf('/');
148 pathInfo = pathInfo.substring(0, idx);
150 page = pages.get(pathInfo + "/*");
158 public static void addXSSHeaders(HttpServletResponse hsr) {
159 hsr.addHeader("Access-Control-Allow-Origin", "https://"
160 + ServerConstants.getWwwHostNamePort() + " https://"
161 + ServerConstants.getSecureHostNamePort());
162 hsr.addHeader("Access-Control-Max-Age", "60");
164 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
165 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
168 private static String defaultCSP = null;
169 private static String getDefaultCSP() {
170 if (defaultCSP == null) {
171 StringBuffer csp = new StringBuffer();
172 csp.append("default-src 'none';");
173 csp.append("font-src https://"
174 + ServerConstants.getStaticHostNamePort());
175 csp.append(";img-src https://"
176 + ServerConstants.getStaticHostNamePort());
177 csp.append(";media-src 'none'; object-src 'none';");
178 csp.append("script-src https://"
179 + ServerConstants.getStaticHostNamePort());
180 csp.append(";style-src https://"
181 + ServerConstants.getStaticHostNamePort());
182 csp.append(";form-action https://"
183 + ServerConstants.getSecureHostNamePort() + " https://"
184 + ServerConstants.getWwwHostNamePort());
185 csp.append("report-url https://api.cacert.org/security/csp/report");
186 defaultCSP = csp.toString();