tfse-admin-api-v0.2/common/security/APIAuth.py

55 lines
1.4 KiB
Python
Raw Normal View History

2022-01-11 11:02:46 +08:00
import functools
from flask import request
2022-01-11 15:12:08 +08:00
from common.db import find_data
2022-01-11 11:02:46 +08:00
def api_secret(func):
"""
校验接口请求密钥
"""
2022-01-11 15:12:08 +08:00
secret = "EZgo9ykxrYuBMYnYmmKIh" # 接口密钥
2022-01-11 11:02:46 +08:00
@functools.wraps(func)
def internal(*args, **kwargs):
try:
token = request.headers.get('secret')
2022-01-11 15:12:08 +08:00
if token != secret:
2022-01-11 11:02:46 +08:00
return {"info": "接口密钥错误"}, 401
except Exception:
return {"info": "请求异常"}, 401
return func(*args, **kwargs)
return internal
2022-01-11 15:12:08 +08:00
def api_verification_code(func):
"""
检查验证码
"""
v_client = "tfse_admin" # 校验数据库服务
v_database = "用户" # 校验数据库名称
v_collection = "验证记录" # 校验数据表名称
@functools.wraps(func)
def internal(*args, **kwargs):
try:
email = request.json['email']
verification_code = request.json['verification_code']
res = find_data(v_client, v_database, v_collection, {"email": email})
if len(res) == 0:
return {"info": "验证码错误"}, 401
if res[0]['verification_code'] == verification_code:
pass
else:
return {"info": "验证码错误"}, 401
except Exception:
return {"info": "请求异常"}, 401
return func(*args, **kwargs)
return internal