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.MailAdd;
29 import org.cacert.gigi.pages.account.MailCertificates;
30 import org.cacert.gigi.pages.account.MailOverview;
31 import org.cacert.gigi.pages.account.MyDetails;
32 import org.cacert.gigi.pages.main.RegisterPage;
33 import org.cacert.gigi.pages.wot.AssurePage;
34 import org.cacert.gigi.util.ServerConstants;
36 public class Gigi extends HttpServlet {
37 public static final String LOGGEDIN = "loggedin";
38 public static final String USER = "user";
39 private static final long serialVersionUID = -6386785421902852904L;
40 private Template baseTemplate;
41 private HashMap<String, Page> pages = new HashMap<String, Page>();
44 public Gigi(Properties conf) {
45 EmailProvider.init(conf);
46 DatabaseConnection.init(conf);
49 public void init() throws ServletException {
50 pages.put("/login", new LoginPage("CACert - Login"));
51 pages.put("/", new MainPage("CACert - Home"));
52 pages.put("/secure", new TestSecure());
53 pages.put(Verify.PATH, new Verify());
54 pages.put(AssurePage.PATH + "/*", new AssurePage());
55 pages.put(MailCertificates.PATH, new MailCertificates());
56 pages.put(MyDetails.PATH, new MyDetails());
57 pages.put(RegisterPage.PATH, new RegisterPage());
58 pages.put(MailOverview.DEFAULT_PATH, new MailOverview(
59 "My email addresses"));
60 pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
61 baseTemplate = new Template(new InputStreamReader(
62 Gigi.class.getResourceAsStream("Gigi.templ")));
63 m = new Menu("Certificates", "cert", new MenuItem(
64 MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("",
65 "Client Certificates"), new MenuItem("", "Domains"),
66 new MenuItem("", "Server Certificates"));
71 protected void service(final HttpServletRequest req,
72 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,
109 Map<String, Object> vars) {
111 if (req.getMethod().equals("POST")) {
116 } catch (IOException e) {
123 vars.put("title", p.getTitle());
124 vars.put("static", ServerConstants.getStaticHostNamePort());
125 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
126 vars.put("content", content);
127 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
129 resp.sendError(404, "Page not found.");
133 private Page getPage(String pathInfo) {
134 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
135 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
137 Page page = pages.get(pathInfo);
141 page = pages.get(pathInfo + "/*");
145 int idx = pathInfo.lastIndexOf('/');
146 pathInfo = pathInfo.substring(0, idx);
148 page = pages.get(pathInfo + "/*");
156 public static void addXSSHeaders(HttpServletResponse hsr) {
157 hsr.addHeader("Access-Control-Allow-Origin", "https://"
158 + ServerConstants.getWwwHostNamePort() + " https://"
159 + ServerConstants.getSecureHostNamePort());
160 hsr.addHeader("Access-Control-Max-Age", "60");
162 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
163 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
166 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://"
172 + ServerConstants.getStaticHostNamePort());
173 csp.append(";img-src https://"
174 + ServerConstants.getStaticHostNamePort());
175 csp.append(";media-src 'none'; object-src 'none';");
176 csp.append("script-src https://"
177 + ServerConstants.getStaticHostNamePort());
178 csp.append(";style-src https://"
179 + ServerConstants.getStaticHostNamePort());
180 csp.append(";form-action https://"
181 + ServerConstants.getSecureHostNamePort() + " https://"
182 + ServerConstants.getWwwHostNamePort());
183 csp.append("report-url https://api.cacert.org/security/csp/report");
184 defaultCSP = csp.toString();