From 9eaca9527783d88e176119e7b2f436a374159357 Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Sat, 30 Nov 2024 04:11:28 +0100 Subject: [PATCH] WebHost: add a page to manage session cookie (#4173) --- WebHostLib/__init__.py | 2 +- WebHostLib/misc.py | 7 ------- WebHostLib/session.py | 31 +++++++++++++++++++++++++++++++ WebHostLib/templates/session.html | 30 ++++++++++++++++++++++++++++++ WebHostLib/templates/siteMap.html | 1 + 5 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 WebHostLib/session.py create mode 100644 WebHostLib/templates/session.html diff --git a/WebHostLib/__init__.py b/WebHostLib/__init__.py index dbe2182b..9b2b6736 100644 --- a/WebHostLib/__init__.py +++ b/WebHostLib/__init__.py @@ -85,6 +85,6 @@ def register(): from WebHostLib.customserver import run_server_process # to trigger app routing picking up on it - from . import tracker, upload, landing, check, generate, downloads, api, stats, misc, robots, options + from . import tracker, upload, landing, check, generate, downloads, api, stats, misc, robots, options, session app.register_blueprint(api.api_endpoints) diff --git a/WebHostLib/misc.py b/WebHostLib/misc.py index c49b1ae1..6be0e470 100644 --- a/WebHostLib/misc.py +++ b/WebHostLib/misc.py @@ -18,13 +18,6 @@ def get_world_theme(game_name: str): return 'grass' -@app.before_request -def register_session(): - session.permanent = True # technically 31 days after the last visit - if not session.get("_id", None): - session["_id"] = uuid4() # uniquely identify each session without needing a login - - @app.errorhandler(404) @app.errorhandler(jinja2.exceptions.TemplateNotFound) def page_not_found(err): diff --git a/WebHostLib/session.py b/WebHostLib/session.py new file mode 100644 index 00000000..d5dab7d6 --- /dev/null +++ b/WebHostLib/session.py @@ -0,0 +1,31 @@ +from uuid import uuid4, UUID + +from flask import session, render_template + +from WebHostLib import app + + +@app.before_request +def register_session(): + session.permanent = True # technically 31 days after the last visit + if not session.get("_id", None): + session["_id"] = uuid4() # uniquely identify each session without needing a login + + +@app.route('/session') +def show_session(): + return render_template( + "session.html", + ) + + +@app.route('/session/') +def set_session(_id: str): + new_id: UUID = UUID(_id, version=4) + old_id: UUID = session["_id"] + if old_id != new_id: + session["_id"] = new_id + return render_template( + "session.html", + old_id=old_id, + ) diff --git a/WebHostLib/templates/session.html b/WebHostLib/templates/session.html new file mode 100644 index 00000000..b7547448 --- /dev/null +++ b/WebHostLib/templates/session.html @@ -0,0 +1,30 @@ +{% extends 'pageWrapper.html' %} + +{% block head %} + {% include 'header/stoneHeader.html' %} + Session + +{% endblock %} + +{% block body %} +
+ {% if old_id is defined %} +

Your old code was:

+ {{ old_id }} +
+ {% endif %} +

The following code is your unique identifier, it binds your uploaded content, such as rooms and seeds to you. + Treat it like a combined login name and password. + You should save this securely if you ever need to restore access. + You can also paste it into another device to access your content from multiple devices / browsers. + Some browsers, such as Brave, will delete your identifier cookie on a timer.

+ {{ session["_id"] }} +
+

+ The following link can be used to set the identifier. Do not share the code or link with others.
+ + {{ url_for('set_session', _id=session['_id'], _external=True) }} + +

+
+{% endblock %} diff --git a/WebHostLib/templates/siteMap.html b/WebHostLib/templates/siteMap.html index cdd6ad45..b7db8227 100644 --- a/WebHostLib/templates/siteMap.html +++ b/WebHostLib/templates/siteMap.html @@ -26,6 +26,7 @@
  • User Content
  • Game Statistics
  • Glossary
  • +
  • Session / Login
  • Tutorials