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.util.AuthorizationContext;
25 import org.cacert.gigi.util.PasswordHash;
26 import org.cacert.gigi.util.ServerConstants;
28 public class LoginPage extends Page {
30 public class LoginForm extends Form {
32 public LoginForm(HttpServletRequest hsr) {
37 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
43 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
44 getDefaultTemplate().output(out, l, vars);
49 public static final String LOGIN_RETURNPATH = "login-returnpath";
51 public LoginPage(String title) {
56 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
57 if (req.getHeader("Host").equals(ServerConstants.getSecureHostNamePort())) {
58 resp.getWriter().println(getLanguage(req).getTranslation("Authentication with certificate failed. Try another certificate or use a password."));
60 new LoginForm(req).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
65 public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
66 String redir = (String) req.getSession().getAttribute(LOGIN_RETURNPATH);
67 if (req.getSession().getAttribute("loggedin") == null) {
68 X509Certificate cert = getCertificateFromRequest(req);
70 tryAuthWithCertificate(req, cert);
72 if (req.getMethod().equals("POST")) {
74 Form.getForm(req, LoginForm.class).submit(resp.getWriter(), req);
75 } catch (GigiApiException e) {
80 if (req.getSession().getAttribute("loggedin") != null) {
83 if ( !s.startsWith("/")) {
88 resp.sendRedirect("/");
96 public boolean needsLogin() {
100 private void tryAuthWithUnpw(HttpServletRequest req) {
101 String un = req.getParameter("username");
102 String pw = req.getParameter("password");
103 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `password`, `id` FROM `users` WHERE `email`=? AND verified='1'")) {
105 GigiResultSet rs = ps.executeQuery();
107 String dbHash = rs.getString(1);
108 String hash = PasswordHash.verifyHash(pw, dbHash);
110 if ( !hash.equals(dbHash)) {
111 try (GigiPreparedStatement gps = new GigiPreparedStatement("UPDATE `users` SET `password`=? WHERE `email`=?")) {
112 gps.setString(1, hash);
113 gps.setString(2, un);
117 loginSession(req, User.getById(rs.getInt(2)));
118 req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Password"));
124 public static User getUser(HttpServletRequest req) {
125 AuthorizationContext ac = getAuthorizationContext(req);
129 return ac.getActor();
132 public static AuthorizationContext getAuthorizationContext(HttpServletRequest req) {
133 return ((AuthorizationContext) req.getSession().getAttribute(AUTH_CONTEXT));
136 private void tryAuthWithCertificate(HttpServletRequest req, X509Certificate x509Certificate) {
137 String serial = extractSerialFormCert(x509Certificate);
138 User user = fetchUserBySerial(serial);
142 loginSession(req, user);
143 req.getSession().setAttribute(CERT_SERIAL, serial);
144 req.getSession().setAttribute(CERT_ISSUER, x509Certificate.getIssuerDN());
145 req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Certificate"));
148 public static String extractSerialFormCert(X509Certificate x509Certificate) {
149 return x509Certificate.getSerialNumber().toString(16).toUpperCase();
152 public static User fetchUserBySerial(String serial) {
153 if ( !serial.matches("[A-Fa-f0-9]+")) {
154 throw new Error("serial malformed.");
157 CertificateOwner o = CertificateOwner.getByEnabledSerial(serial);
158 if (o == null || !(o instanceof User)) {
164 public static X509Certificate getCertificateFromRequest(HttpServletRequest req) {
165 X509Certificate[] cert = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
166 X509Certificate uc = null;
167 if (cert != null && cert[0] != null) {
173 private static final Group LOGIN_BLOCKED = Group.getByString("blockedlogin");
175 private void loginSession(HttpServletRequest req, User user) {
176 if (user.isInGroup(LOGIN_BLOCKED)) {
179 req.getSession().invalidate();
180 HttpSession hs = req.getSession();
181 hs.setAttribute(LOGGEDIN, true);
182 hs.setAttribute(Language.SESSION_ATTRIB_NAME, user.getPreferredLocale());
183 hs.setAttribute(AUTH_CONTEXT, new AuthorizationContext(user, user));
187 public boolean isPermitted(AuthorizationContext ac) {