import static org.cacert.gigi.Gigi.*;
import java.io.IOException;
+import java.io.PrintWriter;
import java.security.cert.X509Certificate;
+import java.util.HashMap;
+import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
+import org.cacert.gigi.GigiApiException;
import org.cacert.gigi.database.DatabaseConnection;
import org.cacert.gigi.database.GigiPreparedStatement;
import org.cacert.gigi.database.GigiResultSet;
import org.cacert.gigi.dbObjects.Group;
import org.cacert.gigi.dbObjects.User;
import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.output.template.Form;
import org.cacert.gigi.util.PasswordHash;
public class LoginPage extends Page {
+ public class LoginForm extends Form {
+
+ public LoginForm(HttpServletRequest hsr) {
+ super(hsr);
+ }
+
+ @Override
+ public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
+ tryAuthWithUnpw(req);
+ return false;
+ }
+
+ @Override
+ protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
+ getDefaultTemplate().output(out, l, vars);
+ }
+
+ }
+
public static final String LOGIN_RETURNPATH = "login-returnpath";
public LoginPage(String title) {
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
- resp.getWriter().println("<form method='POST' action='/login'>" + "<input type='text' name='username'>" + "<input type='password' name='password'> <input type='submit' value='login'></form>");
+ new LoginForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
}
@Override
tryAuthWithCertificate(req, cert[0]);
}
if (req.getMethod().equals("POST")) {
- tryAuthWithUnpw(req);
+ try {
+ Form.getForm(req, LoginForm.class).submit(resp.getWriter(), req);
+ } catch (GigiApiException e) {
+ }
}
}
ps.setString(1, un);
GigiResultSet rs = ps.executeQuery();
if (rs.next()) {
- if (PasswordHash.verifyHash(pw, rs.getString(1))) {
+ String dbHash = rs.getString(1);
+ String hash = PasswordHash.verifyHash(pw, dbHash);
+ if (hash != null) {
+ if ( !hash.equals(dbHash)) {
+ GigiPreparedStatement gps = DatabaseConnection.getInstance().prepare("UPDATE `users` SET `password`=? WHERE `email`=?");
+ gps.setString(1, hash);
+ gps.setString(2, un);
+ gps.executeUpdate();
+ }
loginSession(req, User.getById(rs.getInt(2)));
}
}
GigiResultSet rs = ps.executeQuery();
if (rs.next()) {
loginSession(req, User.getById(rs.getInt(1)));
+ req.getSession().setAttribute(CERT_SERIAL, serial);
+ req.getSession().setAttribute(CERT_ISSUER, x509Certificate.getIssuerDN());
}
rs.close();
}