import org.cacert.gigi.localisation.Language;
import org.cacert.gigi.output.template.Form;
import org.cacert.gigi.output.template.TranslateCommand;
+import org.cacert.gigi.pages.main.RegisterPage;
import org.cacert.gigi.util.AuthorizationContext;
import org.cacert.gigi.util.PasswordHash;
+import org.cacert.gigi.util.RateLimit;
+import org.cacert.gigi.util.RateLimit.RateLimitException;
import org.cacert.gigi.util.ServerConstants;
public class LoginPage extends Page {
+ public static final RateLimit RATE_LIMIT = new RateLimit(10, 5 * 60 * 1000);
+
public class LoginForm extends Form {
public LoginForm(HttpServletRequest hsr) {
}
@Override
- public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
+ public RedirectResult submit(HttpServletRequest req) throws GigiApiException {
+ if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
+ throw new RateLimitException();
+ }
tryAuthWithUnpw(req);
- return false;
+ return new RedirectResult(redirectPath(req));
}
@Override
}
}
+ @Override
+ public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ if (Form.printFormErrors(req, resp.getWriter())) {
+ Form.getForm(req, LoginForm.class).output(resp.getWriter(), getLanguage(req), new HashMap<String, Object>());
+ }
+ }
+
@Override
public boolean beforeTemplate(HttpServletRequest req, HttpServletResponse resp) throws IOException {
- String redir = (String) req.getSession().getAttribute(LOGIN_RETURNPATH);
if (req.getSession().getAttribute("loggedin") == null) {
X509Certificate cert = getCertificateFromRequest(req);
if (cert != null) {
tryAuthWithCertificate(req, cert);
}
if (req.getMethod().equals("POST")) {
- try {
- Form.getForm(req, LoginForm.class).submit(resp.getWriter(), req);
- } catch (GigiApiException e) {
- }
+ return Form.getForm(req, LoginForm.class).submitExceptionProtected(req, resp);
}
}
if (req.getSession().getAttribute("loggedin") != null) {
- String s = redir;
- if (s != null) {
- if ( !s.startsWith("/")) {
- s = "/" + s;
- }
- resp.sendRedirect(s);
- } else {
- resp.sendRedirect("/");
- }
+ resp.sendRedirect(redirectPath(req));
return true;
}
return false;
}
+ private static String redirectPath(HttpServletRequest req) {
+ String redir = (String) req.getSession().getAttribute(LOGIN_RETURNPATH);
+ String s = redir;
+ if (s != null) {
+ if ( !s.startsWith("/")) {
+ s = "/" + s;
+ }
+ return s;
+ } else {
+ return "/";
+ }
+ }
+
@Override
public boolean needsLogin() {
return false;
}
- private void tryAuthWithUnpw(HttpServletRequest req) {
+ private void tryAuthWithUnpw(HttpServletRequest req) throws GigiApiException {
String un = req.getParameter("username");
String pw = req.getParameter("password");
try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `password`, `id` FROM `users` WHERE `email`=? AND verified='1'")) {
}
loginSession(req, User.getById(rs.getInt(2)));
req.getSession().setAttribute(LOGIN_METHOD, new TranslateCommand("Password"));
+ return;
}
}
}
+ throw new GigiApiException("Username and password didn't match.");
}
public static User getUser(HttpServletRequest req) {
return uc;
}
- private static final Group LOGIN_BLOCKED = Group.getByString("blockedlogin");
+ private static final Group LOGIN_BLOCKED = Group.BLOCKEDLOGIN;
private void loginSession(HttpServletRequest req, User user) {
if (user.isInGroup(LOGIN_BLOCKED)) {