Included auth token

This commit is contained in:
Peter Yefi 2023-01-13 12:01:13 -05:00
parent 8c0fed0d9d
commit 6dc6c6ab19
6 changed files with 43 additions and 22 deletions

View File

@ -10,7 +10,7 @@ from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from flask_apispec.extension import FlaskApiSpec
from flask_restful import Api
from hub_api.city_info import CityInfo
from hub_api.city_info import CityInfo, City
from hub_api.geometry import Geometry
from hub_api.greenery_catalog import GreeneryCatalogEntries
from hub_api.greenery_catalog import GreeneryCatalogEntry
@ -60,6 +60,7 @@ api.add_resource(SessionStart, '/v1.4/session/start')
api.add_resource(SessionEnd, '/v1.4/session/end')
api.add_resource(KeepSessionAlive, '/v1.4/session/keep_alive')
api.add_resource(CityInfo, '/v1.4/city_info')
api.add_resource(City, '/v1.4/city')
api.add_resource(Greenery, '/v1.4/greenery')
# Add api documentation
@ -77,3 +78,4 @@ docs = FlaskApiSpec(app)
docs.register(HeatPump)
docs.register(User)
docs.register(UserLogin)
docs.register(City)

View File

@ -4,11 +4,24 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author name guillermo.gutierrezmorote@concordia.ca
"""
import json
from flask_apispec import use_kwargs, doc
from flask import Response, request
from flask_restful import Resource
from hub_api.helpers.session_helper import refresh_session
from marshmallow import fields, Schema
from hub_api.helpers.auth import role_required
from persistence.models import UserRoles
from flask_apispec.views import MethodResource
from hub_logger import logger
class AuthorizationHeader(Schema):
Authorization = fields.Str(required=True, description='Authorization token')
AppID = fields.Str(required=True, description='ID of app accessing API')
class CitySchema(Schema):
city_file = fields.Raw(type='file', required=True, description='City file')
class CityInfo(Resource):
@ -70,3 +83,18 @@ class CityInfo(Resource):
'buildings': buildings
}
return Response(json.dumps(response), headers=headers)
class City(MethodResource, Resource):
@doc(description='Persist a city', tags=['PersistCity'])
@role_required(UserRoles.Admin.value)
@use_kwargs(AuthorizationHeader, location='headers')
@use_kwargs(CitySchema)
def post(self, **kwargs):
try:
print(kwargs)
print(request.files)
return Response(response=json.dumps({'msg': 'Hello'}), status=201)
except Exception as err:
logger.error(err)
return Response(response=json.dumps({'err_msg': 'Sorry an error occurred while creating user'}), status=400)

Binary file not shown.

View File

@ -4,8 +4,7 @@ from jwt import JWT, jwk_from_pem
import os
from jwt.utils import get_int_from_datetime
from functools import wraps
from flask import request
import json
from flask import request, g
from hub_logger import logger
from persistence.models import UserRoles
from jwt.exceptions import JWTException
@ -31,6 +30,7 @@ def validate_auth_token(token: str):
def role_required(role: str):
def auth_module(user):
g.user = user
return user['role'] == role
"""

View File

@ -4,7 +4,6 @@ SPDX - License - Identifier: LGPL - 3.0 - or -later
Copyright © 2022 Project Author Peter Yefi peteryefi@gmail.com
"""
import json
from flask import Response
from flask_apispec import use_kwargs, doc
from flask_apispec.views import MethodResource
@ -19,11 +18,11 @@ from persistence.models import UserRoles
class AuthorizationHeader(Schema):
Authorization = fields.Str(required=True, description='Authorization Token')
AppID = fields.Str(required=True, description='ID of Application Accessing API')
Authorization = fields.Str(required=True, description='Authorization token')
AppID = fields.Str(required=True, description='ID of app accessing API')
class LoginPostData(Schema):
class LoginPostSchema(Schema):
"""
Defines post data for users
"""
@ -31,7 +30,7 @@ class LoginPostData(Schema):
email = fields.String(required=True, description='Email of user')
class UserPostData(LoginPostData):
class UserPostSchema(LoginPostSchema):
"""
Defines post data for users
"""
@ -39,21 +38,13 @@ class UserPostData(LoginPostData):
role = fields.String(required=True, description='Allowed user roles', enum=['Admin', 'Hub_Reader'])
class UserPutData(UserPostData):
class UserPutSchema(UserPostSchema):
"""
Defines put data for users
"""
id = fields.Int(required=True, description='The Id of the user to be Updated')
class UserLoginData(Schema):
"""
Defines post data for users
"""
email = fields.String(required=True, description='Email of user')
password = fields.String(required=True, description='Password of user')
class User(MethodResource, Resource):
def __init__(self):
self.user_factory = UserFactory(db_name='hub_prod', app_env='PROD',
@ -62,7 +53,7 @@ class User(MethodResource, Resource):
@doc(description='Create users', tags=['CreateUser'])
@role_required(UserRoles.Admin.value)
@use_kwargs(AuthorizationHeader, location='headers')
@use_kwargs(UserPostData)
@use_kwargs(UserPostSchema)
def post(self, **kwargs):
try:
@ -77,7 +68,7 @@ class User(MethodResource, Resource):
return Response(response=json.dumps({'err_msg': 'Sorry an error occurred while creating user'}), status=400)
@doc(description='Get all users', tags=['UpdateUsers'])
@use_kwargs(UserPutData)
@use_kwargs(UserPutSchema)
@role_required(UserRoles.Admin.value)
@use_kwargs(AuthorizationHeader, location='headers')
def put(self, **kwargs):
@ -99,7 +90,7 @@ class UserLogin(MethodResource, Resource):
dotenv_path="{}/.env".format(os.path.expanduser('~')))
@doc(description='Create users', tags=['LoginUser'])
@use_kwargs(LoginPostData)
@use_kwargs(LoginPostSchema)
def post(self, **kwargs):
try:
user = self.user_factory.login_user(email=kwargs["email"], password=kwargs["password"])