Skip to content

Commit 12e9fac

Browse files
author
Landon Gilbert-Bland
committed
Start updating documentation for 4.0.0 version
1 parent 4493f11 commit 12e9fac

7 files changed

+59
-59
lines changed

4.0.0_changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Breaking Changes
109109
return jsonify(msg="{} has an expired jwt".format(identity)), 401
110110
```
111111

112-
* `claims_verification_loaders` has been renamed to `token_verification_loader`.
112+
* `claims_verification_loader` has been renamed to `token_verification_loader`.
113113
It now takes two arguments instead of one, which are the `jwt_header` and
114114
`jwt_data`
115115

docs/add_custom_data_claims.rst

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,36 @@
11
Storing Data in Access Tokens
22
=============================
3-
43
You may want to store additional information in the access token which you could
5-
later access in the protected views. This can be done with the
6-
:meth:`~flask_jwt_extended.JWTManager.user_claims_loader` decorator, and the data can be
7-
accessed later in a protected endpoint with the
8-
:func:`~flask_jwt_extended.get_jwt` function.
4+
later access in the protected views. This can be done using the `additional_claims`
5+
argument with the :func:`~flask_jwt_extended.create_access_token` or
6+
:func:`~flask_jwt_extended.create_refresh_token` functions. The claims
7+
can be accessed in a protected route via the :func:`~flask_jwt_extended.get_jwt`
8+
function.
99

10-
Storing data in an access token can be good for performance. If you store data
11-
in the token, you wont need to look it up from disk next time you need it in
12-
a protected endpoint. However, you should take care what data you put in the
13-
token. Any data in the access token can be trivially viewed by anyone who can
14-
read the token. **Do not** store sensitive information in access tokens!
10+
It is important to remember that JWTs are not encrypted and the contents of
11+
a JWT can be trivially decoded by anyone who has access to it. As such, you
12+
should never put any sensitive information in a JWT.
1513

1614
.. literalinclude:: ../examples/additional_data_in_access_token.py
15+
16+
17+
Alternately you can use the :meth:`~flask_jwt_extended.JWTManager.user_claims_loader`
18+
decorator to register a callback function that will be called whenever a new JWT
19+
is created, and return a dictionary of claims to add to that token. In the case
20+
that both :meth:`~flask_jwt_extended.JWTManager.user_claims_loader` and the
21+
`additional_claims` argument are used, both results are merged together, with ties
22+
going to the data suplied by the `additional_claims` argument.
23+
24+
.. code-block:: python
25+
26+
# Using the user_claims_loader, we can specify a method that will be
27+
# called when creating JWTs. The decorated method must take the identity
28+
# we are creating a token for and return a dictionary of additional
29+
# claims to add to the JWT.
30+
@jwt.user_claims_loader
31+
def add_claims_to_access_token(identity):
32+
return = {
33+
"aud": "some_audience",
34+
"foo": "bar",
35+
"upcase_name": identity.upper(),
36+
}

docs/api.rst

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,38 @@ Configuring Flask-JWT-Extended
1212

1313
.. automethod:: __init__
1414
.. automethod:: init_app
15-
.. automethod:: claims_verification_loader
16-
.. automethod:: claims_verification_failed_loader
1715
.. automethod:: decode_key_loader
1816
.. automethod:: encode_key_loader
1917
.. automethod:: expired_token_loader
2018
.. automethod:: invalid_token_loader
2119
.. automethod:: needs_fresh_token_loader
2220
.. automethod:: revoked_token_loader
2321
.. automethod:: token_in_blocklist_loader
22+
.. automethod:: token_verification_failed_loader
23+
.. automethod:: token_verification_loader
2424
.. automethod:: unauthorized_loader
2525
.. automethod:: user_claims_loader
2626
.. automethod:: user_identity_loader
27-
.. automethod:: user_lookup_loader
2827
.. automethod:: user_lookup_error_loader
28+
.. automethod:: user_lookup_loader
2929

3030

3131
Protected endpoint decorators
3232
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3333
.. autofunction:: jwt_required
34-
.. autofunction:: jwt_refresh_token_required
35-
.. autofunction:: fresh_jwt_required
36-
.. autofunction:: jwt_optional
3734

3835

3936
.. _Verify Tokens in Request:
4037

4138
Verify Tokens in Request
4239
~~~~~~~~~~~~~~~~~~~~~~~~
43-
These perform the same actions as the protected endpoint decorators, without
44-
actually decorating a function. These are very useful if you want to create
40+
This performs the same actions as the protected endpoint decorators, without
41+
actually decorating a function. This is very useful if you want to create
4542
your own decorators on top of flask jwt extended (such as role_required), or
4643
if you want to hook some of this extensions functionality into a flask
4744
before_request handler.
4845

4946
.. autofunction:: verify_jwt_in_request
50-
.. autofunction:: verify_jwt_in_request_optional
51-
.. autofunction:: verify_fresh_jwt_in_request
52-
.. autofunction:: verify_jwt_refresh_token_in_request
5347

5448

5549
Utilities

docs/basic_usage.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,19 @@ We can see this in action using CURL:
3030
$ curl -H "Content-Type: application/json" -X POST \
3131
-d '{"username":"test","password":"test"}' http://localhost:5000/login
3232
{
33-
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6dHJ1ZSwianRpIjoiZjhmNDlmMjUtNTQ4OS00NmRjLTkyOWUtZTU2Y2QxOGZhNzRlIiwidXNlcl9jbGFpbXMiOnt9LCJuYmYiOjE0NzQ0NzQ3OTEsImlhdCI6MTQ3NDQ3NDc5MSwiaWRlbnRpdHkiOiJ0ZXN0IiwiZXhwIjoxNDc0NDc1NjkxLCJ0eXBlIjoiYWNjZXNzIn0.vCy0Sec61i9prcGIRRCbG8e9NV6_wFH2ICFgUGCLKpc"
33+
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTYxMTQ2MzE4MCwianRpIjoiZTBjMzhhNDUtNGM5My00NTJmLWIzZWQtOTcyZGJiNzA5YWViIiwibmJmIjoxNjExNDYzMTgwLCJ0eXBlIjoiYWNjZXNzIiwic3ViIjoidGVzdCIsImV4cCI6MTYxNDA1NTE4MH0.Qc87HZBv_qBlzcybCMoeh0SM2oyM6Waefw_xEP0VdF8"
3434
}
3535
36-
$ export ACCESS="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6dHJ1ZSwianRpIjoiZjhmNDlmMjUtNTQ4OS00NmRjLTkyOWUtZTU2Y2QxOGZhNzRlIiwidXNlcl9jbGFpbXMiOnt9LCJuYmYiOjE0NzQ0NzQ3OTEsImlhdCI6MTQ3NDQ3NDc5MSwiaWRlbnRpdHkiOiJ0ZXN0IiwiZXhwIjoxNDc0NDc1NjkxLCJ0eXBlIjoiYWNjZXNzIn0.vCy0Sec61i9prcGIRRCbG8e9NV6_wFH2ICFgUGCLKpc"
36+
$ export JWT="eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTYxMTQ2MzE4MCwianRpIjoiZTBjMzhhNDUtNGM5My00NTJmLWIzZWQtOTcyZGJiNzA5YWViIiwibmJmIjoxNjExNDYzMTgwLCJ0eXBlIjoiYWNjZXNzIiwic3ViIjoidGVzdCIsImV4cCI6MTYxNDA1NTE4MH0.Qc87HZBv_qBlzcybCMoeh0SM2oyM6Waefw_xEP0VdF8"
3737
38-
$ curl -H "Authorization: Bearer $ACCESS" http://localhost:5000/protected
38+
$ curl -H "Authorization: Bearer $JWT" http://localhost:5000/protected
3939
{
4040
"logged_in_as": "test"
4141
}
4242
43-
NOTE: Remember to change the secret key of your application, and ensure that no
44-
one is able to view it. The JSON Web Tokens are signed with the secret key, so
45-
if someone gets that, they can create arbitrary tokens, and in essence log in
46-
as any user.
43+
**Important**
44+
45+
Remember to change the jwt secret key in your application, and ensure that it
46+
is secure. The JWTs are signed with this key, and if someone gets their hands
47+
on it they will be able to create arbitraty tokens that are accepted by your
48+
web flask application.

docs/changing_default_behavior.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ and what the return values of your callback functions need to be.
2323

2424
* - Loader Decorator
2525
- Description
26-
* - :meth:`~flask_jwt_extended.JWTManager.claims_verification_loader`
26+
* - :meth:`~flask_jwt_extended.JWTManager.token_verification_loader`
2727
- Function that is called to verify the user_claims data. Must return True or False
28-
* - :meth:`~flask_jwt_extended.JWTManager.claims_verification_failed_loader`
28+
* - :meth:`~flask_jwt_extended.JWTManager.token_verification_failed_loader`
2929
- Function that is called when the user claims verification callback returns False
3030
* - :meth:`~flask_jwt_extended.JWTManager.decode_key_loader`
3131
- Function that is called to get the decode key before verifying a token

examples/additional_data_in_access_token.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,18 @@
1313
jwt = JWTManager(app)
1414

1515

16-
# Using the user_claims_loader, we can specify a method that will be
17-
# called when creating access tokens, and add these claims to the said
18-
# token. This method is passed the identity of who the token is being
19-
# created for, and must return data that is json serializable
20-
@jwt.user_claims_loader
21-
def add_claims_to_access_token(identity):
22-
return {"hello": identity, "foo": ["bar", "baz"]}
23-
24-
2516
@app.route("/login", methods=["POST"])
2617
def login():
2718
username = request.json.get("username", None)
2819
password = request.json.get("password", None)
2920
if username != "test" or password != "test":
3021
return jsonify({"msg": "Bad username or password"}), 401
3122

32-
ret = {"access_token": create_access_token(username)}
33-
return jsonify(ret), 200
23+
# You can use the additional_claims argument to either add
24+
# custom claims or override default claims in the JWT.
25+
additional_claims = {"aud": "some_audience", "foo": "bar"}
26+
access_token = create_access_token(username, additional_claims=additional_claims)
27+
return jsonify(access_token=access_token)
3428

3529

3630
# In a protected view, get the claims you added to the jwt with the
@@ -39,7 +33,7 @@ def login():
3933
@jwt_required
4034
def protected():
4135
claims = get_jwt()
42-
return jsonify({"hello_is": claims["hello"], "foo_is": claims["foo"]}), 200
36+
return jsonify(foo=claims["foo"])
4337

4438

4539
if __name__ == "__main__":

examples/simple.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,23 @@
1414
jwt = JWTManager(app)
1515

1616

17-
# Provide a method to create access tokens. The create_access_token()
18-
# function is used to actually generate the token, and you can return
19-
# it to the caller however you choose.
17+
# Create a route to authenticate your users and return JWTs. The
18+
# create_access_token() function is used to actually generate the JWT.
2019
@app.route("/login", methods=["POST"])
2120
def login():
22-
if not request.is_json:
23-
return jsonify({"msg": "Missing JSON in request"}), 400
24-
2521
username = request.json.get("username", None)
2622
password = request.json.get("password", None)
27-
if not username:
28-
return jsonify({"msg": "Missing username parameter"}), 400
29-
if not password:
30-
return jsonify({"msg": "Missing password parameter"}), 400
31-
3223
if username != "test" or password != "test":
3324
return jsonify({"msg": "Bad username or password"}), 401
3425

35-
# Identity can be any data that is json serializable
3626
access_token = create_access_token(identity=username)
37-
return jsonify(access_token=access_token), 200
27+
return jsonify(access_token=access_token)
3828

3929

40-
# Protect a view with jwt_required, which requires a valid access token
41-
# in the request to access.
30+
# Protect a route with jwt_required, which will kick out requests
31+
# without a valid JWT present.
4232
@app.route("/protected", methods=["GET"])
43-
@jwt_required
33+
@jwt_required()
4434
def protected():
4535
# Access the identity of the current user with get_jwt_identity
4636
current_user = get_jwt_identity()

0 commit comments

Comments
 (0)