From 7c2c4400dfce3fac5ff44fbcc187472278c01206 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 22 Jul 2016 14:45:45 +0200 Subject: [PATCH] Added login check (test it) --- .../norbipeti/chat/server/IOHelper.java | 6 +++- .../norbipeti/chat/server/page/LoginPage.java | 29 ++++++++++++++++++- .../chat/server/page/RegisterPage.java | 5 ++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java index 73347c3..17fee30 100644 --- a/src/io/github/norbipeti/chat/server/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -10,7 +10,6 @@ import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.HashMap; -import java.util.List; import java.util.UUID; import org.apache.commons.io.IOUtils; @@ -93,4 +92,9 @@ public class IOHelper { exchange.getResponseHeaders().add("Set-Cookie", "user_id=del; expires=" + expiretime); exchange.getResponseHeaders().add("Set-Cookie", "session_id=del; expires=" + expiretime); } + + public static void Redirect(String url, HttpExchange exchange) throws IOException { + exchange.getResponseHeaders().add("Location", url); + IOHelper.SendResponse(303, "If you can see this, click here to continue", exchange); + } } diff --git a/src/io/github/norbipeti/chat/server/page/LoginPage.java b/src/io/github/norbipeti/chat/server/page/LoginPage.java index 7e8f080..675d13d 100644 --- a/src/io/github/norbipeti/chat/server/page/LoginPage.java +++ b/src/io/github/norbipeti/chat/server/page/LoginPage.java @@ -1,16 +1,43 @@ package io.github.norbipeti.chat.server.page; import java.io.IOException; +import java.util.HashMap; + +import org.mindrot.jbcrypt.BCrypt; import com.sun.net.httpserver.HttpExchange; import io.github.norbipeti.chat.server.IOHelper; +import io.github.norbipeti.chat.server.db.DataProvider; +import io.github.norbipeti.chat.server.db.domain.User; public class LoginPage extends Page { @Override public void handlePage(HttpExchange exchange) throws IOException { - IOHelper.SendPage(200, this, exchange); + HashMap post = IOHelper.GetPOST(exchange); + if (post.size() == 0 || !post.containsKey("email") || !post.containsKey("pass")) { + IOHelper.SendPage(200, this, exchange); + return; + } + try (DataProvider provider = new DataProvider()) { + User loginuser = null; + for (User user : provider.getUsers()) { + if (user.getEmail().equals(post.get("email"))) { + loginuser = user; + break; + } + } + if (loginuser == null || !BCrypt.checkpw(post.get("pass"), loginuser.getPassword())) { + IOHelper.SendModifiedPage(200, this, "", "

The E-mail or password is incorrect

", + exchange); + return; + } + IOHelper.LoginUser(exchange, loginuser); + IOHelper.Redirect("/", exchange); + } catch (Exception e) { + throw e; + } } @Override diff --git a/src/io/github/norbipeti/chat/server/page/RegisterPage.java b/src/io/github/norbipeti/chat/server/page/RegisterPage.java index 60df7c3..b11c84e 100644 --- a/src/io/github/norbipeti/chat/server/page/RegisterPage.java +++ b/src/io/github/norbipeti/chat/server/page/RegisterPage.java @@ -38,11 +38,10 @@ public class RegisterPage extends Page { user.setName(post.get("name")); user.setEmail(post.get("email")); user.setSalt(BCrypt.gensalt()); // http://www.mindrot.org/projects/jBCrypt/ - user.setPassword(BCrypt.hashpw(post.get("password"), user.getSalt())); + user.setPassword(BCrypt.hashpw(post.get("pass"), user.getSalt())); provider.addUser(user); IOHelper.LoginUser(exchange, user); - exchange.getResponseHeaders().add("Location", "/"); - IOHelper.SendResponse(303, "If you can see this, click here to continue", exchange); + IOHelper.Redirect("/", exchange); } return; // TODO: Only show tag when needed }