]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssurePage.java
Implement CSRF check on "Assure someone"
[gigi.git] / src / org / cacert / gigi / pages / wot / AssurePage.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.IOException;
4 import java.io.InputStreamReader;
5 import java.io.PrintWriter;
6 import java.sql.PreparedStatement;
7 import java.sql.ResultSet;
8 import java.sql.SQLException;
9 import java.util.HashMap;
10
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.HttpSession;
14
15 import org.cacert.gigi.User;
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.output.DateSelector;
18 import org.cacert.gigi.output.Template;
19 import org.cacert.gigi.output.Form.CSRFError;
20 import org.cacert.gigi.pages.LoginPage;
21 import org.cacert.gigi.pages.Page;
22 import org.cacert.gigi.util.Notary;
23
24 public class AssurePage extends Page {
25         public static final String PATH = "/wot/assure";
26         public static final String SESSION = "/wot/assure/FORM";
27         DateSelector ds = new DateSelector("day", "month", "year");
28         Template t;
29
30         public AssurePage() {
31                 super("Assure someone");
32                 t = new Template(new InputStreamReader(
33                                 AssuranceForm.class.getResourceAsStream("AssureeSearch.templ")));
34
35         }
36
37         @Override
38         public void doGet(HttpServletRequest req, HttpServletResponse resp)
39                         throws IOException {
40
41                 PrintWriter out = resp.getWriter();
42                 String pi = req.getPathInfo().substring(PATH.length());
43                 if (pi.length() > 1) {
44                         User myself = LoginPage.getUser(req);
45                         int mid = Integer.parseInt(pi.substring(1));
46
47                         if (!Notary.checkAssuranceIsPossible(myself, new User(mid), out)) {
48                                 return;
49                         }
50                         HttpSession hs = req.getSession();
51                         AssuranceForm form = (AssuranceForm) hs.getAttribute(SESSION);
52                         if (form == null || form.assuree.getId() != mid) {
53                                 form = new AssuranceForm(mid);
54                                 hs.setAttribute(SESSION, form);
55                         }
56
57                         form.output(out, getLanguage(req), new HashMap<String, Object>());;
58                 } else {
59                         HashMap<String, Object> vars = new HashMap<String, Object>();
60                         vars.put("DoB", ds);
61                         t.output(out, getLanguage(req), vars);
62                 }
63         }
64         @Override
65         public void doPost(HttpServletRequest req, HttpServletResponse resp)
66                         throws IOException {
67                 PrintWriter out = resp.getWriter();
68                 String pi = req.getPathInfo().substring(PATH.length());
69                 if (pi.length() > 1) {
70                         User myself = LoginPage.getUser(req);
71                         int mid = Integer.parseInt(pi.substring(1));
72                         if (mid == myself.getId()) {
73                                 out.println("Cannot assure myself.");
74                                 return;
75                         }
76
77                         AssuranceForm form = (AssuranceForm) req.getSession().getAttribute(
78                                         SESSION);
79                         if (form == null) {
80                                 out.println("No form found. This is an Error. Fill in the form again.");
81                                 return;
82                         }
83                         try {
84                                 form.submit(out, req);
85                         } catch (CSRFError e) {
86                                 resp.sendError(500, "CSRF Failed");
87                                 out.println(translate(req, "CSRF Token failed."));
88                         }
89
90                         return;
91                 }
92
93                 System.out.println("searching for");
94                 ResultSet rs = null;
95                 try {
96                         PreparedStatement ps = DatabaseConnection
97                                         .getInstance()
98                                         .prepare(
99                                                         "SELECT id, verified FROM users WHERE email=? AND dob=? AND deleted=0");
100                         ps.setString(1, req.getParameter("email"));
101                         String day = req.getParameter("year") + "-"
102                                         + req.getParameter("month") + "-" + req.getParameter("day");
103                         ps.setString(2, day);
104                         rs = ps.executeQuery();
105                         int id = 0;
106                         if (rs.next()) {
107                                 id = rs.getInt(1);
108                                 int verified = rs.getInt(2);
109                                 if (rs.next()) {
110                                         out.println("Error, ambigous user. Please contact support@cacert.org.");
111                                 } else {
112                                         if (verified == 0) {
113                                                 out.println(translate(req,
114                                                                 "User is not yet verified. Please try again in 24 hours!"));
115                                         }
116                                         resp.sendRedirect(PATH + "/" + id);
117                                 }
118                         } else {
119                                 out.print("<div class='formError'>");
120
121                                 out.println(translate(
122                                                 req,
123                                                 "I'm sorry, there was no email and date of birth matching"
124                                                                 + " what you entered in the system. Please double check"
125                                                                 + " your information."));
126                                 out.print("</div>");
127                         }
128
129                         rs.close();
130                 } catch (SQLException e) {
131                         e.printStackTrace();
132                 } finally {
133                         try {
134                                 if (rs != null) {
135                                         rs.close();
136                                 }
137                         } catch (SQLException e) {
138                                 e.printStackTrace();
139                         }
140                 }
141         }
142 }