token_decorator.py 1.0 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
from flask import request
import configure
# 认证装饰器


class token_decorator(object):
    def __init__(self, scope='profile'):
        self.scope = scope

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            if configure.PermissionActive:
                token = request.headers.get('Authorization')
                if token:
                    validate_token()
                    if current_token and current_token.user:
                        return func(*args, **kwargs)
                    else:
                        abort(403)

                else:
                    abort(401)  # 无token,401
            else:
                return func(*args, **kwargs)

        @require_oauth(self.scope)
        def validate_token():
            pass

        return wrapped_function