Custom DecoratorsΒΆ

You can create your own decorators that extend the functionality of the decorators provided by this extension. For example, you may want to create your own decorator that verifies a JWT is present as well as verifying that the current user is an administrator.

flask_jwt_extended.verify_jwt_in_request() can be used to build your own decorators. This is the same function used by flask_jwt_extended.jwt_required().

Here is an example of how this might look.

from functools import wraps

from flask import Flask
from flask import jsonify

from flask_jwt_extended import create_access_token
from flask_jwt_extended import get_jwt
from flask_jwt_extended import JWTManager
from flask_jwt_extended import verify_jwt_in_request

app = Flask(__name__)

app.config["JWT_SECRET_KEY"] = "super-secret"  # Change this!
jwt = JWTManager(app)

# Here is a custom decorator that verifies the JWT is present in the request,
# as well as insuring that the JWT has a claim indicating that this user is
# an administrator
def admin_required():
    def wrapper(fn):
        def decorator(*args, **kwargs):
            claims = get_jwt()
            if claims["is_administrator"]:
                return fn(*args, **kwargs)
                return jsonify(msg="Admins only!"), 403

        return decorator

    return wrapper

@app.route("/login", methods=["POST"])
def login():
    access_token = create_access_token(
        "admin_user", additional_claims={"is_administrator": True}
    return jsonify(access_token=access_token)

@app.route("/protected", methods=["GET"])
def protected():
    return jsonify(foo="bar")

if __name__ == "__main__":