Compare commits

...

1 commit

Author SHA1 Message Date
83ba843bf6
Cookie changes, fixes
- Added domain to allow access from another subdomain
- Changed session ID storage from UUID to String
- Mmmm cookies
2018-03-31 01:08:24 +02:00
3 changed files with 11 additions and 11 deletions

View file

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

View file

@ -33,8 +33,8 @@ public class Cookies extends HashMap<String, Cookie> {
public void AddHeaders(HttpExchange exchange) { public void AddHeaders(HttpExchange exchange) {
for (Entry<String, Cookie> item : entrySet()) for (Entry<String, Cookie> item : entrySet())
exchange.getResponseHeaders().add("Set-Cookie", exchange.getResponseHeaders().add("Set-Cookie",
item.getKey() + "=" + item.getValue().getValue() + "; 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); exchange.getResponseHeaders().add("Set-Cookie", "expiretime=" + expiretime + "; expires=" + expiretime + "; Domain=figytuna.com"); //TODO: Fix header stuff
} }
public Cookies add(Cookie cookie) { 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 { public static void SendResponse(int code, String content, HttpExchange exchange) throws IOException {
if (exchange.getRequestMethod().equalsIgnoreCase("HEAD")) { if (exchange.getRequestMethod().equalsIgnoreCase("HEAD")) {
exchange.sendResponseHeaders(code, -1); // -1 indicates no data exchange.sendResponseHeaders(code, -1); // -1 indicates no data
exchange.getResponseBody().close(); exchange.getResponseBody().close(); //TODO: Response headers not sent yet <--
return; return;
} }
try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) { try (BufferedOutputStream out = new BufferedOutputStream(exchange.getResponseBody())) {
@ -86,15 +86,15 @@ public class IOHelper {
*/ */
public static void LoginUser(HttpExchange exchange, WebUser user) { public static void LoginUser(HttpExchange exchange, WebUser user) {
Bukkit.getLogger().fine("Logging in user: " + user); Bukkit.getLogger().fine("Logging in user: " + user);
user.sessionID().set(UUID.randomUUID()); user.sessionID().set(UUID.randomUUID().toString());
user.save(); user.save();
new Cookies(2).add(new Cookie("user_id", user.getUUID() + "")) 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."); Bukkit.getLogger().fine("Logged in user.");
} }
public static void LogoutUser(HttpExchange exchange, WebUser user) { public static void LogoutUser(HttpExchange exchange, WebUser user) {
user.sessionID().set(new UUID(0, 0)); user.sessionID().set(null);
user.save(); user.save();
SendLogoutHeaders(exchange); SendLogoutHeaders(exchange);
} }
@ -150,7 +150,7 @@ public class IOHelper {
return null; return null;
WebUser user = ChromaGamerBase.getUser(cookies.get("user_id").getValue(), WebUser.class); WebUser user = ChromaGamerBase.getUser(cookies.get("user_id").getValue(), WebUser.class);
if (user != null && cookies.get("session_id") != null 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")))) if (cookies.getExpireTimeParsed().minusYears(1).isBefore(ZonedDateTime.now(ZoneId.of("GMT"))))
LoginUser(exchange, user); LoginUser(exchange, user);
return user; return user;