Skip to content

Which decorator to add an authorization step before the request is processed ? #413

@lucj

Description

@lucj

Hello, 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 ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions