Authorization装饰器
from functools import wraps
from sanic.response import json
from settings import AUTH
from utils.status import Status
def authenticator(key):
"""
:param keys: 验证方式 Owllook-Api-Key : Maginc Key, Authorization : Token
:return: 返回值
"""
def wrapper(func):
@wraps(func)
async def authenticate(request, *args, **kwargs):
value = args[0].headers.get(key, None)
if value and AUTH[key] == value:
response = await func(request, *args, **kwargs)
return response
else:
return json({'msg': Status.TOKEN_ERROR.get_msg(), 'code': Status.TOKEN_ERROR.get_code()})
return authenticate
return wrapper