1 package org.cacert.gigi.pages;
3 import static org.cacert.gigi.Gigi.*;
5 import java.io.IOException;
6 import java.io.PrintWriter;
7 import java.security.cert.X509Certificate;
8 import java.util.HashMap;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13 import javax.servlet.http.HttpSession;
15 import org.cacert.gigi.GigiApiException;
16 import org.cacert.gigi.database.GigiPreparedStatement;
17 import org.cacert.gigi.database.GigiResultSet;
18 import org.cacert.gigi.dbObjects.CertificateOwner;
19 import org.cacert.gigi.dbObjects.Group;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.localisation.Language;
22 import org.cacert.gigi.output.template.Form;
23 import org.cacert.gigi.output.template.TranslateCommand;
24 import org.cacert.gigi.pages.main.RegisterPage;
25 import org.cacert.gigi.util.AuthorizationContext;
26 import org.cacert.gigi.util.PasswordHash;
27 import org.cacert.gigi.util.RateLimit;
28 import org.cacert.gigi.util.ServerConstants;
30 public class LoginPage extends Page {
32 public static final RateLimit RATE_LIMIT = new RateLimit(10, 5 * 60 * 1000);
34 public class LoginForm extends Form {
36 public LoginForm(HttpServletRequest hsr) {
41 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
42 if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
43 outputError(out, req, "Rate Limit Exceeded");
51 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
52 getDefaultTemplate().output(out, l, vars);
57 public static final String LOGIN_RETURNPATH = "login-returnpath";
60 super("Password Login");
64 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
65 if (req.getHeader("Host").equals(ServerConstants.getSecureHostNamePort())) {
66 resp.getWriter().println(getLanguage(req).getTranslation("Authentication with certificate failed. Try another certificate or use a password."));
68 new LoginForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
73 public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
74 String redir = (String) req.getSession().getAttribute(LOGIN_RETURNPATH);
75 if (req.getSession().getAttribute("loggedin") == null) {
76 X509Certificate cert = getCertificateFromRequest(req);
78 tryAuthWithCertificate(req, cert);
80 if (req.getMethod().equals("POST")) {
82 Form.getForm(req, LoginForm.class).submit(resp.getWriter(), req);
83 } catch (GigiApiException e) {
88 if (req.getSession().getAttribute("loggedin") != null) {
91 if ( !s.startsWith("/")) {
96 resp.sendRedirect("/");
104 public boolean needsLogin() {
108 private void tryAuthWithUnpw(HttpServletRequest req) {
109 String un = req.getParameter("username");
110 String pw = req.getParameter("password");
111 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `password`, `id` FROM `users` WHERE `email`=? AND verified='1'")) {
113 GigiResultSet rs = ps.executeQuery();
115 String dbHash = rs.getString(1);
116 String hash = PasswordHash.verifyHash(pw, dbHash);
118 if ( !hash.equals(dbHash)) {
119 try (GigiPreparedStatement gps = new GigiPreparedStatement("UPDATE `users` SET `password`=? WHERE `email`=?")) {
120 gps.setString(1, hash);
121 gps.setString(2, un);
125 loginSession(req, User.getById(rs.getInt(2)));
126 req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Password"));
132 public static User getUser(HttpServletRequest req) {
133 AuthorizationContext ac = getAuthorizationContext(req);
137 return ac.getActor();
140 public static AuthorizationContext getAuthorizationContext(HttpServletRequest req) {
141 return ((AuthorizationContext) req.getSession().getAttribute(AUTH_CONTEXT));
144 private void tryAuthWithCertificate(HttpServletRequest req, X509Certificate x509Certificate) {
145 String serial = extractSerialFormCert(x509Certificate);
146 User user = fetchUserBySerial(serial);
150 loginSession(req, user);
151 req.getSession().setAttribute(CERT_SERIAL, serial);
152 req.getSession().setAttribute(CERT_ISSUER, x509Certificate.getIssuerDN());
153 req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Certificate"));
156 public static String extractSerialFormCert(X509Certificate x509Certificate) {
157 return x509Certificate.getSerialNumber().toString(16).toUpperCase();
160 public static User fetchUserBySerial(String serial) {
161 if ( !serial.matches("[A-Fa-f0-9]+")) {
162 throw new Error("serial malformed.");
165 CertificateOwner o = CertificateOwner.getByEnabledSerial(serial);
166 if (o == null || !(o instanceof User)) {
172 public static X509Certificate getCertificateFromRequest(HttpServletRequest req) {
173 X509Certificate[] cert = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
174 X509Certificate uc = null;
175 if (cert != null && cert[0] != null) {
181 private static final Group LOGIN_BLOCKED = Group.getByString("blockedlogin");
183 private void loginSession(HttpServletRequest req, User user) {
184 if (user.isInGroup(LOGIN_BLOCKED)) {
187 req.getSession().invalidate();
188 HttpSession hs = req.getSession();
189 hs.setAttribute(LOGGEDIN, true);
190 hs.setAttribute(Language.SESSION_ATTRIB_NAME, user.getPreferredLocale());
191 hs.setAttribute(AUTH_CONTEXT, new AuthorizationContext(user, user));
195 public boolean isPermitted(AuthorizationContext ac) {