api_v1.4/hub_api/control/session.py
guille 48a7f28404 Bug fix in meb session
Added new reverse method to remove dependency from openstreet maps
2023-04-12 15:26:56 -04:00

86 lines
2.8 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, 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.cities_by_user_and_application(user_info.id, user_info.application_id)
for city in cities:
session['cities'].append({
"name": city.name,
"geometric_level_of_detail": city.level_of_detail
})
sessions[session_id] = session
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():
session_id = request.headers.get('session_id', None)
token = request.headers.get('token', None)
application_uuid = request.headers.get('application_uuid', None)
if remove_session(session_id, token, application_uuid):
return Response(json.dumps({'result': 'succeed'}), status=200)
return Response(json.dumps({'error': 'unauthorized'}), status=403)
class KeepSessionAlive(Resource):
def __init__(self):
pass
@staticmethod
def put():
session_id = request.headers.get('session_id', None)
token = request.headers.get('token', None)
application_uuid = request.headers.get('application_uuid', None)
_session = refresh_session(session_id, token, application_uuid)
if _session is None:
return Response(json.dumps({'error': 'unauthorized'}), status=403)
response = Response(json.dumps({'result': 'succeed'}), status=200)
response.headers['token'] = _session['token']
return response