Skip to content

Add callback hook to decode token from arbitrary source #178

@mjpieters

Description

@mjpieters

Currently, the project will load the JWT data from various sources, depending on what JWT_TOKEN_LOCATION is set to, including cookies, the query string, and in the headers.

I now have a requirement to look for refresh tokens in a JSON POST body. Rather than ask for jet another configurable codepath to be added, can we have a callback instead? The callback would be passed the request type and would have to return the decoded data (using utils.decode_token()) or None if no data for the request type was found.

My callback could then just use:

from flask_jwt_extended import decode_token, JWTManager

jwt = JWTManager()

@jwt.decode_jwt_callback
def decode_jwt_from_json(request_type):
    if request_type != 'refresh':
        return None
    data = request.json or {}
    encoded_token = data.get('refresh_token')   
    if encoded_token is None:
        return None
    return decode_token(encoded_token)

This saves me from having to write a new decorator that then has to remember to verify the token and check the blacklist (if configured).

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