2021-03-08 20:22:04 +03:00
|
|
|
from cryptography.fernet import Fernet
|
|
|
|
from flask import current_app as app
|
|
|
|
|
2021-04-01 07:23:30 +03:00
|
|
|
REQUIRED_SESSION_VALUES = ['uuid', 'config', 'key']
|
2021-03-08 20:22:04 +03:00
|
|
|
|
|
|
|
|
2021-04-01 07:23:30 +03:00
|
|
|
def generate_user_key(cookies_disabled=False) -> bytes:
|
|
|
|
"""Generates a key for encrypting searches and element URLs
|
2021-03-08 20:22:04 +03:00
|
|
|
|
|
|
|
Args:
|
|
|
|
cookies_disabled: Flag for whether or not cookies are disabled by the
|
|
|
|
user. If so, the user can only use the default key
|
2021-04-01 07:23:30 +03:00
|
|
|
generated on app init for queries.
|
2021-03-08 20:22:04 +03:00
|
|
|
|
|
|
|
Returns:
|
2021-04-01 07:23:30 +03:00
|
|
|
str: A unique Fernet key
|
2021-03-08 20:22:04 +03:00
|
|
|
|
|
|
|
"""
|
|
|
|
if cookies_disabled:
|
2021-04-01 07:23:30 +03:00
|
|
|
return app.default_key
|
2021-03-08 20:22:04 +03:00
|
|
|
|
|
|
|
# Generate/regenerate unique key per user
|
2021-04-01 07:23:30 +03:00
|
|
|
return Fernet.generate_key()
|
2021-03-08 20:22:04 +03:00
|
|
|
|
|
|
|
|
|
|
|
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
|