]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/PasswordResetPage.java
fix: circumvent several exceptions
[gigi.git] / src / org / cacert / gigi / pages / PasswordResetPage.java
1 package org.cacert.gigi.pages;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.User;
13 import org.cacert.gigi.localisation.Language;
14 import org.cacert.gigi.output.template.Form;
15 import org.cacert.gigi.output.template.Template;
16 import org.cacert.gigi.util.AuthorizationContext;
17
18 public class PasswordResetPage extends Page {
19
20     public static final String PATH = "/passwordReset";
21
22     public PasswordResetPage() {
23         super("Password Reset");
24     }
25
26     public static class PasswordResetForm extends Form {
27
28         private static Template t = new Template(PasswordResetForm.class.getResource("PasswordResetForm.templ"));
29
30         private User u;
31
32         private int id;
33
34         public PasswordResetForm(HttpServletRequest hsr) throws GigiApiException {
35             super(hsr, PATH);
36             String idS = hsr.getParameter("id");
37             String tokS = hsr.getParameter("token");
38             if (idS == null || tokS == null) {
39                 throw new GigiApiException("requires id and token");
40             }
41             try {
42                 id = Integer.parseInt(idS);
43             } catch (NumberFormatException e) {
44                 throw new GigiApiException("requires id to be integer");
45             }
46             u = User.getResetWithToken(id, tokS);
47             if (u == null) {
48                 throw new GigiApiException("User missing or token invalid");
49             }
50
51         }
52
53         @Override
54         public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
55             String p1 = req.getParameter("pword1");
56             String p2 = req.getParameter("pword2");
57             String tok = req.getParameter("private_token");
58             if (p1 == null || p2 == null || tok == null) {
59                 throw new GigiApiException("Missing form parameter.");
60             }
61             if ( !p1.equals(p2)) {
62                 throw new GigiApiException("New passwords differ.");
63             }
64             u.consumePasswordResetTicket(id, tok, p1);
65             return true;
66         }
67
68         @Override
69         protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
70
71             t.output(out, l, vars);
72         }
73
74     }
75
76     @Override
77     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
78         PasswordResetForm form = Form.getForm(req, PasswordResetForm.class);
79         try {
80             form.submit(resp.getWriter(), req);
81             resp.getWriter().println(getLanguage(req).getTranslation("Password reset successful."));
82             return;
83         } catch (GigiApiException e) {
84             e.format(resp.getWriter(), getLanguage(req));
85         }
86         form.output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
87     }
88
89     @Override
90     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
91         try {
92             new PasswordResetForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
93         } catch (GigiApiException e) {
94             e.format(resp.getWriter(), getLanguage(req));
95         }
96     }
97
98     @Override
99     public boolean isPermitted(AuthorizationContext ac) {
100         return true;
101     }
102 }