token_decorator.py 890 Bytes
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

# 认证装饰器


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

    def __call__(self, func):
        @wraps(func)
        def wrapped_function(*args, **kwargs):
            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

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

        return wrapped_function