Skip to content

Commit 676565a

Browse files
committed
unify set_cookie methods
1 parent e29650b commit 676565a

File tree

6 files changed

+14
-11
lines changed

6 files changed

+14
-11
lines changed

examples/csrf_protection_with_cookies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from flask_jwt_extended import JWTManager, jwt_required, \
44
create_access_token, jwt_refresh_token_required, \
55
create_refresh_token, get_jwt_identity, set_access_cookies, \
6-
set_refresh_cookie
6+
set_refresh_cookies
77

88

99
app = Flask(__name__)
@@ -55,7 +55,7 @@ def login():
5555
# in this response
5656
resp = jsonify({'login': True})
5757
set_access_cookies(resp, access_token)
58-
set_refresh_cookie(resp, refresh_token)
58+
set_refresh_cookies(resp, refresh_token)
5959
return resp, 200
6060

6161

examples/jwt_in_cookie.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from flask_jwt_extended import JWTManager, jwt_required, \
44
create_access_token, jwt_refresh_token_required, \
55
create_refresh_token, get_jwt_identity, set_access_cookies, \
6-
set_refresh_cookie
6+
set_refresh_cookies
77

88
# NOTE: This is just a basic example of how to enable cookies. This is
99
# vulnerable to CSRF attacks, and should not be used as is. See
@@ -46,7 +46,7 @@ def login():
4646
# Set the JWT cookies in the response
4747
resp = jsonify({'login': True})
4848
set_access_cookies(resp, access_token)
49-
set_refresh_cookie(resp, refresh_token)
49+
set_refresh_cookies(resp, refresh_token)
5050
return resp, 200
5151

5252

examples/simple.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from flask import Flask, jsonify, request
22
from flask_jwt_extended import JWTManager, jwt_required,\
3-
create_access_token
3+
create_access_token, get_jwt_identity
44

55
app = Flask(__name__)
66
app.secret_key = 'super-secret' # Change this!
@@ -18,7 +18,8 @@ def login():
1818
if username != 'test' and password != 'test':
1919
return jsonify({"msg": "Bad username or password"}), 401
2020

21-
ret = {'access_token': create_access_token(username)}
21+
# Identity can be any data that is json serializable
22+
ret = {'access_token': create_access_token(identity=username)}
2223
return jsonify(ret), 200
2324

2425

@@ -27,7 +28,9 @@ def login():
2728
@app.route('/protected', methods=['GET'])
2829
@jwt_required
2930
def protected():
30-
return jsonify({'hello': 'world'}), 200
31+
# Access the identity of the current user with get_jwt_identity
32+
current_user = get_jwt_identity()
33+
return jsonify({'hello_from': current_user}), 200
3134

3235
if __name__ == '__main__':
3336
app.run()

flask_jwt_extended/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .jwt_manager import JWTManager
22
from .utils import (jwt_required, fresh_jwt_required, jwt_refresh_token_required,
33
create_refresh_token, create_access_token, get_jwt_identity,
4-
get_jwt_claims, set_access_cookies, set_refresh_cookie)
4+
get_jwt_claims, set_access_cookies, set_refresh_cookies)
55
from .blacklist import (revoke_token, unrevoke_token, get_stored_tokens,
66
get_all_stored_tokens, get_stored_token)

flask_jwt_extended/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ def set_access_cookies(response, encoded_access_token):
389389
path='/')
390390

391391

392-
def set_refresh_cookie(response, encoded_refresh_token):
392+
def set_refresh_cookies(response, encoded_refresh_token):
393393
"""
394394
Takes a flask response object, and configures it to set the encoded refresh
395395
token in a cookie (as well as a csrf refresh cookie if enabled)

tests/test_protected_endpoints.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from flask import Flask, jsonify
77
from flask_jwt_extended.utils import _encode_access_token, get_jwt_claims, \
8-
get_jwt_identity, set_refresh_cookie, set_access_cookies
8+
get_jwt_identity, set_refresh_cookies, set_access_cookies
99
from flask_jwt_extended import JWTManager, create_refresh_token, \
1010
jwt_refresh_token_required, create_access_token, fresh_jwt_required, \
1111
jwt_required
@@ -326,7 +326,7 @@ def login():
326326
# Set the JWTs and the CSRF double submit protection cookies in this response
327327
resp = jsonify({'login': True})
328328
set_access_cookies(resp, access_token)
329-
set_refresh_cookie(resp, refresh_token)
329+
set_refresh_cookies(resp, refresh_token)
330330
return resp, 200
331331

332332
@self.app.route('/auth/refresh', methods=['POST'])

0 commit comments

Comments
 (0)