1 package org.cacert.gigi;
3 import java.io.BufferedReader;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.util.Calendar;
9 import java.util.HashMap;
10 import java.util.Properties;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
18 import org.cacert.gigi.database.DatabaseConnection;
19 import org.cacert.gigi.email.EmailProvider;
20 import org.cacert.gigi.pages.LoginPage;
21 import org.cacert.gigi.pages.MainPage;
22 import org.cacert.gigi.pages.Page;
23 import org.cacert.gigi.pages.TestSecure;
24 import org.cacert.gigi.pages.Verify;
25 import org.cacert.gigi.pages.account.MailAdd;
26 import org.cacert.gigi.pages.account.MailCertificates;
27 import org.cacert.gigi.pages.account.MailOverview;
28 import org.cacert.gigi.pages.account.MyDetails;
29 import org.cacert.gigi.pages.main.RegisterPage;
30 import org.cacert.gigi.pages.wot.AssurePage;
31 import org.cacert.gigi.util.ServerConstants;
32 import org.eclipse.jetty.util.log.Log;
34 public class Gigi extends HttpServlet {
35 public static final String LOGGEDIN = "loggedin";
36 public static final String USER = "user";
37 private static final long serialVersionUID = -6386785421902852904L;
38 private String[] baseTemplate;
39 private HashMap<String, Page> pages = new HashMap<String, Page>();
41 public Gigi(Properties conf) {
42 EmailProvider.init(conf);
43 DatabaseConnection.init(conf);
46 public void init() throws ServletException {
47 pages.put("/login", new LoginPage("CACert - Login"));
48 pages.put("/", new MainPage("CACert - Home"));
49 pages.put("/secure", new TestSecure());
50 pages.put(Verify.PATH, new Verify());
51 pages.put(AssurePage.PATH + "/*", new AssurePage());
52 pages.put(MailCertificates.PATH, new MailCertificates());
53 pages.put(MyDetails.PATH, new MyDetails());
54 pages.put(RegisterPage.PATH, new RegisterPage());
55 pages.put(MailOverview.DEFAULT_PATH, new MailOverview(
56 "My email addresses"));
57 pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
59 try (BufferedReader reader = new BufferedReader(new InputStreamReader(
60 new FileInputStream(new File("templates/base.html"))))) {
62 while ((tmp = reader.readLine()) != null) {
65 baseTemplate = templ.split("\\$content\\$");
66 } catch (Exception e) {
67 Log.getLogger(Gigi.class).warn("Error loading template!", e);
73 protected void service(HttpServletRequest req, HttpServletResponse resp)
74 throws ServletException, IOException {
76 if (req.getHeader("Origin") != null) {
77 resp.getWriter().println("No cross domain access allowed.");
80 HttpSession hs = req.getSession();
81 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
83 hs.setAttribute(LOGGEDIN, null);
86 resp.sendRedirect("/");
90 Page p = getPage(req.getPathInfo());
93 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
94 String request = req.getPathInfo();
95 request = request.split("\\?")[0];
96 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
97 resp.sendRedirect("/login");
100 if (p.beforeTemplate(req, resp)) {
104 String b0 = baseTemplate[0];
105 b0 = makeDynTempl(b0, p);
106 resp.setContentType("text/html; charset=utf-8");
107 resp.getWriter().print(b0);
108 if (req.getMethod().equals("POST")) {
113 String b1 = baseTemplate[1];
114 b1 = makeDynTempl(b1, p);
115 resp.getWriter().print(b1);
117 resp.sendError(404, "Page not found.");
121 private Page getPage(String pathInfo) {
122 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
123 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
125 Page page = pages.get(pathInfo);
129 page = pages.get(pathInfo + "/*");
133 int idx = pathInfo.lastIndexOf('/');
134 pathInfo = pathInfo.substring(0, idx);
136 page = pages.get(pathInfo + "/*");
143 private String makeDynTempl(String in, Page p) {
144 int year = Calendar.getInstance().get(Calendar.YEAR);
145 in = in.replaceAll("\\$title\\$", p.getTitle());
146 in = in.replaceAll("\\$year\\$", year + "");
149 public static void addXSSHeaders(HttpServletResponse hsr) {
150 hsr.addHeader("Access-Control-Allow-Origin",
151 "http://cacert.org https://localhost");
152 hsr.addHeader("Access-Control-Max-Age", "60");
153 hsr.addHeader("Content-Security-Policy", "default-src 'self' https://"
154 + ServerConstants.getStaticHostNamePort()
155 + " https://www.cacert.org/*;frame-ancestors 'none'");
156 // ;report-uri https://felix.dogcraft.de/report.php