Cookie changes, fixes

- Added domain to allow access from another subdomain
- Changed session ID storage from UUID to String
- Mmmm cookies
This commit is contained in:
Norbi Peti 2018-03-31 01:08:24 +02:00
parent 919e57a2a9
commit 83ba843bf6
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
3 changed files with 11 additions and 11 deletions

View file

@ -1,11 +1,11 @@
package buttondevteam.website;
import java.util.UUID;
import buttondevteam.lib.player.ChromaGamerBase;
import buttondevteam.lib.player.PlayerData;
import buttondevteam.lib.player.UserClass;
import java.util.UUID;
@UserClass(foldername = "web")
public class WebUser extends ChromaGamerBase {
private UUID uuid;
@ -16,7 +16,7 @@ public class WebUser extends ChromaGamerBase {
return uuid;
}
public PlayerData<UUID> sessionID() {
return data(new UUID(0, 0)); //It's used with toString() directly, so can't be null
public PlayerData<String> sessionID() {
return data(null);
}
}

View file

@ -33,8 +33,8 @@ public class Cookies extends HashMap<String, Cookie> {
public void AddHeaders(HttpExchange exchange) {
for (Entry<String, Cookie> item : entrySet())
exchange.getResponseHeaders().add("Set-Cookie",
item.getKey() + "=" + item.getValue().getValue() + "; expires=" + expiretime);
exchange.getResponseHeaders().add("Set-Cookie", "expiretime=" + expiretime + "; expires=" + expiretime);
item.getKey() + "=" + item.getValue().getValue() + "; expires=" + expiretime + "; Domain=figytuna.com"); //chromagaming.figytuna.com too (commented so that I can find it later) - server.figytuna.com
exchange.getResponseHeaders().add("Set-Cookie", "expiretime=" + expiretime + "; expires=" + expiretime + "; Domain=figytuna.com"); //TODO: Fix header stuff
}
public Cookies add(Cookie cookie) {

View file

@ -32,7 +32,7 @@ public class IOHelper {
public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException {
if (exchange.getRequestMethod().equalsIgnoreCase("HEAD")) {
exchange.sendResponseHeaders(code, -1); // -1 indicates no data
exchange.getResponseBody().close();
exchange.getResponseBody().close(); //TODO: Response headers not sent yet <--
return;
}
try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) {
@ -86,15 +86,15 @@ public class IOHelper {
*/
public static void LoginUser(HttpExchange exchange, WebUser user) {
Bukkit.getLogger().fine("Logging in user: " + user);
user.sessionID().set(UUID.randomUUID());
user.sessionID().set(UUID.randomUUID().toString());
user.save();
new Cookies(2).add(new Cookie("user_id", user.getUUID() + ""))
.add(new Cookie("session_id", user.sessionID().get().toString())).AddHeaders(exchange);
.add(new Cookie("session_id", user.sessionID().get())).AddHeaders(exchange);
Bukkit.getLogger().fine("Logged in user.");
}
public static void LogoutUser(HttpExchange exchange, WebUser user) {
user.sessionID().set(new UUID(0, 0));
user.sessionID().set(null);
user.save();
SendLogoutHeaders(exchange);
}
@ -150,7 +150,7 @@ public class IOHelper {
return null;
WebUser user = ChromaGamerBase.getUser(cookies.get("user_id").getValue(), WebUser.class);
if (user != null && cookies.get("session_id") != null
&& cookies.get("session_id").getValue().equals(user.sessionID().get().toString())) {
&& cookies.get("session_id").getValue().equals(user.sessionID().get())) {
if (cookies.getExpireTimeParsed().minusYears(1).isBefore(ZonedDateTime.now(ZoneId.of("GMT"))))
LoginUser(exchange, user);
return user;