2021-01-20 16:24:01 +01:00
|
|
|
from functools import wraps
|
|
|
|
from typing import Any, Callable, Union
|
|
|
|
|
|
|
|
from flask import request
|
|
|
|
|
2021-01-20 16:47:00 +01:00
|
|
|
from fittrackee.responses import HttpResponse
|
|
|
|
|
2021-01-20 16:24:01 +01:00
|
|
|
from .utils import verify_user
|
|
|
|
|
|
|
|
|
|
|
|
def authenticate(f: Callable) -> Callable:
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(
|
|
|
|
*args: Any, **kwargs: Any
|
|
|
|
) -> Union[Callable, HttpResponse]:
|
2021-12-01 19:22:47 +01:00
|
|
|
response_object, user = verify_user(request, verify_admin=False)
|
2021-01-20 16:24:01 +01:00
|
|
|
if response_object:
|
|
|
|
return response_object
|
2021-12-01 19:22:47 +01:00
|
|
|
return f(user, *args, **kwargs)
|
2021-01-20 16:24:01 +01:00
|
|
|
|
|
|
|
return decorated_function
|
|
|
|
|
|
|
|
|
|
|
|
def authenticate_as_admin(f: Callable) -> Callable:
|
|
|
|
@wraps(f)
|
|
|
|
def decorated_function(
|
|
|
|
*args: Any, **kwargs: Any
|
|
|
|
) -> Union[Callable, HttpResponse]:
|
2021-12-01 19:22:47 +01:00
|
|
|
response_object, user = verify_user(request, verify_admin=True)
|
2021-01-20 16:24:01 +01:00
|
|
|
if response_object:
|
|
|
|
return response_object
|
2021-12-01 19:22:47 +01:00
|
|
|
return f(user, *args, **kwargs)
|
2021-01-20 16:24:01 +01:00
|
|
|
|
|
|
|
return decorated_function
|