From 2486a97271caf5cde1a978d9a810772a00a37fd3 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Mon, 25 Jul 2016 14:54:40 +0200 Subject: [PATCH] Added DataProvider.SetValues, improvde User.toString --- .../norbipeti/chat/server/IOHelper.java | 22 +++++++++++++++---- .../chat/server/db/DataProvider.java | 17 ++++++++++++-- .../norbipeti/chat/server/db/domain/User.java | 11 ++++++---- .../norbipeti/chat/server/page/LoginPage.java | 2 +- .../chat/server/page/RegisterPage.java | 4 +++- 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java index cc97b35..1f2b258 100644 --- a/src/io/github/norbipeti/chat/server/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -86,8 +86,9 @@ public class IOHelper { return true; } - public static void LoginUser(HttpExchange exchange, User user) { - user.setSessionid(UUID.randomUUID()); + public static void LoginUser(HttpExchange exchange, User user, DataProvider provider) { + provider.SetValues(() -> user.setSessionid(UUID.randomUUID())); + System.out.println("Logging in user: " + user); ZonedDateTime expiretime = ZonedDateTime.now(ZoneId.of("GMT")).plus(Period.of(2, 0, 0)); exchange.getResponseHeaders().add("Set-Cookie", "user_id=" + user.getId() + "; expires=" + expiretime.format(DateTimeFormatter.RFC_1123_DATE_TIME)); @@ -112,7 +113,7 @@ public class IOHelper { return new HashMap<>(); HashMap map = new HashMap<>(); for (String cheader : exchange.getRequestHeaders().get("Cookie")) { - String[] spl = cheader.split("\\;\\S"); + String[] spl = cheader.split("\\;\\s*"); for (String s : spl) { String[] kv = s.split("\\="); if (kv.length < 2) @@ -123,14 +124,27 @@ public class IOHelper { return map; } + /** + * Get logged in user. It may also send logout headers if the cookies are + * invalid. + * + * @param exchange + * @return The logged in user or null if not logged in. + */ public static User GetLoggedInUser(HttpExchange exchange) { HashMap cookies = GetCookies(exchange); + System.out.println("Cookies: " + cookies); if (!cookies.containsKey("user_id") || !cookies.containsKey("session_id")) return null; + System.out.println("Cookies found"); try (DataProvider provider = new DataProvider()) { User user = provider.getUser(Long.parseLong(cookies.get("user_id"))); + System.out.println("User: " + user); + System.out.println("session_id: " + cookies.get("session_id")); + if (user != null) + System.out.println("Equals: " + UUID.fromString(cookies.get("session_id")).equals(user.getSessionid())); if (user != null && cookies.get("session_id") != null - && cookies.get("session_id").equals(user.getSessionid())) + && UUID.fromString(cookies.get("session_id")).equals(user.getSessionid())) return user; } return null; diff --git a/src/io/github/norbipeti/chat/server/db/DataProvider.java b/src/io/github/norbipeti/chat/server/db/DataProvider.java index 7d7fac0..a8c2c46 100644 --- a/src/io/github/norbipeti/chat/server/db/DataProvider.java +++ b/src/io/github/norbipeti/chat/server/db/DataProvider.java @@ -1,6 +1,7 @@ package io.github.norbipeti.chat.server.db; import java.util.List; +import java.util.function.Consumer; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; @@ -58,10 +59,22 @@ public class DataProvider implements AutoCloseable { } public User getUser(Long id) { + return get(User.class, id); + } + + private T get(Class cl, Long id) { EntityManager em = emf.createEntityManager(); - User managedUser = em.find(User.class, id); + T result = em.find(cl, id); + em.close(); + return result; + } + + public void SetValues(Runnable action) { + EntityManager em = emf.createEntityManager(); + em.getTransaction().begin(); + action.run(); + em.getTransaction().commit(); em.close(); - return managedUser; } @Override diff --git a/src/io/github/norbipeti/chat/server/db/domain/User.java b/src/io/github/norbipeti/chat/server/db/domain/User.java index 55a4e7e..918fc10 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/User.java +++ b/src/io/github/norbipeti/chat/server/db/domain/User.java @@ -45,11 +45,14 @@ public class User { @Override public String toString() { - List c = new ArrayList<>(); - for (User u : contacts) - c.add(u.name); + List c = null; + if (contacts != null) { + c = new ArrayList<>(); + for (User u : contacts) + c.add(u.name); + } return "User [id=" + id + ", name=" + name + ", email=" + email + ", password=" + password + ", contacts=" + c - + "]"; + + ", sessionid=" + sessionid + "]"; //TODO: SessionID null after getting from db } public void setEmail(String email) { diff --git a/src/io/github/norbipeti/chat/server/page/LoginPage.java b/src/io/github/norbipeti/chat/server/page/LoginPage.java index 3094398..6145353 100644 --- a/src/io/github/norbipeti/chat/server/page/LoginPage.java +++ b/src/io/github/norbipeti/chat/server/page/LoginPage.java @@ -39,7 +39,7 @@ public class LoginPage extends Page { }, exchange); return; } - IOHelper.LoginUser(exchange, loginuser); + IOHelper.LoginUser(exchange, loginuser, provider); IOHelper.Redirect("/", exchange); } catch (Exception e) { throw e; diff --git a/src/io/github/norbipeti/chat/server/page/RegisterPage.java b/src/io/github/norbipeti/chat/server/page/RegisterPage.java index 9c63425..3284ed2 100644 --- a/src/io/github/norbipeti/chat/server/page/RegisterPage.java +++ b/src/io/github/norbipeti/chat/server/page/RegisterPage.java @@ -44,8 +44,10 @@ public class RegisterPage extends Page { user.setSalt(BCrypt.gensalt()); // http://www.mindrot.org/projects/jBCrypt/ user.setPassword(BCrypt.hashpw(post.get("pass"), user.getSalt())); provider.addUser(user); - IOHelper.LoginUser(exchange, user); + IOHelper.LoginUser(exchange, user, provider); IOHelper.Redirect("/", exchange); + } catch (Exception e) { + throw e; } return; }