78 lines
2.5 KiB
Python
78 lines
2.5 KiB
Python
"""
|
|
Session
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
|
|
"""
|
|
|
|
import datetime
|
|
import json
|
|
import uuid
|
|
|
|
from flask import request, Response
|
|
from flask_restful import Resource
|
|
|
|
from hub_api.config import Config
|
|
from hub_api.helpers.session_helper import remove_session, clear_old_sessions, sessions, refresh_session
|
|
|
|
|
|
class SessionStart(Resource, Config):
|
|
def __init__(self):
|
|
super().__init__()
|
|
|
|
def put(self):
|
|
username = request.headers.get('username', None)
|
|
password = request.headers.get('password', None)
|
|
application_uuid = request.headers.get('application_uuid', None)
|
|
ip = request.remote_addr
|
|
user_info = self.export_db_factory.user_login(name=username, password=password, application_uuid=application_uuid)
|
|
if user_info:
|
|
session_id = str(uuid.uuid4())
|
|
token = str(uuid.uuid4())
|
|
session = {
|
|
'user_id': user_info.id,
|
|
'user': username,
|
|
'token': token,
|
|
'expire': str(datetime.datetime.now() + datetime.timedelta(minutes=5)),
|
|
'application_id': user_info.application_id,
|
|
'application_uuid': application_uuid,
|
|
'ip': ip,
|
|
'cities': []
|
|
}
|
|
cities = self.export_db_factory.get_city_by_user(user_info.id)
|
|
for city in cities:
|
|
session['cities'].append({
|
|
"name": city.name,
|
|
"geometric_level_of_detail": city.level_of_detail
|
|
})
|
|
sessions[session_id] = session
|
|
clear_old_sessions()
|
|
response = Response(json.dumps({'cities': session['cities'], 'result': 'OK'}), status=200)
|
|
response.headers['session_id'] = session_id
|
|
response.headers['token'] = token
|
|
return response
|
|
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
|
|
|
class SessionEnd(Resource):
|
|
def __init__(self):
|
|
pass
|
|
|
|
@staticmethod
|
|
def put():
|
|
if remove_session(request):
|
|
return Response(json.dumps({'result': 'succeed'}))
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
|
|
|
class KeepSessionAlive(Resource):
|
|
def __init__(self):
|
|
pass
|
|
# todo : finish implementing KeepSessionAlive and include error handling for missing invalid session_id or empty sessions
|
|
@staticmethod
|
|
def put():
|
|
session = refresh_session(request)
|
|
if session is None:
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
|
|
|
response = {'result': 'succeed'}
|
|
return Response(json.dumps(response), headers=headers)
|