import java.io.IOException;
import java.io.PrintWriter;
+import java.math.BigInteger;
import java.security.cert.X509Certificate;
import java.util.Map;
try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `password`, `id` FROM `users` WHERE `email`=? AND verified='1'")) {
ps.setString(1, un);
GigiResultSet rs = ps.executeQuery();
- if (rs.next()) {
- String dbHash = rs.getString(1);
- String hash = PasswordHash.verifyHash(pw, dbHash);
- if (hash != null) {
- if ( !hash.equals(dbHash)) {
- try (GigiPreparedStatement gps = new GigiPreparedStatement("UPDATE `users` SET `password`=? WHERE `email`=?")) {
- gps.setString(1, hash);
- gps.setString(2, un);
- gps.executeUpdate();
- }
+ if ( !rs.next()) {
+ throw new GigiApiException("Username and password didn't match.");
+ }
+
+ User user = User.getById(rs.getInt(2));
+ if (user == null) {
+ throw new GigiApiException("Username and password didn't match.");
+ }
+
+ String dbHash = rs.getString(1);
+ String hash = PasswordHash.verifyHash(pw, dbHash);
+ if (hash != null) {
+ if ( !hash.equals(dbHash)) {
+ try (GigiPreparedStatement gps = new GigiPreparedStatement("UPDATE `users` SET `password`=? WHERE `email`=?")) {
+ gps.setString(1, hash);
+ gps.setString(2, un);
+ gps.executeUpdate();
}
- loginSession(req, User.getById(rs.getInt(2)));
- req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Password"));
- return;
}
+
+ loginSession(req, user);
+ req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Password"));
+ return;
}
}
- throw new GigiApiException("Username and password didn't match.");
}
public static User getUser(HttpServletRequest req) {
}
private void tryAuthWithCertificate(HttpServletRequest req, X509Certificate x509Certificate) {
- String serial = extractSerialFormCert(x509Certificate);
+ BigInteger serial = extractSerialFormCert(x509Certificate);
User user = fetchUserBySerial(serial);
if (user == null) {
return;
req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Certificate"));
}
- public static String extractSerialFormCert(X509Certificate x509Certificate) {
- return x509Certificate.getSerialNumber().toString(16).toLowerCase();
+ public static BigInteger extractSerialFormCert(X509Certificate x509Certificate) {
+ return x509Certificate.getSerialNumber();
}
- public static User fetchUserBySerial(String serial) {
- if ( !serial.matches("[a-f0-9]+")) {
- throw new Error("serial malformed.");
- }
-
+ public static User fetchUserBySerial(BigInteger serial) {
CertificateOwner o = CertificateOwner.getByEnabledSerial(serial);
if (o == null || !(o instanceof User)) {
return null;