]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/PasswordResetPage.java
upd: transform existing mails into mail templates
[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         try {
92             form.submit(resp.getWriter(), req);
93             resp.getWriter().println(getLanguage(req).getTranslation("Password reset successful."));
94             return;
95         } catch (GigiApiException e) {
96             e.format(resp.getWriter(), getLanguage(req));
97         }
98         form.output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
99     }
100
101     @Override
102     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
103         try {
104             new PasswordResetForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
105         } catch (GigiApiException e) {
106             e.format(resp.getWriter(), getLanguage(req));
107         }
108     }
109
110     @Override
111     public boolean isPermitted(AuthorizationContext ac) {
112         return true;
113     }
114
115     private static final MailTemplate passwordResetMail = new MailTemplate(PasswordResetPage.class.getResource("PasswordResetMail.templ"));
116
117     public static void initPasswordResetProcess(PrintWriter out, User targetUser, HttpServletRequest req, String aword, Language l, String method, String subject) {
118         String ptok = RandomToken.generateToken(32);
119         int id = targetUser.generatePasswordResetTicket(Page.getUser(req), ptok, aword);
120         try {
121             HashMap<String, Object> vars = new HashMap<>();
122             vars.put("subject", subject);
123             vars.put("method", method);
124             vars.put("link", "https://" + ServerConstants.getWwwHostNamePortSecure() + PasswordResetPage.PATH //
125                     + "?id=" + id + "&token=" + URLEncoder.encode(ptok, "UTF-8"));
126             vars.put("hour_max", HOUR_MAX);
127
128             passwordResetMail.sendMail(l, vars, Page.getUser(req).getEmail());
129             out.println(Page.getLanguage(req).getTranslation("Password reset successful."));
130         } catch (IOException e) {
131             e.printStackTrace();
132         }
133
134     }
135 }