Added DataProvider.SetValues, improvde User.toString

This commit is contained in:
Norbi Peti 2016-07-25 14:54:40 +02:00
parent 4ea179ed91
commit 2486a97271
5 changed files with 44 additions and 12 deletions

View file

@ -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<String, String> 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<String, String> 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;

View file

@ -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> T get(Class<T> 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

View file

@ -45,11 +45,14 @@ public class User {
@Override
public String toString() {
List<String> c = new ArrayList<>();
for (User u : contacts)
c.add(u.name);
List<String> 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) {

View file

@ -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;

View file

@ -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;
}