Skip to content

Commit 1267749

Browse files
committed
Example of returning double submit values in json instead of cookies
1 parent 5fa925e commit 1267749

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

docs/tokens_in_cookies.rst

+34
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,37 @@ protection is a choice you will have to make.
4444
Here is an example of what this would look like:
4545

4646
.. literalinclude:: ../examples/csrf_protection_with_cookies.py
47+
48+
By default, the CSRF double submit values are sent back as additional cookies
49+
to the caller. If you prefer, you can disable that, and send them back directly
50+
to the caller, like such:
51+
52+
.. code-block:: python
53+
54+
app.config('JWT_CSRF_IN_COOKIES') = False
55+
#...
56+
#...
57+
#...
58+
@app.route('/token/auth', methods=['POST'])
59+
def login():
60+
username = request.json.get('username', None)
61+
password = request.json.get('password', None)
62+
if username != 'test' or password != 'test':
63+
return jsonify({'login': False}), 401
64+
65+
# Create the tokens we will be sending back to the user
66+
access_token = create_access_token(identity=username)
67+
refresh_token = create_refresh_token(identity=username)
68+
69+
# Return the double submit values in the resulting JSON
70+
# instead of in additional cookies
71+
resp = jsonify({
72+
'access_csrf': get_csrf_token(access_token),
73+
'refresh_csrf': get_csrf_token(refresh_token)
74+
})
75+
76+
# We still need to call these functions to set the
77+
# JWTs in the cookies
78+
set_access_cookies(resp, access_token)
79+
set_refresh_cookies(resp, refresh_token)
80+
return resp, 200

0 commit comments

Comments
 (0)