X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;ds=inline;f=src%2Forg%2Fcacert%2Fgigi%2Foutput%2FForm.java;h=e5d05cb6c0e294fe80129aa19baf4e0f9c5211db;hb=48b552dcb661a6ba039e6b28bea50655667c70f9;hp=dd244d749afc36077bc49e55b5813268b17ddf4c;hpb=13cb21e19f65dc9f8230a641edae676d88418889;p=gigi.git diff --git a/src/org/cacert/gigi/output/Form.java b/src/org/cacert/gigi/output/Form.java index dd244d74..e5d05cb6 100644 --- a/src/org/cacert/gigi/output/Form.java +++ b/src/org/cacert/gigi/output/Form.java @@ -13,56 +13,62 @@ import org.cacert.gigi.pages.Page; import org.cacert.gigi.util.RandomToken; public abstract class Form implements Outputable { - public static final String CSRF_FIELD = "csrf"; - 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; - } - - 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 { - - } + + public static final String CSRF_FIELD = "csrf"; + + 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("
"); + 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; + } + + 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 { + + } }