X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=tests%2Fclub%2Fwpia%2Fgigi%2FtestUtils%2FManagedTest.java;h=4a8324bd860d2d95a71060b6fdf4126af8556376;hp=a2eb449fb988b083d2369c9e6ef6b028be5e7c07;hb=c23bd923858a6c589bddecebd65fdf0739c62e6a;hpb=7b9bab0434b96d4bf196a51ab769e33553d3e09e diff --git a/tests/club/wpia/gigi/testUtils/ManagedTest.java b/tests/club/wpia/gigi/testUtils/ManagedTest.java index a2eb449f..4a8324bd 100644 --- a/tests/club/wpia/gigi/testUtils/ManagedTest.java +++ b/tests/club/wpia/gigi/testUtils/ManagedTest.java @@ -17,7 +17,9 @@ import java.net.URLConnection; import java.net.URLEncoder; import java.nio.file.Files; import java.nio.file.Paths; +import java.security.GeneralSecurityException; import java.security.KeyManagementException; +import java.security.KeyPair; import java.security.NoSuchAlgorithmException; import java.security.Principal; import java.security.PrivateKey; @@ -42,6 +44,9 @@ import club.wpia.gigi.DevelLauncher; import club.wpia.gigi.GigiApiException; import club.wpia.gigi.database.GigiPreparedStatement; import club.wpia.gigi.database.GigiResultSet; +import club.wpia.gigi.dbObjects.Certificate; +import club.wpia.gigi.dbObjects.Certificate.CSRType; +import club.wpia.gigi.dbObjects.Digest; import club.wpia.gigi.dbObjects.EmailAddress; import club.wpia.gigi.dbObjects.Group; import club.wpia.gigi.dbObjects.Job; @@ -70,6 +75,10 @@ public class ManagedTest extends ConfiguredTest { private static String acceptLanguage = null; + protected static Certificate loginCertificate; + + protected static PrivateKey loginPrivateKey; + public static void setAcceptLanguage(String acceptLanguage) { ManagedTest.acceptLanguage = acceptLanguage; } @@ -278,7 +287,7 @@ public class ManagedTest extends ConfiguredTest { public static void registerUser(String firstName, String lastName, String email, String password) { try { - String query = "name-type=western&fname=" + URLEncoder.encode(firstName, "UTF-8") + "&lname=" + URLEncoder.encode(lastName, "UTF-8") + "&email=" + URLEncoder.encode(email, "UTF-8") + "&pword1=" + URLEncoder.encode(password, "UTF-8") + "&pword2=" + URLEncoder.encode(password, "UTF-8") + "&day=1&month=1&year=1910&tos_agree=1"; + String query = "name-type=western&fname=" + URLEncoder.encode(firstName, "UTF-8") + "&lname=" + URLEncoder.encode(lastName, "UTF-8") + "&email=" + URLEncoder.encode(email, "UTF-8") + "&pword1=" + URLEncoder.encode(password, "UTF-8") + "&pword2=" + URLEncoder.encode(password, "UTF-8") + "&day=1&month=1&year=1910&tos_agree=1&dp_agree=1"; String data = fetchStartErrorMessage(runRegister(query)); assertNull(data); } catch (UnsupportedEncodingException e) { @@ -469,12 +478,16 @@ public class ManagedTest extends ConfiguredTest { } public static HttpURLConnection post(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 server = getServerName(); + if (loginCertificate != null) { + server = getSecureServerName(); + } + URLConnection uc = new URL("https://" + server + path).openConnection(); + authenticate((HttpURLConnection) uc, cookie); String csrf = getCSRF(uc, formIndex); - uc = new URL("https://" + getServerName() + path).openConnection(); - uc.addRequestProperty("Cookie", cookie); + uc = new URL("https://" + server + path).openConnection(); + authenticate((HttpURLConnection) uc, cookie); uc.setDoOutput(true); OutputStream os = uc.getOutputStream(); os.write(("csrf=" + URLEncoder.encode(csrf, "UTF-8") + "&" // @@ -485,8 +498,12 @@ public class ManagedTest extends ConfiguredTest { } public static HttpURLConnection get(String cookie, String path) throws IOException { - URLConnection uc = new URL("https://" + getServerName() + path).openConnection(); - uc.addRequestProperty("Cookie", cookie); + String server = getServerName(); + if (loginCertificate != null) { + server = getSecureServerName(); + } + URLConnection uc = new URL("https://" + server + path).openConnection(); + authenticate((HttpURLConnection) uc, cookie); return (HttpURLConnection) uc; } @@ -525,4 +542,40 @@ public class ManagedTest extends ConfiguredTest { supporter = User.getById(i); return supporter; } + + protected static void authenticate(HttpURLConnection uc, String cookie) throws IOException { + uc.addRequestProperty("Cookie", cookie); + if (loginCertificate != null) { + try { + authenticateClientCert(loginPrivateKey, loginCertificate.cert(), uc); + } catch (GeneralSecurityException | GigiApiException e) { + throw new IOException(e); + } + } + } + + protected String cookieWithCertificateLogin(User u) throws IOException, GigiApiException { + + try { + KeyPair kp; + kp = generateKeypair(); + + String csr; + csr = generatePEMCSR(kp, "CN=" + u.getPreferredName().toString()); + + Certificate c = new Certificate(u, u, Certificate.buildDN("CN", u.getPreferredName().toString()), Digest.SHA256, csr, CSRType.CSR, getClientProfile()); + final PrivateKey pk = kp.getPrivate(); + await(c.issue(null, "2y", u)); + final X509Certificate ce = c.cert(); + c.setLoginEnabled(true); + loginCertificate = c; + loginPrivateKey = pk; + return login(pk, ce); + } catch (InterruptedException e) { + throw new GigiApiException(e.toString()); + } catch (GeneralSecurityException e) { + throw new GigiApiException(e.toString()); + } + + } }