简体   繁体   English

在flask-restful和create_app中使用flask-jwt-extended回调

[英]Using flask-jwt-extended callbacks with flask-restful and create_app

I'm trying to create API tokens for my flask API with flask-jwt-extended. 我正在尝试使用flask-jwt-extended为我的flask API创建API令牌。 I'm trying to initialize the token_in_blacklist_loader but can't figure out the right way to do that. 我正在尝试初始化token_in_blacklist_loader,但找不到正确的方法。

The problem is that token_in_blacklist_loader is implemented as a decorator. 问题是token_in_blacklist_loader被实现为装饰器。 It is supposed to be used in the following way: 应该以以下方式使用它:

@jwt.token_in_blacklist_loader
def check_if_token_in_blacklist(decrypted_token):
    jti = decrypted_token['jti']
    return jti in blacklist

^ from the docs here 来自此处文档的 ^

Where jwt is defined as: 其中jwt定义为:

jwt = JWTManager(app)

But if using the create_app pattern, then jwt variable is hidden inside a function, and cannot be used in the global scope for decorators. 但是,如果使用create_app模式,则jwt变量将隐藏在函数内部,并且不能在装饰器的全局范围内使用。

What is the right way to fix this / work around this? 解决此问题/解决此问题的正确方法是什么?

What I ended up doing was putting the handler inside of create_app like so: 我最终要做的是将处理程序放入create_app如下所示:

def create_app(name: str, settings_override: dict = {}):
    app = Flask(name, ...)
    ...
    jwt = JWTManager(app)
    @jwt.token_in_blacklist_loader
    def check_token_in_blacklist(token_dict: dict) -> bool:
        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM