]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/wot/AssuranceForm.java
Merge "Update notes about password security"
[gigi.git] / src / org / cacert / gigi / pages / wot / AssuranceForm.java
1 package org.cacert.gigi.pages.wot;
2
3 import java.io.PrintWriter;
4 import java.text.SimpleDateFormat;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.Map;
9
10 import javax.servlet.http.HttpServletRequest;
11
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.localisation.Language;
17 import org.cacert.gigi.output.template.Form;
18 import org.cacert.gigi.output.template.IterableDataset;
19 import org.cacert.gigi.output.template.Template;
20 import org.cacert.gigi.pages.Page;
21 import org.cacert.gigi.pages.PasswordResetPage;
22 import org.cacert.gigi.util.DayDate;
23 import org.cacert.gigi.util.Notary;
24
25 public class AssuranceForm extends Form {
26
27     private User assuree;
28
29     private Name assureeName;
30
31     private DayDate dob;
32
33     private String location = "";
34
35     private String date = "";
36
37     private String aword;
38
39     private User assurer;
40
41     private AssuranceType type = AssuranceType.FACE_TO_FACE;
42
43     private static final Template templ;
44     static {
45         templ = new Template(AssuranceForm.class.getResource("AssuranceForm.templ"));
46     }
47
48     public AssuranceForm(HttpServletRequest hsr, User assuree) {
49         super(hsr);
50         assurer = Page.getUser(hsr);
51         this.assuree = assuree;
52         assureeName = this.assuree.getName();
53         dob = this.assuree.getDoB();
54     }
55
56     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
57
58     SimpleDateFormat sdf2 = new SimpleDateFormat("dd. MMM yyyy");
59
60     @Override
61     public void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
62         HashMap<String, Object> res = new HashMap<String, Object>();
63         res.putAll(vars);
64         res.put("nameExplicit", assuree.getName());
65         res.put("name", assuree.getName().toString());
66         res.put("maxpoints", assurer.getMaxAssurePoints());
67         res.put("dob", sdf.format(assuree.getDoB().toDate()));
68         res.put("dobFmt2", sdf2.format(assuree.getDoB().toDate()));
69         res.put("location", location);
70         res.put("date", date);
71         res.put("aword", aword);
72         final LinkedList<AssuranceType> ats = new LinkedList<>();
73         for (AssuranceType at : AssuranceType.values()) {
74             try {
75                 Notary.may(assurer, assuree, at);
76                 ats.add(at);
77             } catch (GigiApiException e) {
78             }
79         }
80         res.put("ats", new IterableDataset() {
81
82             Iterator<AssuranceType> t = ats.iterator();
83
84             @Override
85             public boolean next(Language l, Map<String, Object> vars) {
86                 if ( !t.hasNext()) {
87                     return false;
88                 }
89                 AssuranceType t1 = t.next();
90                 vars.put("type", t1.getDescription());
91                 vars.put("id", t1.toString());
92                 vars.put("sel", t1 == type ? " selected" : "");
93                 return true;
94             }
95         });
96         templ.output(out, l, res);
97     }
98
99     @Override
100     public boolean submit(PrintWriter out, HttpServletRequest req) {
101         location = req.getParameter("location");
102         date = req.getParameter("date");
103         if (date == null || location == null) {
104             outputError(out, req, "You need to enter location and date!");
105         }
106
107         if ( !"1".equals(req.getParameter("certify")) || !"1".equals(req.getParameter("rules")) || !"1".equals(req.getParameter("tos_agree")) || !"1".equals(req.getParameter("assertion"))) {
108             outputError(out, req, "You failed to check all boxes to validate" + " your adherence to the rules and policies of SomeCA");
109         }
110         if ("1".equals(req.getParameter("passwordReset"))) {
111             aword = req.getParameter("passwordResetValue");
112             if ("".equals(aword)) {
113                 aword = null;
114             }
115         } else {
116             aword = null;
117         }
118         String val = req.getParameter("assuranceType");
119         if (val != null) {
120             try {
121                 type = AssuranceType.valueOf(val);
122             } catch (IllegalArgumentException e) {
123                 outputError(out, req, "Assurance Type wrong.");
124             }
125         }
126
127         int pointsI = 0;
128         String points = req.getParameter("points");
129         if (points == null || "".equals(points)) {
130             outputError(out, req, "For an assurance, you need to enter points.");
131         } else {
132             try {
133                 pointsI = Integer.parseInt(points);
134             } catch (NumberFormatException e) {
135                 outputError(out, req, "The points entered were not a number.");
136             }
137         }
138
139         if (isFailed(out)) {
140             return false;
141         }
142         try {
143             Notary.assure(assurer, assuree, assureeName, dob, pointsI, location, req.getParameter("date"), type);
144             if (aword != null && !aword.equals("")) {
145                 Language l = Language.getInstance(assuree.getPreferredLocale());
146                 String method = l.getTranslation("A password reset was triggered. If you did a password reset by assurance, please enter your secret password using this form:");
147                 String subject = l.getTranslation("Password reset by assurance");
148                 PasswordResetPage.initPasswordResetProcess(out, assuree, req, aword, l, method, subject);
149             }
150             return true;
151         } catch (GigiApiException e) {
152             e.format(out, Page.getLanguage(req));
153         }
154
155         return false;
156     }
157
158     public User getAssuree() {
159         return assuree;
160     }
161
162 }