X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FGigi.java;h=fa259c96c77cbf99835c8df72e374c863935bd3e;hp=7aa26d87ef70a0752907f818339c07f60d260318;hb=d690eda36eba121aa79e4f456d5f0eb481be8b86;hpb=3ad481bf50a3562142f3acd882e669acc9bc3f88 diff --git a/src/org/cacert/gigi/Gigi.java b/src/org/cacert/gigi/Gigi.java index 7aa26d87..fa259c96 100644 --- a/src/org/cacert/gigi/Gigi.java +++ b/src/org/cacert/gigi/Gigi.java @@ -1,7 +1,13 @@ package org.cacert.gigi; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; -import java.security.cert.X509Certificate; +import java.io.InputStreamReader; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -9,59 +15,145 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; +import org.cacert.gigi.database.DatabaseConnection; +import org.cacert.gigi.email.EmailProvider; +import org.cacert.gigi.pages.LoginPage; +import org.cacert.gigi.pages.MainPage; +import org.cacert.gigi.pages.Page; +import org.cacert.gigi.pages.TestSecure; +import org.cacert.gigi.pages.Verify; +import org.cacert.gigi.pages.account.MailAdd; +import org.cacert.gigi.pages.account.MailCertificates; +import org.cacert.gigi.pages.account.MailOverview; +import org.cacert.gigi.pages.account.MyDetails; +import org.cacert.gigi.pages.main.RegisterPage; +import org.cacert.gigi.pages.wot.AssurePage; +import org.cacert.gigi.util.ServerConstants; +import org.eclipse.jetty.util.log.Log; + public class Gigi extends HttpServlet { + public static final String LOGGEDIN = "loggedin"; + public static final String USER = "user"; + private static final long serialVersionUID = -6386785421902852904L; + private String[] baseTemplate; + private HashMap pages = new HashMap(); + + public Gigi(Properties conf) { + EmailProvider.init(conf); + DatabaseConnection.init(conf); + } @Override - protected void service(HttpServletRequest req, HttpServletResponse resp) - throws ServletException, IOException { - X509Certificate[] cert = (X509Certificate[]) req - .getAttribute("javax.servlet.request.X509Certificate"); - HttpSession hs = req.getSession(false); - if (hs == null || !((Boolean) hs.getAttribute("loggedin"))) { - if (cert != null) { - tryAuthWithCertificate(req, cert[0]); - hs = req.getSession(false); + public void init() throws ServletException { + pages.put("/login", new LoginPage("CACert - Login")); + pages.put("/", new MainPage("CACert - Home")); + pages.put("/secure", new TestSecure()); + pages.put(Verify.PATH, new Verify()); + pages.put(AssurePage.PATH + "/*", new AssurePage()); + pages.put(MailCertificates.PATH, new MailCertificates()); + pages.put(MyDetails.PATH, new MyDetails()); + pages.put(RegisterPage.PATH, new RegisterPage()); + pages.put(MailOverview.DEFAULT_PATH, new MailOverview( + "My email addresses")); + pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email")); + String templ = ""; + try (BufferedReader reader = new BufferedReader(new InputStreamReader( + new FileInputStream(new File("templates/base.html"))))) { + String tmp; + while ((tmp = reader.readLine()) != null) { + templ += tmp + "\n"; } + baseTemplate = templ.split("\\$content\\$"); + } catch (Exception e) { + Log.getLogger(Gigi.class).warn("Error loading template!", e); } - if (req.getMethod().equals("POST") && req.getPathInfo() != null - && req.getPathInfo().equals("/login")) { - authWithUnpw(req); - resp.sendRedirect("/"); + super.init(); + + } + @Override + protected void service(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + addXSSHeaders(resp); + if (req.getHeader("Origin") != null) { + resp.getWriter().println("No cross domain access allowed."); return; } + HttpSession hs = req.getSession(); if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) { if (hs != null) { - hs.setAttribute("loggedin", false); + hs.setAttribute(LOGGEDIN, null); hs.invalidate(); } - authWithUnpw(req); resp.sendRedirect("/"); return; } - if (hs == null || !((Boolean) hs.getAttribute("loggedin"))) { - resp.setContentType("text/html"); - resp.getWriter().println("Access denied. Sending login form."); - resp.getWriter() - .println( - "
" - + "" - + "
"); - return; + Page p = getPage(req.getPathInfo()); + if (p != null) { + + if (p.needsLogin() && hs.getAttribute("loggedin") == null) { + String request = req.getPathInfo(); + request = request.split("\\?")[0]; + hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request); + resp.sendRedirect("/login"); + return; + } + if (p.beforeTemplate(req, resp)) { + return; + } + + String b0 = baseTemplate[0]; + b0 = makeDynTempl(b0, p); + resp.setContentType("text/html; charset=utf-8"); + resp.getWriter().print(b0); + if (req.getMethod().equals("POST")) { + p.doPost(req, resp); + } else { + p.doGet(req, resp); + } + String b1 = baseTemplate[1]; + b1 = makeDynTempl(b1, p); + resp.getWriter().print(b1); + } else { + resp.sendError(404, "Page not found."); } - resp.getWriter().println("Access granted."); } - private void authWithUnpw(HttpServletRequest req) { - String un = req.getParameter("username"); - String pw = req.getParameter("password"); - // TODO dummy password check if (un.equals(pw)) { - HttpSession hs = req.getSession(); - hs.setAttribute("loggedin", true); + private Page getPage(String pathInfo) { + if (pathInfo.endsWith("/") && !pathInfo.equals("/")) { + pathInfo = pathInfo.substring(0, pathInfo.length() - 1); + } + Page page = pages.get(pathInfo); + if (page != null) { + return page; + } + page = pages.get(pathInfo + "/*"); + if (page != null) { + return page; + } + int idx = pathInfo.lastIndexOf('/'); + pathInfo = pathInfo.substring(0, idx); + + page = pages.get(pathInfo + "/*"); + if (page != null) { + return page; + } + return null; + } - private void tryAuthWithCertificate(HttpServletRequest req, - X509Certificate x509Certificate) { - // TODO ckeck if certificate is valid - HttpSession hs = req.getSession(); - hs.setAttribute("loggedin", true); + private String makeDynTempl(String in, Page p) { + int year = Calendar.getInstance().get(Calendar.YEAR); + in = in.replaceAll("\\$title\\$", p.getTitle()); + in = in.replaceAll("\\$year\\$", year + ""); + return in; + } + public static void addXSSHeaders(HttpServletResponse hsr) { + hsr.addHeader("Access-Control-Allow-Origin", + "http://cacert.org https://localhost"); + hsr.addHeader("Access-Control-Max-Age", "60"); + hsr.addHeader("Content-Security-Policy", "default-src 'self' https://" + + ServerConstants.getStaticHostNamePort() + + " https://www.cacert.org/*;frame-ancestors 'none'"); + // ;report-uri https://felix.dogcraft.de/report.php + } }