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.RateLimit.RateLimitException;
29 import org.cacert.gigi.util.ServerConstants;
31 public class LoginPage extends Page {
33 public static final RateLimit RATE_LIMIT = new RateLimit(10, 5 * 60 * 1000);
35 public class LoginForm extends Form {
37 public LoginForm(HttpServletRequest hsr) {
42 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
43 if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
44 throw new RateLimitException();
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";
59 private static final String SUBMIT_EXCEPTION = "login-submit-exception";
62 super("Password Login");
66 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
67 Object o = req.getAttribute(SUBMIT_EXCEPTION);
69 ((GigiApiException) o).format(resp.getWriter(), getLanguage(req));
71 if (req.getHeader("Host").equals(ServerConstants.getSecureHostNamePort())) {
72 resp.getWriter().println(getLanguage(req).getTranslation("Authentication with certificate failed. Try another certificate or use a password."));
74 new LoginForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
79 public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
80 String redir = (String) req.getSession().getAttribute(LOGIN_RETURNPATH);
81 if (req.getSession().getAttribute("loggedin") == null) {
82 X509Certificate cert = getCertificateFromRequest(req);
84 tryAuthWithCertificate(req, cert);
86 if (req.getMethod().equals("POST")) {
88 Form.getForm(req, LoginForm.class).submit(resp.getWriter(), req);
89 } catch (GigiApiException e) {
90 req.setAttribute(SUBMIT_EXCEPTION, e);
96 if (req.getSession().getAttribute("loggedin") != null) {
99 if ( !s.startsWith("/")) {
102 resp.sendRedirect(s);
104 resp.sendRedirect("/");
112 public boolean needsLogin() {
116 private void tryAuthWithUnpw(HttpServletRequest req) throws GigiApiException {
117 String un = req.getParameter("username");
118 String pw = req.getParameter("password");
119 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `password`, `id` FROM `users` WHERE `email`=? AND verified='1'")) {
121 GigiResultSet rs = ps.executeQuery();
123 String dbHash = rs.getString(1);
124 String hash = PasswordHash.verifyHash(pw, dbHash);
126 if ( !hash.equals(dbHash)) {
127 try (GigiPreparedStatement gps = new GigiPreparedStatement("UPDATE `users` SET `password`=? WHERE `email`=?")) {
128 gps.setString(1, hash);
129 gps.setString(2, un);
133 loginSession(req, User.getById(rs.getInt(2)));
134 req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Password"));
139 throw new GigiApiException("Username and password didn't match.");
142 public static User getUser(HttpServletRequest req) {
143 AuthorizationContext ac = getAuthorizationContext(req);
147 return ac.getActor();
150 public static AuthorizationContext getAuthorizationContext(HttpServletRequest req) {
151 return ((AuthorizationContext) req.getSession().getAttribute(AUTH_CONTEXT));
154 private void tryAuthWithCertificate(HttpServletRequest req, X509Certificate x509Certificate) {
155 String serial = extractSerialFormCert(x509Certificate);
156 User user = fetchUserBySerial(serial);
160 loginSession(req, user);
161 req.getSession().setAttribute(CERT_SERIAL, serial);
162 req.getSession().setAttribute(CERT_ISSUER, x509Certificate.getIssuerDN());
163 req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Certificate"));
166 public static String extractSerialFormCert(X509Certificate x509Certificate) {
167 return x509Certificate.getSerialNumber().toString(16).toUpperCase();
170 public static User fetchUserBySerial(String serial) {
171 if ( !serial.matches("[A-Fa-f0-9]+")) {
172 throw new Error("serial malformed.");
175 CertificateOwner o = CertificateOwner.getByEnabledSerial(serial);
176 if (o == null || !(o instanceof User)) {
182 public static X509Certificate getCertificateFromRequest(HttpServletRequest req) {
183 X509Certificate[] cert = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
184 X509Certificate uc = null;
185 if (cert != null && cert[0] != null) {
191 private static final Group LOGIN_BLOCKED = Group.getByString("blockedlogin");
193 private void loginSession(HttpServletRequest req, User user) {
194 if (user.isInGroup(LOGIN_BLOCKED)) {
197 req.getSession().invalidate();
198 HttpSession hs = req.getSession();
199 hs.setAttribute(LOGGEDIN, true);
200 hs.setAttribute(Language.SESSION_ATTRIB_NAME, user.getPreferredLocale());
201 hs.setAttribute(AUTH_CONTEXT, new AuthorizationContext(user, user));
205 public boolean isPermitted(AuthorizationContext ac) {