1 package org.cacert.gigi.output.template;
3 import java.io.IOException;
4 import java.io.PrintWriter;
7 import javax.servlet.ServletRequest;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpSession;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.localisation.Language;
13 import org.cacert.gigi.pages.Page;
14 import org.cacert.gigi.util.RandomToken;
17 * A generic HTML-form that handles CSRF-token creation.
19 public abstract class Form implements Outputable {
21 public static final String CSRF_FIELD = "csrf";
23 private final String csrf;
25 private final String action;
28 * Creates a new {@link Form}.
31 * the request to register the form against.
33 public Form(HttpServletRequest hsr) {
38 * Creates a new {@link Form}.
41 * the request to register the form against.
43 * the target path where the form should be submitted.
45 public Form(HttpServletRequest hsr, String action) {
46 csrf = RandomToken.generateToken(32);
48 HttpSession hs = hsr.getSession();
49 hs.setAttribute("form/" + getClass().getName() + "/" + csrf, this);
53 * Update the forms internal state based on submitted data.
56 * the stream to the user.
58 * the request to take the initial data from.
59 * @return true, iff the form succeeded and the user should be redirected.
60 * @throws GigiApiException
61 * if internal operations went wrong.
63 public abstract boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException;
65 protected String getCsrfFieldName() {
70 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
72 out.println("<form method='POST'>");
74 out.println("<form method='POST' action='" + action + "'>");
77 outputContent(out, l, vars);
78 out.print("<input type='hidden' name='" + CSRF_FIELD + "' value='");
79 out.print(getCSRFToken());
80 out.println("'></form>");
84 * Outputs the forms contents.
89 * {@link Language} to translate text to.
91 * Variables supplied from the outside.
93 protected abstract void outputContent(PrintWriter out, Language l, Map<String, Object> vars);
95 private boolean failed;
97 protected void outputError(PrintWriter out, ServletRequest req, String text, Object... contents) {
100 out.println("<div class='formError'>");
103 if (contents.length == 0) {
104 out.print(Page.translate(req, text));
106 out.print(String.format(Page.translate(req, text), contents));
108 out.println("</div>");
111 protected void outputErrorPlain(PrintWriter out, String text) {
114 out.println("<div class='formError'>");
118 out.println("</div>");
121 public boolean isFailed(PrintWriter out) {
123 out.println("</div>");
128 protected String getCSRFToken() {
133 * Re-fetches a form e.g. when a Post-request is received.
136 * the request that is directed to the form.
138 * the {@link Class} of the expected form.
139 * @return the form where this request is directed to.
140 * @throws CSRFException
141 * if no CSRF-token is found or the token is wrong.
143 public static <T extends Form> T getForm(HttpServletRequest req, Class<T> target) throws CSRFException {
144 String csrf = req.getParameter(CSRF_FIELD);
146 throw new CSRFException();
148 HttpSession hs = req.getSession();
150 throw new CSRFException();
152 Form f = (Form) hs.getAttribute("form/" + target.getName() + "/" + csrf);
154 throw new CSRFException();
159 public static class CSRFException extends IOException {
161 private static final long serialVersionUID = 59708247477988362L;