Small corrections in session start and config

This commit is contained in:
Guille Gutierrez 2023-02-21 10:47:17 -05:00
parent 901fcd461c
commit f56f0b8a17
3 changed files with 57 additions and 49 deletions

View File

@ -3,10 +3,10 @@ Config
SPDX - License - Identifier: LGPL - 3.0 - or -later SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2023 Project Peter Yefi peteryefi@gmail.com Copyright © 2023 Project Peter Yefi peteryefi@gmail.com
""" """
from pathlib import Path
from hub.exports.db_factory import DBFactory as CityExportFactory from hub.exports.db_factory import DBFactory as CityExportFactory
from hub.imports.db_factory import DBFactory 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 os
import pickle import pickle
@ -14,25 +14,14 @@ import pickle
class Config: class Config:
def __init__(self): def __init__(self):
db_name = None dotenv_path = Path('/usr/local/etc/hub/.env').resolve()
app_env = None environment = 'TEST'
if os.getenv("FLASK_DEBUG") == 'production': database_name = 'persistence_test'
db_name = 'hub_prod'
app_env = 'PROD'
elif os.getenv("FLASK_DEBUG") == 'testing':
db_name = 'persistence_test'
app_env = 'TEST'
db_name = 'persistence_test' self.export_db_factory = CityExportFactory(db_name=database_name, app_env=environment,
app_env = 'TEST' dotenv_path=dotenv_path)
self.export_db_factory = CityExportFactory(db_name=db_name, app_env=app_env, self.import_db_factory = DBFactory(db_name=database_name, app_env=environment,
dotenv_path="{}/.env".format(os.path.expanduser('~'))) dotenv_path=dotenv_path)
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('~')))
def get_city(self, city_id): def get_city(self, city_id):
city_obj = self.export_db_factory.get_city(city_id) city_obj = self.export_db_factory.get_city(city_id)

View File

@ -41,23 +41,23 @@ paths:
required: true required: true
description: the password for the user accessing this API description: the password for the user accessing this API
- in: header - in: header
name: application_id name: application_uuid
schema: schema:
type: string type: string
required: true required: true
description: the Id of the application accessing this API description: the uuid of the application accessing this API
tags: tags:
- Session start - Session start
summary: Starts an user session summary: Starts an user session
operationId: session_start 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: responses:
'200': '200':
description: Succeed description: Login succeed
content: content:
application/json: application/json:
schema: schema:
$ref: '#/components/schemas/succeed' $ref: '#/components/schemas/login_succeed'
headers: headers:
session_id: session_id:
type: string type: string
@ -80,7 +80,7 @@ paths:
security: security:
- session_id: [] - session_id: []
- token: [] - token: []
- application_id: [] - application_uuid: []
parameters: parameters:
[] []
tags: tags:
@ -112,7 +112,7 @@ paths:
security: security:
- session_id: [] - session_id: []
- token: [] - token: []
- application_id: [] - application_uuid: []
parameters: parameters:
[] []
tags: tags:
@ -145,10 +145,10 @@ components:
type: apiKey type: apiKey
in: header in: header
name: session_id name: session_id
application_id: application_uuid:
type: apiKey type: apiKey
in: header in: header
name: application_id name: application_uuid
token: token:
type: apiKey type: apiKey
in: header in: header
@ -170,6 +170,15 @@ components:
succeed: succeed:
type: object type: object
properties: properties:
succeed: result:
type: string type: string
example: 'OK' 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'}]

View File

@ -4,16 +4,16 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
""" """
import datetime
import json import json
import uuid 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 import request, Response
from flask_restful import Resource from flask_restful import Resource
from hub.exports.db_factory import DBFactory
from hub_api.config import Config 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): class SessionStart(Resource, Config):
def __init__(self): def __init__(self):
@ -22,25 +22,36 @@ class SessionStart(Resource, Config):
def put(self): def put(self):
username = request.headers.get('username', None) username = request.headers.get('username', None)
password = request.headers.get('password', None) password = request.headers.get('password', None)
application_id = request.headers.get('application_id', None) application_uuid = request.headers.get('application_uuid', None)
ip = request.headers.get('ip', None) ip = request.remote_addr
user_info = self.export_db_factory.user_login(name=username, password=password, application_uuid=application_uuid)
if(self.export_db_factory.user_info(name=username, password=password, application_id=application_id)): if user_info:
session_id = str(uuid.uuid4()) session_id = str(uuid.uuid4())
token = str(uuid.uuid4())
session = { session = {
'username': username, 'user_id': user_info.id,
'token': str(uuid.uuid4()), 'user': username,
'token': token,
'expire': str(datetime.datetime.now() + datetime.timedelta(minutes=5)), 'expire': str(datetime.datetime.now() + datetime.timedelta(minutes=5)),
'application_id': application_id, 'application_id': user_info.application_id,
'ip': ip 'application_uuid': application_uuid,
#'city': sh.city.copy, '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 sessions[session_id] = session
clear_old_sessions() clear_old_sessions()
print(sessions) response = Response(json.dumps({'cities': session['cities'], 'result': 'OK'}), status=200)
return Response(json.dumps({'session_id': session_id, 'session': session}), 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): class SessionEnd(Resource):
def __init__(self): def __init__(self):
@ -55,8 +66,7 @@ class SessionEnd(Resource):
class KeepSessionAlive(Resource): class KeepSessionAlive(Resource):
def __init__(self): def __init__(self):
pass pass
#todo : finish implementing KeepSessionAlive and include error handling for missing invalid session_id or # todo : finish implementing KeepSessionAlive and include error handling for missing invalid session_id or empty sessions
# empty sessions
@staticmethod @staticmethod
def put(): def put():
session = refresh_session(request) session = refresh_session(request)