Skip to content

Need way to determine in which allowed location (cookie, header, etc) the JWT was found for the current request #419

@sammck

Description

@sammck

For testing and other reasons, I would like to have protected routes that can be authorized by a JWT either in a cookie (for browser-based access) or in a header (for single-page apps or standalone clients). This works fine, except that the recommended approach for implicit access token refresh:

# Using an `after_request` callback, we refresh any token that is within 30
# minutes of expiring. Change the timedeltas to match the needs of your application.
@app.after_request
def refresh_expiring_jwts(response):
    try:
        exp_timestamp = get_jwt()["exp"]
        now = datetime.now(timezone.utc)
        target_timestamp = datetime.timestamp(now + timedelta(minutes=30))
        if target_timestamp > exp_timestamp:
            access_token = create_access_token(identity=get_jwt_identity())
            set_access_cookies(response, access_token)
        return response
    except (RuntimeError, KeyError):
        # Case where there is not a valid JWT. Just return the original respone
        return response

should be suppressed in the case where the access token jwt was supplied in a header rather than a cookie.

Is there a viable way to make this work in the current implementation? If not, does this sound like a reasonable feature request? I'd be willing to take a stab at a PR.

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