]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/PasswordResetPage.java
6c1b24ec2f2c3ccf34c0ccdb30b9f760f1c7e98f
[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.net.URLEncoder;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11
12 import org.cacert.gigi.GigiApiException;
13 import org.cacert.gigi.database.GigiPreparedStatement;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Form;
17 import org.cacert.gigi.output.template.MailTemplate;
18 import org.cacert.gigi.output.template.Template;
19 import org.cacert.gigi.util.AuthorizationContext;
20 import org.cacert.gigi.util.RandomToken;
21 import org.cacert.gigi.util.ServerConstants;
22
23 public class PasswordResetPage extends Page {
24
25     public static final int HOUR_MAX = 96;
26
27     public static final String PATH = "/passwordReset";
28
29     public PasswordResetPage() {
30         super("Password Reset");
31     }
32
33     public static class PasswordResetForm extends Form {
34
35         private static final Template t = new Template(PasswordResetForm.class.getResource("PasswordResetForm.templ"));
36
37         private User u;
38
39         private int id;
40
41         public PasswordResetForm(HttpServletRequest hsr) throws GigiApiException {
42             super(hsr, PATH);
43             String idS = hsr.getParameter("id");
44             String tokS = hsr.getParameter("token");
45             if (idS == null || tokS == null) {
46                 throw new GigiApiException("requires id and token");
47             }
48             try {
49                 id = Integer.parseInt(idS);
50             } catch (NumberFormatException e) {
51                 throw new GigiApiException("requires id to be integer");
52             }
53             u = User.getResetWithToken(id, tokS);
54             if (u == null) {
55                 throw new GigiApiException("User missing or token invalid");
56             }
57
58         }
59
60         @Override
61         public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
62             try (GigiPreparedStatement passwordReset = new GigiPreparedStatement("UPDATE `passwordResetTickets` SET `used` = CURRENT_TIMESTAMP WHERE `used` IS NULL AND `created` < CURRENT_TIMESTAMP - interval '1 hours' * ?;")) {
63                 passwordReset.setInt(1, HOUR_MAX);
64                 passwordReset.execute();
65             }
66
67             String p1 = req.getParameter("pword1");
68             String p2 = req.getParameter("pword2");
69             String tok = req.getParameter("private_token");
70             if (p1 == null || p2 == null || tok == null) {
71                 throw new GigiApiException("Missing form parameter.");
72             }
73             if ( !p1.equals(p2)) {
74                 throw new GigiApiException("New passwords differ.");
75             }
76             u.consumePasswordResetTicket(id, tok, p1);
77             return true;
78         }
79
80         @Override
81         protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
82
83             t.output(out, l, vars);
84         }
85
86     }
87
88     @Override
89     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
90         PasswordResetForm form = Form.getForm(req, PasswordResetForm.class);
91         if (form.submitProtected(resp.getWriter(), req)) {
92             resp.getWriter().println(getLanguage(req).getTranslation("Password reset successful."));
93             return;
94         }
95     }
96
97     @Override
98     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
99         try {
100             new PasswordResetForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
101         } catch (GigiApiException e) {
102             e.format(resp.getWriter(), getLanguage(req));
103         }
104     }
105
106     @Override
107     public boolean isPermitted(AuthorizationContext ac) {
108         return true;
109     }
110
111     private static final MailTemplate passwordResetMail = new MailTemplate(PasswordResetPage.class.getResource("PasswordResetMail.templ"));
112
113     public static void initPasswordResetProcess(PrintWriter out, User targetUser, HttpServletRequest req, String aword, Language l, String method, String subject) {
114         String ptok = RandomToken.generateToken(32);
115         int id = targetUser.generatePasswordResetTicket(Page.getUser(req), ptok, aword);
116         try {
117             HashMap<String, Object> vars = new HashMap<>();
118             vars.put("subject", subject);
119             vars.put("method", method);
120             vars.put("link", "https://" + ServerConstants.getWwwHostNamePortSecure() + PasswordResetPage.PATH //
121                     + "?id=" + id + "&token=" + URLEncoder.encode(ptok, "UTF-8"));
122             vars.put("hour_max", HOUR_MAX);
123
124             passwordResetMail.sendMail(l, vars, Page.getUser(req).getEmail());
125             out.println(Page.getLanguage(req).getTranslation("Password reset successful."));
126         } catch (IOException e) {
127             e.printStackTrace();
128         }
129
130     }
131 }