This introduces a new approach to handling user sessions. Previously, when a user with cookies disabled would update their config, this would modify the app's default config file, which would in turn cause new users to inherit these settings when visiting the app for the first time. There was also some janky logic for determining on the backend whether or not a user had cookies disabled, which lead to some issues with out of control session creation by Flask. Now, when a user visits the site, their initial request is forwarded to a `session/<session id>` endpoint, and during that subsequent request their current session id is matched against the one found in the url. If the ids match, the user has cookies enabled. If not, their original request is modified with a 'cookies_disabled' query param that tells Flask not to bother trying to set up a new session for that user, and instead just use the app's fallback Fernet key for encryption and the default config. Sessions are also now (semi)permanent and have a lifetime of 1 year.
40 lines
1.0 KiB
Python
40 lines
1.0 KiB
Python
from cryptography.fernet import Fernet
|
|
from flask import current_app as app
|
|
|
|
REQUIRED_SESSION_VALUES = ['uuid', 'config', 'key']
|
|
|
|
|
|
def generate_user_key() -> bytes:
|
|
"""Generates a key for encrypting searches and element URLs
|
|
|
|
Args:
|
|
cookies_disabled: Flag for whether or not cookies are disabled by the
|
|
user. If so, the user can only use the default key
|
|
generated on app init for queries.
|
|
|
|
Returns:
|
|
str: A unique Fernet key
|
|
|
|
"""
|
|
# Generate/regenerate unique key per user
|
|
return Fernet.generate_key()
|
|
|
|
|
|
def valid_user_session(session: dict) -> bool:
|
|
"""Validates the current user session
|
|
|
|
Args:
|
|
session: The current Flask user session
|
|
|
|
Returns:
|
|
bool: True/False indicating that all required session values are
|
|
available
|
|
|
|
"""
|
|
# Generate secret key for user if unavailable
|
|
for value in REQUIRED_SESSION_VALUES:
|
|
if value not in session:
|
|
return False
|
|
|
|
return True
|