1 package org.cacert.gigi.pages;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.HashMap;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
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;
18 public class PasswordResetPage extends Page {
20 public static final String PATH = "/passwordReset";
22 public PasswordResetPage() {
23 super("Password Reset");
26 public static class PasswordResetForm extends Form {
28 private static Template t = new Template(PasswordResetForm.class.getResource("PasswordResetForm.templ"));
34 public PasswordResetForm(HttpServletRequest hsr) throws GigiApiException {
36 id = Integer.parseInt(hsr.getParameter("id"));
37 u = User.getResetWithToken(id, hsr.getParameter("token"));
39 throw new GigiApiException("User missing or token invalid");
45 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
46 String p1 = req.getParameter("pword1");
47 String p2 = req.getParameter("pword2");
48 String tok = req.getParameter("private_token");
49 if (p1 == null || p2 == null || tok == null) {
50 throw new GigiApiException("Missing form parameter.");
52 if ( !p1.equals(p2)) {
53 throw new GigiApiException("New passwords differ.");
55 u.consumePasswordResetTicket(id, tok, p1);
60 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
62 t.output(out, l, vars);
68 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
69 PasswordResetForm form = Form.getForm(req, PasswordResetForm.class);
71 form.submit(resp.getWriter(), req);
72 resp.getWriter().println(getLanguage(req).getTranslation("Password reset successful."));
74 } catch (GigiApiException e) {
75 e.format(resp.getWriter(), getLanguage(req));
77 form.output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
81 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
83 new PasswordResetForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
84 } catch (GigiApiException e) {
85 e.format(resp.getWriter(), getLanguage(req));
90 public boolean isPermitted(AuthorizationContext ac) {