From 12be9737249eb5b90b07275143b727b46ffa655c Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 22 Jul 2016 14:17:21 +0200 Subject: [PATCH] (Re)Added expire time for cookies (Just deleted today's work) --- .buildpath | 6 +++--- .settings/org.eclipse.vjet.eclipse.core.prefs | 2 ++ src/io/github/norbipeti/chat/server/IOHelper.java | 11 +++++++++++ .../github/norbipeti/chat/server/db/domain/User.java | 10 ++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 .settings/org.eclipse.vjet.eclipse.core.prefs diff --git a/.buildpath b/.buildpath index 52fa783..cf9cdf2 100644 --- a/.buildpath +++ b/.buildpath @@ -1,7 +1,7 @@ - - - + + + diff --git a/.settings/org.eclipse.vjet.eclipse.core.prefs b/.settings/org.eclipse.vjet.eclipse.core.prefs new file mode 100644 index 0000000..46d2bef --- /dev/null +++ b/.settings/org.eclipse.vjet.eclipse.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +initialized_project_from_v4classpath=true diff --git a/src/io/github/norbipeti/chat/server/IOHelper.java b/src/io/github/norbipeti/chat/server/IOHelper.java index 6d0bbdb..e18afd0 100644 --- a/src/io/github/norbipeti/chat/server/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/IOHelper.java @@ -5,6 +5,9 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.time.Period; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.HashMap; import java.util.List; import java.util.UUID; @@ -75,6 +78,14 @@ public class IOHelper { } public static void LoginUser(HttpExchange exchange, User user) { + user.setSessionid(UUID.randomUUID()); + ZonedDateTime expiretime = ZonedDateTime.now(ZoneId.of("GMT")).plus(Period.of(2, 0, 0)); + exchange.getResponseHeaders().add("Set-Cookie", "user_id=" + user.getId() + "; expires=" + expiretime); + exchange.getResponseHeaders().add("Set-Cookie", + "session_id=" + user.getSessionid() + "; expires=" + expiretime); + } + + public static void LogoutUser(HttpExchange exchange, User user) { exchange.getResponseHeaders().add("Set-Cookie", "user_id=" + user.getId()); exchange.getResponseHeaders().add("Set-Cookie", "session_id=" + UUID.randomUUID()); } 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 53f3296..55a4e7e 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/User.java +++ b/src/io/github/norbipeti/chat/server/db/domain/User.java @@ -2,6 +2,7 @@ package io.github.norbipeti.chat.server.db.domain; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import javax.persistence.*; @@ -18,6 +19,7 @@ public class User { @ElementCollection(fetch = FetchType.EAGER) private List contacts; private String salt; + private UUID sessionid; public List getContacts() { if (contacts == null) @@ -78,6 +80,14 @@ public class User { this.salt = salt; } + public UUID getSessionid() { + return sessionid; + } + + public void setSessionid(UUID sessionid) { + this.sessionid = sessionid; + } + public User() { }