]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
Implement CSRF check on "Assure someone"
[gigi.git] / src / org / cacert / gigi / pages / wot / AssuranceForm.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.InputStreamReader;
4 import java.io.PrintWriter;
5 import java.sql.SQLException;
6 import java.text.ParseException;
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9 import java.util.HashMap;
10 import java.util.Map;
11
12 import javax.servlet.http.HttpServletRequest;
13
14 import org.cacert.gigi.Language;
15 import org.cacert.gigi.User;
16 import org.cacert.gigi.output.Form;
17 import org.cacert.gigi.output.Template;
18 import org.cacert.gigi.pages.LoginPage;
19 import org.cacert.gigi.util.Notary;
20
21 public class AssuranceForm extends Form {
22         User assuree;
23         static final Template templ;
24         static {
25                 templ = new Template(new InputStreamReader(
26                                 AssuranceForm.class.getResourceAsStream("AssuranceForm.templ")));
27         }
28
29         public AssuranceForm(int assuree) {
30                 this.assuree = new User(assuree);
31         }
32         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
33
34         @Override
35         public void outputContent(PrintWriter out, Language l,
36                         Map<String, Object> vars) {
37                 HashMap<String, Object> res = new HashMap<String, Object>();
38                 res.putAll(vars);
39                 res.put("name", assuree.getName());
40                 try {
41                         res.put("maxpoints", assuree.getMaxAssurePoints());
42                 } catch (SQLException e) {
43                         e.printStackTrace();
44                 }
45                 res.put("dob", sdf.format(assuree.getDob()));
46                 templ.output(out, l, res);
47         }
48
49         @Override
50         public boolean submit(PrintWriter out, HttpServletRequest req) {
51                 checkCSRF(req);
52
53                 out.println("<div class='formError'>");
54                 boolean failed = false;
55
56                 if (!"1".equals(req.getParameter("certify"))
57                                 || !"1".equals(req.getParameter("rules"))
58                                 || !"1".equals(req.getParameter("CCAAgreed"))
59                                 || !"1".equals(req.getParameter("assertion"))) {
60                         outputError(out, req, "You failed to check all boxes to validate"
61                                         + " your adherence to the rules and policies of CAcert");
62                         failed = true;
63
64                 }
65                 if (req.getParameter("date") == null
66                                 || req.getParameter("date").equals("")) {
67                         outputError(out, req,
68                                         "You must enter the date when you met the assuree.");
69                         failed = true;
70                 } else {
71                         try {
72                                 Date d = sdf.parse(req.getParameter("date"));
73                                 if (d.getTime() > System.currentTimeMillis()) {
74                                         outputError(out, req,
75                                                         "You must not enter a date in the future.");
76                                         failed = true;
77                                 }
78                         } catch (ParseException e) {
79                                 outputError(out, req,
80                                                 "You must enter the date in this format: YYYY-MM-DD.");
81                                 failed = true;
82                         }
83                 }
84                 // check location, min 3 characters
85                 if (req.getParameter("location") == null
86                                 || req.getParameter("location").equals("")) {
87                         outputError(out, req,
88                                         "You failed to enter a location of your meeting.");
89                         failed = true;
90                 } else if (req.getParameter("location").length() <= 2) {
91                         outputError(out, req,
92                                         "You must enter a location with at least 3 characters eg town and country.");
93                         failed = true;
94                 }
95                 // TODO checkPoints
96                 String points = req.getParameter("points");
97                 if (points == null || "".equals(points)) {
98                         // TODO message
99                         failed = true;
100                 }
101                 if (failed) {
102                         out.println("</div>");
103                         return false;
104                 }
105                 try {
106                         boolean success = Notary.assure(LoginPage.getUser(req), assuree,
107                                         Integer.parseInt(req.getParameter("points")),
108                                         req.getParameter("location"), req.getParameter("date"));
109                         if (!success) {
110                                 outputError(out, req,
111                                                 "Assurance failed. Maybe user data changed.");
112                         }
113                         out.println("</div>");
114                         return success;
115                 } catch (SQLException e) {
116                         e.printStackTrace();
117                 }
118
119                 out.println("</div>");
120                 return false;
121         }
122
123 }