Skip to content

Commit 5adb219

Browse files
committed
Addressing comments
1 parent 48e96c5 commit 5adb219

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

flask_jwt_extended/view_decorators.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ def _decode_jwt_from_json(request_type):
233233

234234
try:
235235
encoded_token = request.json.get(token_key, None)
236-
assert encoded_token
237-
except (BadRequest, AssertionError):
236+
if not encoded_token:
237+
raise BadRequest()
238+
except BadRequest:
238239
raise NoAuthorizationError('Missing "{}" key in json data.'.format(token_key))
239240

240241
return decode_token(encoded_token)

tests/test_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_default_configs(app):
7373

7474

7575
def test_override_configs(app):
76-
app.config['JWT_TOKEN_LOCATION'] = ['cookies', 'query_string']
76+
app.config['JWT_TOKEN_LOCATION'] = ['cookies', 'query_string', 'json']
7777
app.config['JWT_HEADER_NAME'] = 'TestHeader'
7878
app.config['JWT_HEADER_TYPE'] = 'TestType'
7979
app.config['JWT_JSON_KEY'] = 'TestKey'
@@ -120,11 +120,11 @@ class CustomJSONEncoder(JSONEncoder):
120120
app.json_encoder = CustomJSONEncoder
121121

122122
with app.test_request_context():
123-
assert config.token_location == ['cookies', 'query_string']
123+
assert config.token_location == ['cookies', 'query_string', 'json']
124124
assert config.jwt_in_query_string is True
125125
assert config.jwt_in_cookies is True
126126
assert config.jwt_in_headers is False
127-
assert config.jwt_in_json is False
127+
assert config.jwt_in_json is True
128128
assert config.header_name == 'TestHeader'
129129
assert config.header_type == 'TestType'
130130
assert config.json_key == 'TestKey'

tests/test_json.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def test_custom_body_key(app):
6666
assert response.status_code == 401
6767
assert response.get_json() == {'msg': 'Missing "Bar" key in json data.'}
6868

69-
# Ensure new headers do work
69+
# Ensure new keys do work
7070
data = {'Foo': access_token}
7171
response = test_client.post('/protected', json=data)
7272
assert response.status_code == 200
@@ -83,16 +83,33 @@ def test_missing_keys(app):
8383
jwtM = get_jwt_manager(app)
8484
headers = {'content-type': 'application/json'}
8585

86-
# Ensure 'default' no headers response
86+
# Ensure 'default' no json response
8787
response = test_client.post('/protected', headers=headers)
8888
assert response.status_code == 401
8989
assert response.get_json() == {'msg': 'Missing "access_token" key in json data.'}
9090

91-
# Test custom no headers response
91+
# Test custom no json response
9292
@jwtM.unauthorized_loader
9393
def custom_response(err_str):
9494
return jsonify(foo='bar'), 201
9595

9696
response = test_client.post('/protected', headers=headers)
9797
assert response.status_code == 201
9898
assert response.get_json() == {'foo': "bar"}
99+
100+
def test_defaults(app):
101+
test_client = app.test_client()
102+
103+
with app.test_request_context():
104+
access_token = create_access_token('username')
105+
refresh_token = create_refresh_token('username')
106+
107+
data = {'access_token': access_token}
108+
response = test_client.post('/protected', json=data)
109+
assert response.status_code == 200
110+
assert response.get_json() == {'foo': 'bar'}
111+
112+
data = {'refresh_token': refresh_token}
113+
response = test_client.post('/refresh', json=data)
114+
assert response.status_code == 200
115+
assert response.get_json() == {'foo': 'bar'}

tests/test_multiple_token_locations.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
def app():
1111
app = Flask(__name__)
1212
app.config['JWT_SECRET_KEY'] = 'foobarbaz'
13-
app.config['JWT_TOKEN_LOCATION'] = ['headers', 'cookies', 'query_string']
13+
app.config['JWT_TOKEN_LOCATION'] = ['headers', 'cookies', 'query_string', 'json']
1414
JWTManager(app)
1515

1616
@app.route('/cookie_login', methods=['GET'])
@@ -20,7 +20,7 @@ def cookie_login():
2020
set_access_cookies(resp, access_token)
2121
return resp
2222

23-
@app.route('/protected', methods=['GET'])
23+
@app.route('/protected', methods=['GET', 'POST'])
2424
@jwt_required
2525
def access_protected():
2626
return jsonify(foo='bar')
@@ -58,6 +58,18 @@ def test_query_string_access(app):
5858
assert response.get_json() == {'foo': 'bar'}
5959

6060

61+
def test_json_access(app):
62+
test_client = app.test_client()
63+
64+
with app.test_request_context():
65+
access_token = create_access_token('username')
66+
67+
data = {'access_token': access_token}
68+
response = test_client.post('/protected', json=data)
69+
assert response.status_code == 200
70+
assert response.get_json() == {'foo': 'bar'}
71+
72+
6173
@pytest.mark.parametrize("options", [
6274
(['cookies', 'headers'], ('Missing JWT in cookies or headers (Missing cookie '
6375
'"access_token_cookie"; Missing Authorization Header)')),

0 commit comments

Comments
 (0)