X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2FForm.java;h=a6374e532b74c0b2b57f50015bbdd878fc7c9d56;hp=11209ca51146769f15ec27654556db9343a80260;hb=fb38a9c8b9d86289213a36bd3d2afddc58ec7d3f;hpb=0c19a843c23f61cf32f555355a9fa1baf9c8f8f1 diff --git a/src/org/cacert/gigi/output/Form.java b/src/org/cacert/gigi/output/Form.java index 11209ca5..a6374e53 100644 --- a/src/org/cacert/gigi/output/Form.java +++ b/src/org/cacert/gigi/output/Form.java @@ -1,5 +1,6 @@ package org.cacert.gigi.output; +import java.io.IOException; import java.io.PrintWriter; import java.util.Map; @@ -7,66 +8,95 @@ import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; -import org.cacert.gigi.Language; +import org.cacert.gigi.localisation.Language; import org.cacert.gigi.pages.Page; import org.cacert.gigi.util.RandomToken; public abstract class Form implements Outputable { - String csrf; - - public Form(HttpServletRequest hsr) { - csrf = RandomToken.generateToken(32); - HttpSession hs = hsr.getSession(); - hs.setAttribute("form/" + getClass().getName() + "/" + csrf, this); - - } - - public abstract boolean submit(PrintWriter out, HttpServletRequest req); - - @Override - public final void output(PrintWriter out, Language l, Map vars) { - out.println("
"); - outputContent(out, l, vars); - out.print("
"); - } - - protected abstract void outputContent(PrintWriter out, Language l, Map vars); - - protected void outputError(PrintWriter out, ServletRequest req, String text) { - out.print("
"); - out.print(Page.translate(req, text)); - out.println("
"); - } - - protected String getCSRFToken() { - return csrf; - } - - protected void checkCSRF(HttpServletRequest req) { - if (!csrf.equals(req.getParameter("csrf"))) { - throw new CSRFError(); - } - } - - public static T getForm(HttpServletRequest req, Class target) { - String csrf = req.getParameter("csrf"); - if (csrf == null) { - throw new CSRFError(); - } - HttpSession hs = req.getSession(); - if (hs == null) { - throw new CSRFError(); - } - Form f = (Form) hs.getAttribute("form/" + target.getName() + "/" + csrf); - if (f == null) { - throw new CSRFError(); - } - return (T) f; - } - - public static class CSRFError extends Error { - - } + + public static final String CSRF_FIELD = "csrf"; + + private String csrf; + + public Form(HttpServletRequest hsr) { + csrf = RandomToken.generateToken(32); + HttpSession hs = hsr.getSession(); + hs.setAttribute("form/" + getClass().getName() + "/" + csrf, this); + + } + + public abstract boolean submit(PrintWriter out, HttpServletRequest req); + + protected String getCsrfFieldName() { + return CSRF_FIELD; + } + + @Override + public void output(PrintWriter out, Language l, Map vars) { + out.println("
"); + failed = false; + outputContent(out, l, vars); + out.print("
"); + } + + protected abstract void outputContent(PrintWriter out, Language l, Map vars); + + boolean failed; + + protected void outputError(PrintWriter out, ServletRequest req, String text, Object... contents) { + if ( !failed) { + failed = true; + out.println("
"); + } + out.print("
"); + if (contents.length == 0) { + out.print(Page.translate(req, text)); + } else { + out.print(String.format(Page.translate(req, text), contents)); + } + out.println("
"); + } + + protected void outputErrorPlain(PrintWriter out, String text) { + if ( !failed) { + failed = true; + out.println("
"); + } + out.print("
"); + out.print(text); + out.println("
"); + } + + public boolean isFailed(PrintWriter out) { + if (failed) { + out.println("
"); + } + return failed; + } + + protected String getCSRFToken() { + return csrf; + } + + public static T getForm(HttpServletRequest req, Class target) throws CSRFException { + String csrf = req.getParameter(CSRF_FIELD); + if (csrf == null) { + throw new CSRFException(); + } + HttpSession hs = req.getSession(); + if (hs == null) { + throw new CSRFException(); + } + Form f = (Form) hs.getAttribute("form/" + target.getName() + "/" + csrf); + if (f == null) { + throw new CSRFException(); + } + return (T) f; + } + + public static class CSRFException extends IOException { + + } }