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-07-21 16:59:56 -04:00
|
|
|
try:
|
|
|
|
application_uuid = uuid.UUID(request.headers.get('application-uuid', None))
|
|
|
|
user_info = self.database.user_login(name=username, password=password, application_uuid=application_uuid)
|
|
|
|
except ValueError:
|
|
|
|
return Response(json.dumps({'error': 'unauthorized'}), status=403)
|
2023-02-21 10:47:17 -05:00
|
|
|
ip = request.remote_addr
|
|
|
|
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,
|
2023-07-28 08:26:02 -04:00
|
|
|
'scenarios': []
|
2023-02-20 22:10:09 -05:00
|
|
|
}
|
2023-07-21 16:59:56 -04:00
|
|
|
cities = self.database.cities_by_user_and_application(user_info.id, user_info.application_id)
|
2023-02-21 10:47:17 -05:00
|
|
|
for city in cities:
|
2023-07-28 08:26:02 -04:00
|
|
|
if city.scenario not in session['scenarios']:
|
|
|
|
session['scenarios'].append(city.scenario)
|
2023-02-20 22:10:09 -05:00
|
|
|
sessions[session_id] = session
|
2023-07-28 08:26:02 -04:00
|
|
|
response = Response(json.dumps({'scenarios': session['scenarios'], 'result': 'OK'}), status=200)
|
2023-02-21 10:47:17 -05:00
|
|
|
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
|
|
|
|
2023-07-21 11:43:52 -04:00
|
|
|
|
2023-01-10 12:12:01 -05:00
|
|
|
class SessionEnd(Resource):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def put():
|
2023-07-21 16:59:56 -04:00
|
|
|
session_id = request.headers.get('session-id', None)
|
2023-02-22 23:14:41 -05:00
|
|
|
token = request.headers.get('token', None)
|
2023-07-21 16:59:56 -04:00
|
|
|
application_uuid = request.headers.get('application-uuid', None)
|
2023-02-22 23:14:41 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
2023-07-21 11:43:52 -04:00
|
|
|
|
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-07-21 16:59:56 -04:00
|
|
|
session_id = request.headers.get('session-id', None)
|
2023-02-22 23:14:41 -05:00
|
|
|
token = request.headers.get('token', None)
|
2023-07-21 16:59:56 -04:00
|
|
|
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
|