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