2023-01-10 12:12:01 -05:00
|
|
|
"""
|
|
|
|
Session helper
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
|
|
|
|
"""
|
|
|
|
import datetime
|
2024-05-13 02:03:26 -04:00
|
|
|
import shutil
|
2023-02-22 23:14:41 -05:00
|
|
|
import time
|
2024-05-13 02:03:26 -04:00
|
|
|
import uuid
|
2024-05-10 13:34:36 -04:00
|
|
|
from copy import deepcopy
|
2024-06-10 00:31:49 -04:00
|
|
|
from glob import glob
|
2024-05-02 11:28:53 -04:00
|
|
|
from pathlib import Path
|
2023-01-10 12:12:01 -05:00
|
|
|
|
2023-02-20 22:10:09 -05:00
|
|
|
sessions = {}
|
2023-01-10 12:12:01 -05:00
|
|
|
begin_time = None
|
2023-01-23 12:29:41 -05:00
|
|
|
swagger_data = None
|
2023-01-10 12:12:01 -05:00
|
|
|
city = None
|
|
|
|
greenery_catalog = None
|
|
|
|
construction_catalog = None
|
|
|
|
usage_catalog = None
|
2023-03-10 12:45:11 -05:00
|
|
|
debug_mode = False
|
|
|
|
|
2023-07-20 20:15:24 -04:00
|
|
|
|
2023-02-23 05:04:40 -05:00
|
|
|
def expired_sessions_collector(session_timeout_duration):
|
2023-02-27 17:50:53 -05:00
|
|
|
"""
|
|
|
|
Goes through each session in sessions and removes expired session(s)
|
|
|
|
"""
|
2023-02-23 04:18:46 -05:00
|
|
|
while True:
|
2023-02-22 23:14:41 -05:00
|
|
|
if bool(sessions):
|
2024-05-10 13:34:36 -04:00
|
|
|
_sessions = deepcopy(sessions)
|
|
|
|
for session_uuid in _sessions:
|
|
|
|
_expire = datetime.datetime.strptime(_sessions[session_uuid]['expire'], '%Y-%m-%d %H:%M:%S.%f')
|
2023-02-22 23:14:41 -05:00
|
|
|
if _expire < datetime.datetime.now():
|
2024-05-10 13:34:36 -04:00
|
|
|
print("session for user: ", _sessions[session_uuid]['user'], "expired.")
|
|
|
|
response_path = (Path(__file__).parent.parent / f'response_files/{session_uuid}').resolve()
|
2024-06-10 00:31:49 -04:00
|
|
|
if response_path.exists():
|
|
|
|
shutil.rmtree(response_path)
|
2023-08-03 10:42:41 -04:00
|
|
|
del sessions[session_uuid]
|
2024-06-10 00:31:49 -04:00
|
|
|
existing_directories = glob(f'{Path(__file__).parent.parent.resolve()}/response_files/*')
|
|
|
|
for directory in existing_directories:
|
|
|
|
if directory not in _sessions.keys():
|
|
|
|
shutil.rmtree(directory)
|
2023-03-10 12:45:11 -05:00
|
|
|
time.sleep(60 * int(session_timeout_duration))
|
|
|
|
|
2023-02-22 23:14:41 -05:00
|
|
|
|
|
|
|
def _validate_session(session_id, token, application_uuid):
|
2023-02-27 17:50:53 -05:00
|
|
|
"""
|
|
|
|
Checks if session is valid
|
|
|
|
"""
|
2023-02-22 23:14:41 -05:00
|
|
|
try:
|
2023-04-12 15:26:56 -04:00
|
|
|
_session = session(session_id)
|
2023-03-10 12:45:11 -05:00
|
|
|
if debug_mode:
|
2023-04-12 15:26:56 -04:00
|
|
|
token = _session['token']
|
2023-07-21 16:59:56 -04:00
|
|
|
return bool(_session) and (_session['token'] == token) and str(_session['application_uuid']) == application_uuid
|
2023-03-10 12:45:11 -05:00
|
|
|
except KeyError:
|
|
|
|
return False
|
2023-02-22 23:14:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
def remove_session(session_id, token, application_uuid):
|
2023-01-10 12:12:01 -05:00
|
|
|
"""
|
|
|
|
Remove a session from the sessions array
|
|
|
|
"""
|
2023-02-22 23:14:41 -05:00
|
|
|
if _validate_session(session_id, token, application_uuid):
|
2023-03-10 12:45:11 -05:00
|
|
|
del sessions[session_id]
|
|
|
|
return True
|
2023-02-22 23:14:41 -05:00
|
|
|
return False
|
2023-01-10 12:12:01 -05:00
|
|
|
|
|
|
|
|
2023-02-22 23:14:41 -05:00
|
|
|
def refresh_session(session_id, token, application_uuid):
|
2023-01-10 12:12:01 -05:00
|
|
|
"""
|
|
|
|
Validate and extend current session
|
2023-02-22 23:14:41 -05:00
|
|
|
:return: valid session
|
2023-01-10 12:12:01 -05:00
|
|
|
"""
|
2023-02-22 23:14:41 -05:00
|
|
|
if _validate_session(session_id, token, application_uuid):
|
2023-02-20 22:10:09 -05:00
|
|
|
sessions[session_id]['expire'] = str(datetime.datetime.now() + datetime.timedelta(minutes=5))
|
2023-02-22 23:14:41 -05:00
|
|
|
sessions[session_id]['token'] = str(uuid.uuid4())
|
2023-04-12 15:26:56 -04:00
|
|
|
return session(session_id)
|
2023-02-20 22:10:09 -05:00
|
|
|
return None
|
2023-02-24 16:57:51 -05:00
|
|
|
|
2023-03-10 12:45:11 -05:00
|
|
|
|
2023-02-24 16:57:51 -05:00
|
|
|
def active_session(session_id, token, application_uuid):
|
2023-03-13 11:56:05 -04:00
|
|
|
_is_valid = _validate_session(session_id=session_id, token=token, application_uuid=application_uuid)
|
|
|
|
if _is_valid:
|
|
|
|
refresh_session(session_id, token, application_uuid)
|
|
|
|
return _is_valid
|
2023-03-13 14:21:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
def session(session_id) -> {}:
|
|
|
|
return sessions[session_id]
|