-
Notifications
You must be signed in to change notification settings - Fork 2
/
auth.py
62 lines (51 loc) · 2.07 KB
/
auth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from flask import Flask, request, jsonify, make_response
import hashlib
import logging
from datetime import datetime
from db.redis_cli import get_redis_client, REDIS_PREFIX
logger = logging.getLogger()
redis_client = get_redis_client()
def authenticate_request(func):
async def wrapper(*args, **kwargs):
data = request.get_json()
if not data or 'user' not in data or 'timestamp' not in data or 'text' not in data or 'md5hash' not in data:
logger.error('Invalid request')
response = {'status': False, 'text': 'Invalid request'}
return make_response(jsonify(response), 400)
user_key = await redis_client.get(REDIS_PREFIX + data['user'])
if not user_key:
logger.error(f"User not found: {data['user']}")
response = {'status': False, 'text': 'user not found'}
return make_response(jsonify(response), 401)
raw_string = f"{data['user']}{user_key}{data['timestamp']}{data['text']}".encode()
md5hash = hashlib.md5(raw_string).hexdigest()
if md5hash == data['md5hash']:
return func(*args, **kwargs)
else:
logger.error(f"Authentication failed for user: {data['user']}")
response = {'status': False, 'text': 'authentication failed'}
return make_response(jsonify(response), 401)
wrapper.__name__ = func.__name__
return wrapper
async def gen_md5hash(user, timestamp, text):
# 获取存储于Redis的密钥
user_key = await redis_client.get(REDIS_PREFIX + user)
if not user_key:
logger.error(f"User not found: {user}")
return ''
# 生成MD5散列
raw_string = f"{user}{user_key}{timestamp}{text}".encode()
md5hash = hashlib.md5(raw_string).hexdigest()
return md5hash
async def gen_post_request(user, text):
timestamp = datetime.now().timestamp()
md5hash = await gen_md5hash(user, timestamp, text)
if not md5hash:
return None
data = {
'user': user,
'timestamp': timestamp,
'text': text,
'md5hash': md5hash
}
return data