From f56f0b8a179f6c411fbedd0ed693304fb7dc0c1c Mon Sep 17 00:00:00 2001 From: guille Date: Tue, 21 Feb 2023 10:47:17 -0500 Subject: [PATCH] Small corrections in session start and config --- hub_api/config.py | 29 +++++++-------------- hub_api/docs/openapi-specs.yml | 31 +++++++++++++++-------- hub_api/session.py | 46 +++++++++++++++++++++------------- 3 files changed, 57 insertions(+), 49 deletions(-) diff --git a/hub_api/config.py b/hub_api/config.py index 6b9a687..8b697b3 100644 --- a/hub_api/config.py +++ b/hub_api/config.py @@ -3,10 +3,10 @@ Config SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2023 Project Peter Yefi peteryefi@gmail.com """ +from pathlib import Path + from hub.exports.db_factory import DBFactory as CityExportFactory from hub.imports.db_factory import DBFactory -from hub.imports.user_factory import UserFactory -from hub.exports.user_factory import UserFactory as ExUserFactory import os import pickle @@ -14,25 +14,14 @@ import pickle class Config: def __init__(self): - db_name = None - app_env = None - if os.getenv("FLASK_DEBUG") == 'production': - db_name = 'hub_prod' - app_env = 'PROD' - elif os.getenv("FLASK_DEBUG") == 'testing': - db_name = 'persistence_test' - app_env = 'TEST' + dotenv_path = Path('/usr/local/etc/hub/.env').resolve() + environment = 'TEST' + database_name = 'persistence_test' - db_name = 'persistence_test' - app_env = 'TEST' - self.export_db_factory = CityExportFactory(db_name=db_name, app_env=app_env, - dotenv_path="{}/.env".format(os.path.expanduser('~'))) - self.import_db_factory = DBFactory(db_name=db_name, app_env=app_env, - dotenv_path="{}/.env".format(os.path.expanduser('~'))) - self.user_factory = UserFactory(db_name=db_name, app_env=app_env, - dotenv_path="{}/.env".format(os.path.expanduser('~'))) - self.ex_user_factory = ExUserFactory(db_name=db_name, app_env=app_env, - dotenv_path="{}/.env".format(os.path.expanduser('~'))) + self.export_db_factory = CityExportFactory(db_name=database_name, app_env=environment, + dotenv_path=dotenv_path) + self.import_db_factory = DBFactory(db_name=database_name, app_env=environment, + dotenv_path=dotenv_path) def get_city(self, city_id): city_obj = self.export_db_factory.get_city(city_id) diff --git a/hub_api/docs/openapi-specs.yml b/hub_api/docs/openapi-specs.yml index a002d0f..439eb68 100644 --- a/hub_api/docs/openapi-specs.yml +++ b/hub_api/docs/openapi-specs.yml @@ -41,23 +41,23 @@ paths: required: true description: the password for the user accessing this API - in: header - name: application_id + name: application_uuid schema: type: string required: true - description: the Id of the application accessing this API + description: the uuid of the application accessing this API tags: - Session start summary: Starts an user session operationId: session_start - description: Authentication and initialisation of a user session in the Api + description: Authentication and initialization of a user session in the Api responses: '200': - description: Succeed + description: Login succeed content: application/json: schema: - $ref: '#/components/schemas/succeed' + $ref: '#/components/schemas/login_succeed' headers: session_id: type: string @@ -80,7 +80,7 @@ paths: security: - session_id: [] - token: [] - - application_id: [] + - application_uuid: [] parameters: [] tags: @@ -112,7 +112,7 @@ paths: security: - session_id: [] - token: [] - - application_id: [] + - application_uuid: [] parameters: [] tags: @@ -145,10 +145,10 @@ components: type: apiKey in: header name: session_id - application_id: + application_uuid: type: apiKey in: header - name: application_id + name: application_uuid token: type: apiKey in: header @@ -170,6 +170,15 @@ components: succeed: type: object properties: - succeed: + result: type: string - example: 'OK' \ No newline at end of file + example: 'OK' + login_succeed: + type: object + properties: + result: + type: string + example: 'OK' + cities: + type: array + example: [{'name': 'city 1', 'geometric_level_of_detail': '1'}, {'name': 'city 2', 'geometric_level_of_detail': '1'}] \ No newline at end of file diff --git a/hub_api/session.py b/hub_api/session.py index 27cacd7..71381ac 100644 --- a/hub_api/session.py +++ b/hub_api/session.py @@ -4,16 +4,16 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca """ +import datetime import json import uuid -import datetime -from hub_api.helpers.session_helper import remove_session, clear_old_sessions, sessions, refresh_session -import hub_api.helpers.session_helper as sh from flask import request, Response from flask_restful import Resource -from hub.exports.db_factory import DBFactory + 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): @@ -22,25 +22,36 @@ class SessionStart(Resource, Config): def put(self): username = request.headers.get('username', None) password = request.headers.get('password', None) - application_id = request.headers.get('application_id', None) - ip = request.headers.get('ip', None) - - if(self.export_db_factory.user_info(name=username, password=password, application_id=application_id)): + 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 = { - 'username': username, - 'token': str(uuid.uuid4()), + 'user_id': user_info.id, + 'user': username, + 'token': token, 'expire': str(datetime.datetime.now() + datetime.timedelta(minutes=5)), - 'application_id': application_id, - 'ip': ip - #'city': sh.city.copy, + '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() - print(sessions) - return Response(json.dumps({'session_id': session_id, 'session': session}), status=200) + 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({'message': 'invalid credentials'}), status=401) + return Response(json.dumps({'error': 'unauthorized'}), status=403) class SessionEnd(Resource): def __init__(self): @@ -55,8 +66,7 @@ class SessionEnd(Resource): class KeepSessionAlive(Resource): def __init__(self): pass - #todo : finish implementing KeepSessionAlive and include error handling for missing invalid session_id or - # empty sessions + # todo : finish implementing KeepSessionAlive and include error handling for missing invalid session_id or empty sessions @staticmethod def put(): session = refresh_session(request)