auth_decorator.py 1.1 KB
from functools import wraps
from authlib.integrations.flask_oauth2 import current_token
from flask import abort
from app.modules.auth.oauth2 import require_oauth

# 认证装饰器

class auth_decorator(object):
    def __init__(self, action='', permission=''):
        self.permission = permission
        self.action = action

    def __call__(self, func):
        @wraps(func)
        @require_oauth("profile")
        def wrapped_function(*args, **kwargs):
            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:
                pass  # 无token,401

        return wrapped_function