From 0fa2038d33d575c8101d1f06767df914425e8f88 Mon Sep 17 00:00:00 2001 From: Bernhard Haumacher Date: Wed, 1 Jan 2025 17:37:16 +0100 Subject: [PATCH] Issue #113: Allow to log-in with code sent to e-mail address. --- .../app/EMailVerificationServlet.java | 24 ++++- .../phoneblock/app/LoginServlet.java | 7 +- .../phoneblock/app/RegistrationServlet.java | 20 ++++- .../app/oauth/OAuthLoginServlet.java | 3 +- phoneblock/src/main/webapp/login.jsp | 89 +++++++++++++++++-- phoneblock/src/main/webapp/signup-code.jsp | 64 ++++++------- 6 files changed, 152 insertions(+), 55 deletions(-) diff --git a/phoneblock/src/main/java/de/haumacher/phoneblock/app/EMailVerificationServlet.java b/phoneblock/src/main/java/de/haumacher/phoneblock/app/EMailVerificationServlet.java index 165122e3..b2d74232 100644 --- a/phoneblock/src/main/java/de/haumacher/phoneblock/app/EMailVerificationServlet.java +++ b/phoneblock/src/main/java/de/haumacher/phoneblock/app/EMailVerificationServlet.java @@ -18,16 +18,29 @@ import de.haumacher.phoneblock.db.DBService; import de.haumacher.phoneblock.mail.MailService; import de.haumacher.phoneblock.mail.MailServiceStarter; +import de.haumacher.phoneblock.util.ServletUtil; /** * {@link HttpServlet} that is invoked from the signup.jsp form. */ @WebServlet(urlPatterns = { EMailVerificationServlet.VERIFY_WEB, + EMailVerificationServlet.LOGIN_WEB, }) public class EMailVerificationServlet extends HttpServlet { + /** + * Request attribute holding the page to re-start login/signup. + */ + public static final String RESTART_PAGE_ATTR = "restartPage"; + + /** + * Request attribute set, if e-mail verification failed. + */ + public static final String VERIFY_ERROR_ATTR = "message"; + public static final String VERIFY_WEB = "/verify-web"; + public static final String LOGIN_WEB = "/login-web"; private static final Logger LOG = LoggerFactory.getLogger(EMailVerificationServlet.class); @@ -66,17 +79,23 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S req.getSession().setAttribute("email", email); req.getSession().setAttribute("code", code); req.setAttribute("email", email); + req.setAttribute(RESTART_PAGE_ATTR, failurePage(req)); req.getRequestDispatcher(successPage(req)).forward(req, resp); } private void sendFailure(HttpServletRequest req, HttpServletResponse resp, String message) throws ServletException, IOException { - req.setAttribute("message", message); + req.setAttribute(VERIFY_ERROR_ATTR, message); req.getRequestDispatcher(failurePage(req)).forward(req, resp); } - private String failurePage(HttpServletRequest req) { + /** + * The page to redirect, if something went wrong. + */ + private static String failurePage(HttpServletRequest req) { switch (req.getServletPath()) { + case LOGIN_WEB: + return "/login.jsp"; case VERIFY_WEB: default: return "/signup.jsp"; @@ -85,6 +104,7 @@ private String failurePage(HttpServletRequest req) { private String successPage(HttpServletRequest req) { switch (req.getServletPath()) { + case LOGIN_WEB: case VERIFY_WEB: default: return "/signup-code.jsp"; diff --git a/phoneblock/src/main/java/de/haumacher/phoneblock/app/LoginServlet.java b/phoneblock/src/main/java/de/haumacher/phoneblock/app/LoginServlet.java index 74afa0a5..8fd99c92 100644 --- a/phoneblock/src/main/java/de/haumacher/phoneblock/app/LoginServlet.java +++ b/phoneblock/src/main/java/de/haumacher/phoneblock/app/LoginServlet.java @@ -27,6 +27,11 @@ @WebServlet(urlPatterns = LoginServlet.PATH) public class LoginServlet extends HttpServlet { + /** + * Request attribute set, if a login was not successful. + */ + public static final String LOGIN_ERROR_ATTR = "loginError"; + public static final String USER_NAME_PARAM = "userName"; public static final String PASSWORD_PARAM = "password"; @@ -120,7 +125,7 @@ public static void processRememberMe(HttpServletRequest req, HttpServletResponse * Redirects the client to the login page. */ public static void sendFailure(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - req.setAttribute("error", "Anmeldung fehlgeschlagen."); + req.setAttribute(LOGIN_ERROR_ATTR, "Anmeldung fehlgeschlagen."); req.getRequestDispatcher("/login.jsp").forward(req, resp); } diff --git a/phoneblock/src/main/java/de/haumacher/phoneblock/app/RegistrationServlet.java b/phoneblock/src/main/java/de/haumacher/phoneblock/app/RegistrationServlet.java index 34448218..14639ff3 100644 --- a/phoneblock/src/main/java/de/haumacher/phoneblock/app/RegistrationServlet.java +++ b/phoneblock/src/main/java/de/haumacher/phoneblock/app/RegistrationServlet.java @@ -27,6 +27,11 @@ }) public class RegistrationServlet extends HttpServlet { + /** + * Request attribute set, if registration fails. + */ + public static final String REGISTER_ERROR_ATTR = "message"; + public static final String REGISTER_WEB = "/register-web"; private static final String PASSWORD_ATTR = "passwd"; @@ -53,16 +58,23 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws S String login; try { + String passwd; + DB db = DBService.getInstance(); login = db.getEmailLogin(email); if (login == null) { login = UUID.randomUUID().toString(); - String passwd = db.createUser(login, email); + passwd = db.createUser(login, email); db.setEmail(login, email); - startSetup(req, resp, login, passwd); } else { - startSetup(req, resp, login, null); + // No longer known. + passwd = null; } + + String rememberValue = req.getParameter(LoginServlet.REMEMBER_PARAM); + LoginServlet.processRememberMe(req, resp, db, rememberValue, login); + + startSetup(req, resp, login, passwd); } catch (Exception ex) { LOG.error("Failed to create user: " + email, ex); @@ -103,7 +115,7 @@ private static String successPage(HttpServletRequest req) { } private void sendError(HttpServletRequest req, HttpServletResponse resp, String message) throws ServletException, IOException { - req.setAttribute("message", message); + req.setAttribute(REGISTER_ERROR_ATTR, message); req.getRequestDispatcher(errorPage(req)).forward(req, resp); } diff --git a/phoneblock/src/main/java/de/haumacher/phoneblock/app/oauth/OAuthLoginServlet.java b/phoneblock/src/main/java/de/haumacher/phoneblock/app/oauth/OAuthLoginServlet.java index 0e010989..347e77e2 100644 --- a/phoneblock/src/main/java/de/haumacher/phoneblock/app/oauth/OAuthLoginServlet.java +++ b/phoneblock/src/main/java/de/haumacher/phoneblock/app/oauth/OAuthLoginServlet.java @@ -101,6 +101,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se } if (login == null) { + // Create new account. login = UUID.randomUUID().toString(); if (displayName == null) { @@ -112,7 +113,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se } String passwd = db.createUser(login, displayName); - db.setGoogleId(login, googleId, displayName); + db.setGoogleId(login, googleId, null); if (email != null) { try { db.setEmail(login, email); diff --git a/phoneblock/src/main/webapp/login.jsp b/phoneblock/src/main/webapp/login.jsp index 66df6683..f4ef7b71 100644 --- a/phoneblock/src/main/webapp/login.jsp +++ b/phoneblock/src/main/webapp/login.jsp @@ -1,4 +1,5 @@ +<%@page import="de.haumacher.phoneblock.app.EMailVerificationServlet"%> <%@page import="de.haumacher.phoneblock.app.SettingsServlet"%> <%@page import="java.net.URLEncoder"%> <%@page import="java.net.URL"%> @@ -15,10 +16,6 @@ -<% - boolean error = request.getAttribute("error") != null; - String userActive = error ? "is-active" : ""; -%> @@ -82,7 +79,83 @@ + +<% + Object emailMessage = request.getAttribute(EMailVerificationServlet.VERIFY_ERROR_ATTR); + boolean emailError = emailMessage != null; + String emailActive = emailError ? "is-active" : ""; + String inputClass = emailError ? "input is-danger" : "input"; +%> + +<% + boolean loginError = request.getAttribute(LoginServlet.LOGIN_ERROR_ATTR) != null; + String userActive = loginError ? "is-active" : ""; +%> +