+
+ public String getCSRF(URLConnection u, int formIndex) throws IOException {
+ String content = IOUtils.readURL(u);
+ Pattern p = Pattern.compile("<input type='hidden' name='csrf' value='([^']+)'>");
+ Matcher m = p.matcher(content);
+ for (int i = 0; i < formIndex + 1; i++) {
+ if (!m.find()) {
+ throw new Error("No CSRF Token");
+ }
+ }
+ return m.group(1);
+ }
+
+ public static String[] generateCSR(String dn) throws IOException {
+ Process p = Runtime.getRuntime().exec(
+ new String[] { "openssl", "req", "-newkey", "rsa:1024", "-nodes", "-subj", dn, "-config",
+ "keys/selfsign.config" });
+ String csr = IOUtils.readURL(new InputStreamReader(p.getInputStream()));
+
+ String[] parts = csr.split("(?<=-----)\n(?=-----)");
+ if (parts.length != 2) {
+ System.err.println(IOUtils.readURL(new InputStreamReader(p.getErrorStream())));
+ throw new Error();
+ }
+ return parts;
+ }
+
+ public String executeBasicWebInteraction(String cookie, String path, String query) throws MalformedURLException,
+ UnsupportedEncodingException, IOException {
+ return executeBasicWebInteraction(cookie, path, query, 0);
+ }
+
+ public String executeBasicWebInteraction(String cookie, String path, String query, int formIndex)
+ throws IOException, MalformedURLException, UnsupportedEncodingException {
+ URLConnection uc = new URL("https://" + getServerName() + path).openConnection();
+ uc.addRequestProperty("Cookie", cookie);
+ String csrf = getCSRF(uc, formIndex);
+
+ uc = new URL("https://" + getServerName() + path).openConnection();
+ uc.addRequestProperty("Cookie", cookie);
+ uc.setDoOutput(true);
+ OutputStream os = uc.getOutputStream();
+ os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" //
+ + query//
+ ).getBytes());
+ os.flush();
+ String error = fetchStartErrorMessage(IOUtils.readURL(uc));
+ return error;
+ }
+
+ public EmailAddress createVerifiedEmail(User u) throws InterruptedException, GigiApiException {
+ EmailAddress adrr = new EmailAddress(createUniqueName() + "test@test.tld", u);
+ adrr.insert(Language.getInstance("en"));
+ TestMail testMail = getMailReciever().recieve();
+ assertTrue(adrr.getAddress().equals(testMail.getTo()));
+ String hash = testMail.extractLink().substring(testMail.extractLink().lastIndexOf('=') + 1);
+ adrr.verify(hash);
+ getMailReciever().clearMails();
+ return adrr;
+ }
+