auth_decorator.py
1.6 KB
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
from functools import wraps
from authlib.integrations.flask_oauth2 import current_token
from flask import abort
from app.modules.auth.oauth2 import require_oauth
from flask import request
import configure
# 认证装饰器
class auth_decorator(object):
def __init__(self, action='', permission='', scope='profile'):
self.permission = permission
self.action = action
self.scope = scope
def __call__(self, func):
@wraps(func)
def wrapped_function(*args, **kwargs):
if configure.PermissionActive:
token = request.headers.get('Authorization')
if not token:
abort(401)
validate_token()
if current_token and current_token.user and current_token.user.role:
print(func.__name__)
if self.permission and len(self.permission) > 0:
# 判断角色是否在permission列表中
role = current_token.user.role
for p in self.permission:
if role == p:
return func(*args, **kwargs)
abort(403)
else:
# 无permission,不校验
return func(*args, **kwargs)
else:
abort(401) # 无token,401
else:
return func(*args, **kwargs)
@require_oauth(self.scope)
def validate_token():
pass
return wrapped_function