]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
Draft for "Add mail certificate"
[gigi.git] / src / org / cacert / gigi / Gigi.java
1 package org.cacert.gigi;
2
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;
8 import java.util.Map;
9 import java.util.Properties;
10
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;
16
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;
37
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>();
44         Menu m;
45
46         public Gigi(Properties conf) {
47                 EmailProvider.init(conf);
48                 DatabaseConnection.init(conf);
49         }
50         @Override
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"));
71                 super.init();
72
73         }
74         @Override
75         protected void service(final HttpServletRequest req,
76                         final HttpServletResponse resp) throws ServletException,
77                         IOException {
78                 addXSSHeaders(resp);
79                 if (req.getHeader("Origin") != null) {
80                         resp.getWriter().println("No cross domain access allowed.");
81                         return;
82                 }
83                 HttpSession hs = req.getSession();
84                 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
85                         if (hs != null) {
86                                 hs.setAttribute(LOGGEDIN, null);
87                                 hs.invalidate();
88                         }
89                         resp.sendRedirect("/");
90                         return;
91                 }
92
93                 final Page p = getPage(req.getPathInfo());
94                 if (p != null) {
95
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");
101                                 return;
102                         }
103                         if (p.beforeTemplate(req, resp)) {
104                                 return;
105                         }
106                         HashMap<String, Object> vars = new HashMap<String, Object>();
107
108                         resp.setContentType("text/html; charset=utf-8");
109                         Outputable content = new Outputable() {
110
111                                 @Override
112                                 public void output(PrintWriter out, Language l,
113                                                 Map<String, Object> vars) {
114                                         try {
115                                                 if (req.getMethod().equals("POST")) {
116                                                         p.doPost(req, resp);
117                                                 } else {
118                                                         p.doGet(req, resp);
119                                                 }
120                                         } catch (IOException e) {
121                                                 e.printStackTrace();
122                                         }
123
124                                 }
125                         };
126                         vars.put("menu", m);
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);
132                 } else {
133                         resp.sendError(404, "Page not found.");
134                 }
135
136         }
137         private Page getPage(String pathInfo) {
138                 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
139                         pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
140                 }
141                 Page page = pages.get(pathInfo);
142                 if (page != null) {
143                         return page;
144                 }
145                 page = pages.get(pathInfo + "/*");
146                 if (page != null) {
147                         return page;
148                 }
149                 int idx = pathInfo.lastIndexOf('/');
150                 pathInfo = pathInfo.substring(0, idx);
151
152                 page = pages.get(pathInfo + "/*");
153                 if (page != null) {
154                         return page;
155                 }
156                 return null;
157
158         }
159
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");
165
166                 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
167                 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
168
169         }
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();
189                 }
190                 return defaultCSP;
191         }
192 }