Which decorator to add an authorization step before the request is processed ?
See original GitHub issueHello, this is more a question than an issue. I’ve added @jwt_required() on several routes
@routes.route('/devices', methods=['GET'])
@jwt_required()
def get_devices(args):
...
but I need to have a method that extracts, for each request, the user data from the JWT so it can verify if the user is authorized for the request. I though about adding a @jwt.user_lookup_loader in a @before_request method, something like the following:
@routes.before_request
@jwt.user_lookup_loader
def user_lookup_callback(_jwt_header, jwt_data):
email = jwt_data["sub"]
user = Database.find_one("accounts", {"username": email})
return user
but this is not working as expected. I think I’m missing something here. Can a decorator be added to trigger some kind of authorization function before each @jwt_required() decorated routes ?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Using authentication decorators in Flask - CircleCI
Now we can explore how to use decorators for authentication. Endpoints must be authenticated before they are allowed to make requests in an ......
Read more >Using Python decorators to process and authorize requests
Here Python decorators came in very handy. They allow adding behavior and pre-processing to any function without modifying it:.
Read more >Using Python decorators to process and authorize requests
Here I chained three decorators: two that we saw before and a new one: @load_user_or_fail loads the user model and passes it as...
Read more >Middleware - FastAPI
You can add middleware to FastAPI applications. A "middleware" is a function that works with every request before it is processed by any...
Read more >A practical guide to TypeScript decorators - LogRocket Blog
This type of decorator can be very useful for authentication and authorization purposes, such as checking whether a user is allowed to access ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You don’t need to add a
@app.before_request, just having the@jwt.user_lookup_loaderdefined is all flask-jwt-extended needs to cause it to be called any time a valid JWT is present in the request. You can then access the user via thecurrent_userproxy. Full docs around that can be found here: https://flask-jwt-extended.readthedocs.io/en/stable/automatic_user_loading.If you are trying to do something like permission checks, you can do that in conjunction with the
@jwt.user_lookup_loader/current_uservia jwt_manager.verify_jwt_in_request in anapp.before_requestor in a custom decoratorI’m playing with custom decorators, exactly what I need. Thanks a lot