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;
import java.security.cert.X509Certificate;
import java.sql.SQLException;
import java.util.Locale;
+import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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;
private static String acceptLanguage = null;
+ protected static Certificate loginCertificate;
+
+ protected static PrivateKey loginPrivateKey;
+
public static void setAcceptLanguage(String acceptLanguage) {
ManagedTest.acceptLanguage = acceptLanguage;
}
private static boolean inited = false;
public static Properties initEnvironment() {
+ return initEnvironment(new Properties());
+ }
+
+ public static Properties initEnvironment(Properties additionalConfig) {
try {
Properties mainProps = ConfiguredTest.initEnvironment();
+ for (Entry<Object, Object> i : additionalConfig.entrySet()) {
+ if (i.getKey() instanceof String && i.getValue() instanceof String) {
+ mainProps.setProperty((String) i.getKey(), (String) i.getValue());
+ }
+ }
if (inited) {
return mainProps;
}
}
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") + "&" //
}
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;
}
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());
+ }
+
+ }
}