2023-01-10 12:12:01 -05:00
|
|
|
"""
|
|
|
|
Session
|
|
|
|
SPDX - License - Identifier: LGPL - 3.0 - or -later
|
|
|
|
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
|
|
|
|
"""
|
|
|
|
|
2023-02-21 10:47:17 -05:00
|
|
|
import datetime
|
2023-01-10 12:12:01 -05:00
|
|
|
import json
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from flask import request, Response
|
|
|
|
from flask_restful import Resource
|
2023-02-21 10:47:17 -05:00
|
|
|
|
2023-02-20 22:10:09 -05:00
|
|
|
from hub_api.config import Config
|
2023-02-22 23:14:41 -05:00
|
|
|
from hub_api.helpers.session_helper import remove_session, sessions, refresh_session
|
2023-02-21 10:47:17 -05:00
|
|
|
|
2023-01-10 12:12:01 -05:00
|
|
|
|
2023-02-20 22:10:09 -05:00
|
|
|
class SessionStart(Resource, Config):
|
2023-01-10 12:12:01 -05:00
|
|
|
def __init__(self):
|
2023-02-20 22:10:09 -05:00
|
|
|
super().__init__()
|
2023-01-10 12:12:01 -05:00
|
|
|
|
2023-02-20 22:10:09 -05:00
|
|
|
def put(self):
|
|
|
|
username = request.headers.get('username', None)
|
|
|
|
password = request.headers.get('password', None)
|
2023-04-03 14:51:21 -04:00
|
|
|
application_uuid = request.headers.get('application-uuid', None)
|
2023-02-21 10:47:17 -05:00
|
|
|
ip = request.remote_addr
|
|
|
|
user_info = self.export_db_factory.user_login(name=username, password=password, application_uuid=application_uuid)
|
2023-04-03 14:51:21 -04:00
|
|
|
print(user_info, username, password, application_uuid)
|
2023-02-21 10:47:17 -05:00
|
|
|
if user_info:
|
2023-02-20 22:10:09 -05:00
|
|
|
session_id = str(uuid.uuid4())
|
2023-02-21 10:47:17 -05:00
|
|
|
token = str(uuid.uuid4())
|
2023-02-20 22:10:09 -05:00
|
|
|
session = {
|
2023-02-21 10:47:17 -05:00
|
|
|
'user_id': user_info.id,
|
|
|
|
'user': username,
|
|
|
|
'token': token,
|
2023-02-20 22:10:09 -05:00
|
|
|
'expire': str(datetime.datetime.now() + datetime.timedelta(minutes=5)),
|
2023-02-21 10:47:17 -05:00
|
|
|
'application_id': user_info.application_id,
|
|
|
|
'application_uuid': application_uuid,
|
|
|
|
'ip': ip,
|
|
|
|
'cities': []
|
2023-02-20 22:10:09 -05:00
|
|
|
}
|
2023-03-13 11:56:05 -04:00
|
|
|
cities = self.export_db_factory.cities_by_user_and_application(user_info.id, user_info.application_id)
|
2023-02-21 10:47:17 -05:00
|
|
|
for city in cities:
|
|
|
|
session['cities'].append({
|
|
|
|
"name": city.name,
|
|
|
|
"geometric_level_of_detail": city.level_of_detail
|
|
|
|
})
|
2023-02-20 22:10:09 -05:00
|
|
|
sessions[session_id] = session
|
2023-02-21 10:47:17 -05:00
|
|
|
response = Response(json.dumps({'cities': session['cities'], 'result': 'OK'}), status=200)
|
|
|
|
response.headers['session_id'] = session_id
|
|
|
|
response.headers['token'] = token
|
|
|
|
return response
|
2023-02-20 22:10:09 -05:00
|
|
|
|
2023-02-21 10:47:17 -05:00
|
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
2023-01-10 12:12:01 -05:00
|
|
|
|
|
|
|
class SessionEnd(Resource):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def put():
|
2023-02-22 23:14:41 -05:00
|
|
|
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)
|
2023-02-14 05:37:59 -05:00
|
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
2023-01-10 12:12:01 -05:00
|
|
|
|
|
|
|
class KeepSessionAlive(Resource):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
2023-02-22 23:14:41 -05:00
|
|
|
|
2023-01-10 12:12:01 -05:00
|
|
|
@staticmethod
|
|
|
|
def put():
|
2023-02-22 23:14:41 -05:00
|
|
|
session_id = request.headers.get('session_id', None)
|
|
|
|
token = request.headers.get('token', None)
|
|
|
|
application_uuid = request.headers.get('application_uuid', None)
|
2023-04-12 15:26:56 -04:00
|
|
|
_session = refresh_session(session_id, token, application_uuid)
|
2023-02-22 23:14:41 -05:00
|
|
|
|
2023-04-12 15:26:56 -04:00
|
|
|
if _session is None:
|
2023-02-14 05:37:59 -05:00
|
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
2023-02-20 22:10:09 -05:00
|
|
|
|
2023-02-22 23:14:41 -05:00
|
|
|
response = Response(json.dumps({'result': 'succeed'}), status=200)
|
2023-04-12 15:26:56 -04:00
|
|
|
response.headers['token'] = _session['token']
|
2023-02-22 23:14:41 -05:00
|
|
|
return response
|