]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/Verify.java
upd: use a more strict pattern for handling forms
[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.Arrays;
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.dbObjects.Domain;
14 import org.cacert.gigi.dbObjects.EmailAddress;
15 import org.cacert.gigi.dbObjects.Verifyable;
16 import org.cacert.gigi.localisation.Language;
17 import org.cacert.gigi.output.template.Form;
18 import org.cacert.gigi.output.template.SprintfCommand;
19
20 public class Verify extends Page {
21
22     private static final SprintfCommand emailAddressVerified = new SprintfCommand("Email address {0} verified", Arrays.asList("${subject}"));
23
24     private static final SprintfCommand domainVerified = new SprintfCommand("Domain {0} verified", Arrays.asList("${subject}"));
25
26     private class VerificationForm extends Form {
27
28         private String hash;
29
30         private String type;
31
32         private String id;
33
34         private Verifyable target;
35
36         String subject;
37
38         public VerificationForm(HttpServletRequest hsr) {
39             super(hsr, PATH);
40             hash = hsr.getParameter("hash");
41             type = hsr.getParameter("type");
42             id = hsr.getParameter("id");
43             if ("email".equals(type)) {
44                 EmailAddress addr = EmailAddress.getById(Integer.parseInt(id));
45                 subject = addr.getAddress();
46                 target = addr;
47             } else if ("domain".equals(type)) {
48                 Domain domain = Domain.getById(Integer.parseInt(id));
49                 subject = domain.getSuffix();
50                 target = domain;
51             } else {
52                 throw new IllegalArgumentException();
53             }
54         }
55
56         @Override
57         public boolean submit(HttpServletRequest req) throws GigiApiException {
58             if ("email".equals(type)) {
59                 try {
60                     target.verify(hash);
61                 } catch (IllegalArgumentException e) {
62                     throw new GigiApiException("The email address is invalid.");
63                 }
64             } else if ("domain".equals(type)) {
65                 try {
66                     target.verify(hash);
67                 } catch (IllegalArgumentException e) {
68                     throw new GigiApiException("The domain is invalid.");
69                 }
70             }
71             return true;
72         }
73
74         @Override
75         protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
76             vars.put("hash", hash);
77             vars.put("id", id);
78             vars.put("type", type);
79
80             vars.put("subject", subject);
81             getDefaultTemplate().output(out, l, vars);
82         }
83     }
84
85     public static final String PATH = "/verify";
86
87     public Verify() {
88         super("Verify email");
89     }
90
91     @Override
92     public boolean needsLogin() {
93         return false;
94     }
95
96     @Override
97     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
98         VerificationForm form = Form.getForm(req, VerificationForm.class);
99         if (form.submitProtected(resp.getWriter(), req)) {
100             String type = form.type;
101             HashMap<String, Object> data = new HashMap<>();
102             data.put("subject", form.subject);
103             PrintWriter out = resp.getWriter();
104             if ("email".equals(type)) {
105                 emailAddressVerified.output(out, getLanguage(req), data);
106             } else if ("domain".equals(type)) {
107                 domainVerified.output(out, getLanguage(req), data);
108             }
109         }
110     }
111
112     @Override
113     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
114         try {
115             new VerificationForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
116         } catch (IllegalArgumentException e) {
117             resp.getWriter().println(translate(req, "The object to verify is invalid."));
118         }
119     }
120
121 }