From 2cae85e20d4d8df4d7c1580f0cb3e4be352d6266 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 19 Aug 2016 09:57:00 +0200 Subject: [PATCH] Added E-mail check and fixes --- pages/js/receivemessage.js | 2 ++ .../norbipeti/chat/server/page/IndexPage.java | 2 +- .../chat/server/page/RegisterAjaxPage.java | 19 ++++++++++++++----- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/pages/js/receivemessage.js b/pages/js/receivemessage.js index 0c90054..91e83ee 100644 --- a/pages/js/receivemessage.js +++ b/pages/js/receivemessage.js @@ -89,12 +89,14 @@ $(document).ready(function () { $('#msginput').on("focus", function () { readTimer == null ? readTimer = setTimeout(function () { resetUnread(); + readTimer = null; }, 3000) : readTimer; shouldread = true; }); $('#msginput').on("keydown", resetUnread); $('#msginput').on("blur", function () { readTimer != null ? clearTimeout(readTimer) : readTimer; + readTimer = null; shouldread = false; }); }); diff --git a/src/io/github/norbipeti/chat/server/page/IndexPage.java b/src/io/github/norbipeti/chat/server/page/IndexPage.java index f9afb19..1420ecc 100644 --- a/src/io/github/norbipeti/chat/server/page/IndexPage.java +++ b/src/io/github/norbipeti/chat/server/page/IndexPage.java @@ -64,7 +64,7 @@ public class IndexPage extends Page { return doc; }, exchange); - } // TODO: Validation at registration (no special chars, etc.) + } @Override public String GetName() { diff --git a/src/io/github/norbipeti/chat/server/page/RegisterAjaxPage.java b/src/io/github/norbipeti/chat/server/page/RegisterAjaxPage.java index dc4e693..833d243 100644 --- a/src/io/github/norbipeti/chat/server/page/RegisterAjaxPage.java +++ b/src/io/github/norbipeti/chat/server/page/RegisterAjaxPage.java @@ -1,6 +1,8 @@ package io.github.norbipeti.chat.server.page; import java.io.IOException; +import java.util.regex.Pattern; + import org.mindrot.jbcrypt.BCrypt; import com.google.gson.JsonObject; @@ -12,6 +14,8 @@ import io.github.norbipeti.chat.server.db.domain.User; import io.github.norbipeti.chat.server.io.IOHelper; public class RegisterAjaxPage extends Page { + private static final Pattern EMAIL_PATTERN = Pattern.compile("^[\\w.-]+@[\\w.-]+\\.[\\w.-]+$"); + @Override public void handlePage(HttpExchange exchange) throws IOException { JsonObject post = IOHelper.GetPOSTJSON(exchange); @@ -22,10 +26,15 @@ public class RegisterAjaxPage extends Page { IOHelper.SendResponse(200, (doc) -> doc.html(msg).ownerDocument(), exchange); return; // TODO: Use JavaScript too, for error checks } - for (User user : DataManager.getAll(User.class)) { // TODO: Optimize - if (post.get("email").getAsString().equals(user.getEmail())) { - errormsg += "

An user with this E-mail already exists

"; - break; + String email = post.get("email").getAsString(); + if (!EMAIL_PATTERN.matcher(email).matches()) + errormsg += "

Invalid E-mail address

"; + else { + for (User user : DataManager.getAll(User.class)) { // TODO: Optimize + if (email.equals(user.getEmail())) { + errormsg += "

An user with this E-mail already exists

"; + break; + } } } if (!post.get("pass").getAsString().equals(post.get("pass2").getAsString())) @@ -37,7 +46,7 @@ public class RegisterAjaxPage extends Page { } User user = ManagedData.create(User.class); user.setName(post.get("name").getAsString()); - user.setEmail(post.get("email").getAsString()); + user.setEmail(email); user.setSalt(BCrypt.gensalt()); // http://www.mindrot.org/projects/jBCrypt/ user.setPassword(BCrypt.hashpw(post.get("pass").getAsString(), user.getSalt())); IOHelper.LoginUser(exchange, user);