Skip to content

Commit a0fa761

Browse files
author
Landon Gilbert-Bland
committed
Update documentation for custom decorators
1 parent 10c758d commit a0fa761

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

docs/custom_decorators.rst

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
Custom Decorators
22
=================
33

4-
NOTE: THIS DOCUMENTATION HAS NOT YET BEEN UPDATED
5-
6-
74
You can create your own decorators that extend the functionality of the
85
decorators provided by this extension. For example, you may want to create
96
your own decorator that verifies a JWT is present as well as verifying that
10-
this token has sufficient permissions/roles to access an endpoint.
7+
the current user is an administrator.
118

129
:func:`flask_jwt_extended.verify_jwt_in_request` can be used to build your own decorators.
13-
This is the same function used the :func:`flask_jwt_extended.jwt_required`.
10+
This is the same function used by :func:`flask_jwt_extended.jwt_required`.
1411

1512
Here is an example of how this might look.
1613

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Flask-JWT-Extended's Documentation
4242
token_locations
4343
refreshing_tokens
4444
blocklist_and_token_revoking
45-
custom_decorators
46-
changing_default_behavior
4745
options
46+
changing_default_behavior
47+
custom_decorators
4848
api

examples/custom_decorators.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from flask import Flask
44
from flask import jsonify
5-
from flask import request
65

76
from flask_jwt_extended import create_access_token
87
from flask_jwt_extended import get_jwt
@@ -15,41 +14,37 @@
1514
jwt = JWTManager(app)
1615

1716

18-
# Here is a custom decorator that verifies the JWT is present in
19-
# the request, as well as insuring that this user has a role of
20-
# `admin` in the access token
21-
def admin_required(fn):
22-
@wraps(fn)
23-
def wrapper(*args, **kwargs):
24-
verify_jwt_in_request()
25-
claims = get_jwt()
26-
if claims["roles"] != "admin":
27-
return jsonify(msg="Admins only!"), 403
28-
else:
29-
return fn(*args, **kwargs)
30-
31-
return wrapper
17+
# Here is a custom decorator that verifies the JWT is present in the request,
18+
# as well as insuring that the JWT has a claim indicating that this user is
19+
# an administrator
20+
def admin_required():
21+
def wrapper(fn):
22+
@wraps(fn)
23+
def decorator(*args, **kwargs):
24+
verify_jwt_in_request()
25+
claims = get_jwt()
26+
if claims["is_administrator"]:
27+
return fn(*args, **kwargs)
28+
else:
29+
return jsonify(msg="Admins only!"), 403
3230

31+
return decorator
3332

34-
@jwt.additional_claims_loader
35-
def add_claims_to_access_token(identity):
36-
if identity == "admin":
37-
return {"roles": "admin"}
38-
else:
39-
return {"roles": "peasant"}
33+
return wrapper
4034

4135

4236
@app.route("/login", methods=["POST"])
4337
def login():
44-
username = request.json.get("username", None)
45-
access_token = create_access_token(username)
38+
access_token = create_access_token(
39+
"admin_user", additional_claims={"is_administrator": True}
40+
)
4641
return jsonify(access_token=access_token)
4742

4843

4944
@app.route("/protected", methods=["GET"])
50-
@admin_required
45+
@admin_required()
5146
def protected():
52-
return jsonify(secret_message="go banana!")
47+
return jsonify(foo="bar")
5348

5449

5550
if __name__ == "__main__":

0 commit comments

Comments
 (0)