]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/Verify.java
Merge remote-tracking branch 'origin/libs/scrypt/local'
[gigi.git] / src / org / cacert / gigi / pages / Verify.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.dbObjects.Domain;
13 import org.cacert.gigi.dbObjects.EmailAddress;
14 import org.cacert.gigi.localisation.Language;
15 import org.cacert.gigi.output.template.Form;
16
17 public class Verify extends Page {
18
19     private class VerificationForm extends Form {
20
21         private String hash;
22
23         private String type;
24
25         private String id;
26
27         public VerificationForm(HttpServletRequest hsr) {
28             super(hsr, PATH);
29             hash = hsr.getParameter("hash");
30             type = hsr.getParameter("type");
31             id = hsr.getParameter("id");
32         }
33
34         @Override
35         public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
36             if ("email".equals(type)) {
37                 try {
38                     EmailAddress ea = EmailAddress.getById(Integer.parseInt(id));
39                     ea.verify(hash);
40                     out.println("Email verification completed.");
41                 } catch (IllegalArgumentException e) {
42                     out.println(translate(req, "The email address is invalid."));
43                 } catch (GigiApiException e) {
44                     e.format(out, getLanguage(req));
45                 }
46             } else if ("domain".equals(type)) {
47                 try {
48                     Domain ea = Domain.getById(Integer.parseInt(id));
49                     ea.verify(hash);
50                     out.println("Domain verification completed.");
51                 } catch (IllegalArgumentException e) {
52                     out.println(translate(req, "The domain address is invalid."));
53                 } catch (GigiApiException e) {
54                     e.format(out, getLanguage(req));
55                 }
56             }
57             return true;
58         }
59
60         @Override
61         protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
62             vars.put("hash", hash);
63             vars.put("id", id);
64             vars.put("type", type);
65             getDefaultTemplate().output(out, l, vars);
66         }
67
68     }
69
70     public static final String PATH = "/verify";
71
72     public Verify() {
73         super("Verify email");
74     }
75
76     @Override
77     public boolean needsLogin() {
78         return false;
79     }
80
81     @Override
82     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
83         try {
84             if (Form.getForm(req, VerificationForm.class).submit(resp.getWriter(), req)) {
85             }
86         } catch (GigiApiException e) {
87             e.format(resp.getWriter(), getLanguage(req));
88         }
89     }
90
91     @Override
92     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
93         new VerificationForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
94     }
95
96 }