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