This repository was archived by the owner on Feb 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathtest_jwt.py
More file actions
311 lines (219 loc) · 9.6 KB
/
test_jwt.py
File metadata and controls
311 lines (219 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -*- coding: utf-8 -*-
"""
tests.test_jwt
~~~~~~~~~~~~~~
Flask-JWT tests
"""
import time
from datetime import datetime, timedelta
import jwt as _jwt
import pytest
from flask import Flask, json, jsonify
import flask_jwt
def post_json(client, url, data):
data = json.dumps(data)
resp = client.post(url, headers={'Content-Type': 'application/json'}, data=data)
return resp, json.loads(resp.data)
def assert_error_response(r, code, msg, desc):
assert r.status_code == code
jdata = json.loads(r.data)
assert jdata['status_code'] == code
assert jdata['error'] == msg
assert jdata['description'] == desc
def test_initialize():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'super-secret'
jwt = flask_jwt.JWT(app, lambda: None, lambda: None)
assert isinstance(jwt, flask_jwt.JWT)
assert len(app.url_map._rules) == 2
def test_adds_auth_endpoint():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'super-secret'
app.config['JWT_AUTH_URL_RULE'] = '/auth'
app.config['JWT_AUTH_ENDPOINT'] = 'jwt_auth'
flask_jwt.JWT(app, lambda: None, lambda: None)
rules = [str(r) for r in app.url_map._rules]
assert '/auth' in rules
def test_auth_endpoint_with_valid_request(client, user):
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
assert resp.status_code == 200
assert 'access_token' in jdata
def test_custom_auth_endpoint_with_valid_request(app, client, user):
app.config['JWT_AUTH_USERNAME_KEY'] = 'email'
app.config['JWT_AUTH_PASSWORD_KEY'] = 'pass'
resp, jdata = post_json(
client,
'/auth',
{'email': user.username, 'pass': user.password}
)
assert resp.status_code == 200
assert 'access_token' in jdata
def test_auth_endpoint_with_invalid_request(client, user):
# Invalid request (no password)
resp, jdata = post_json(client, '/auth', {'username': user.username})
assert resp.status_code == 401
assert 'error' in jdata
assert jdata['error'] == 'Bad Request'
assert 'description' in jdata
assert jdata['description'] == 'Invalid credentials'
assert 'status_code' in jdata
assert jdata['status_code'] == 401
def test_auth_endpoint_with_invalid_credentials(client):
resp, jdata = post_json(
client, '/auth', {'username': 'bogus', 'password': 'bogus'})
assert resp.status_code == 401
assert 'error' in jdata
assert jdata['error'] == 'Bad Request'
assert 'description' in jdata
assert jdata['description'] == 'Invalid credentials'
assert 'status_code' in jdata
assert jdata['status_code'] == 401
def test_jwt_required_decorator_with_valid_token(app, client, user):
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
token = jdata['access_token']
resp = client.get('/protected', headers={'Authorization': 'JWT ' + token})
assert resp.status_code == 200
assert resp.data == b'success'
def test_jwt_required_decorator_with_valid_request_current_identity(app, client, user):
with client as c:
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
token = jdata['access_token']
c.get(
'/protected',
headers={'authorization': 'JWT ' + token})
assert flask_jwt.current_identity
def test_jwt_required_decorator_with_invalid_request_current_identity(app, client):
with client as c:
c.get('/protected', headers={'authorization': 'JWT bogus'})
assert flask_jwt.current_identity._get_current_object() is None
def test_jwt_required_decorator_with_invalid_authorization_headers(app, client):
# Missing authorization header
r = client.get('/protected')
assert_error_response(
r, 401, 'Authorization Required', 'Request does not contain an access token')
assert r.headers['WWW-Authenticate'] == 'JWT realm="Login Required"'
# Not a JWT auth header prefix
r = client.get('/protected', headers={'authorization': 'Bogus xxx'})
assert_error_response(
r, 401, 'Invalid JWT header', 'Unsupported authorization type')
# Missing token
r = client.get('/protected', headers={'authorization': 'JWT'})
assert_error_response(
r, 401, 'Invalid JWT header', 'Token missing')
# Token with spaces
r = client.get('/protected', headers={'authorization': 'JWT xxx xxx'})
assert_error_response(
r, 401, 'Invalid JWT header', 'Token contains spaces')
def test_jwt_required_decorator_with_invalid_jwt_tokens(client, user, app):
app.config['JWT_LEEWAY'] = timedelta(seconds=0)
app.config['JWT_EXPIRATION_DELTA'] = timedelta(milliseconds=200)
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
token = jdata['access_token']
# Undecipherable
r = client.get('/protected', headers={'authorization': 'JWT %sX' % token})
assert_error_response(r, 401, 'Invalid token', 'Signature verification failed')
# Expired
time.sleep(1.5)
r = client.get('/protected', headers={'authorization': 'JWT ' + token})
assert_error_response(r, 401, 'Invalid token', 'Signature has expired')
def test_jwt_required_decorator_with_missing_user(client, jwt, user):
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
token = jdata['access_token']
@jwt.identity_handler
def load_user(payload):
return None
r = client.get('/protected', headers={'authorization': 'JWT %s' % token})
assert_error_response(r, 401, 'Invalid JWT', 'User does not exist')
def test_custom_error_handler(client, jwt):
@jwt.jwt_error_handler
def error_handler(e):
return "custom"
r = client.get('/protected')
assert r.data == b'custom'
def test_custom_response_handler(client, jwt, user):
@jwt.auth_response_handler
def resp_handler(access_token, identity):
return jsonify({'mytoken': access_token.decode('utf-8')})
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
assert 'mytoken' in jdata
def test_custom_encode_handler(client, jwt, user, app):
secret = app.config['JWT_SECRET_KEY']
alg = 'HS256'
@jwt.jwt_encode_handler
def encode_data(identity):
return _jwt.encode({'hello': 'world'}, secret, algorithm=alg)
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
decoded = _jwt.decode(jdata['access_token'], secret, algorithms=[alg])
assert decoded == {'hello': 'world'}
def test_custom_decode_handler(client, user, jwt):
# The following function should receive the decode return value
@jwt.identity_handler
def load_user(payload):
assert payload == {'user_id': user.id}
@jwt.jwt_decode_handler
def decode_data(token):
return {'user_id': user.id}
with client as c:
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
token = jdata['access_token']
c.get('/protected', headers={'authorization': 'JWT ' + token})
def test_custom_payload_handler(client, jwt, user):
@jwt.identity_handler
def load_user(payload):
if payload['id'] == user.id:
return user
@jwt.jwt_payload_handler
def make_payload(u):
iat = datetime.utcnow()
exp = iat + timedelta(seconds=60)
nbf = iat + timedelta(seconds=0)
return {'iat': iat, 'exp': exp, 'nbf': nbf, 'id': u.id}
with client as c:
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
token = jdata['access_token']
c.get('/protected', headers={'authorization': 'JWT ' + token})
assert flask_jwt.current_identity == user
def test_custom_auth_header(app, client, user):
app.config['JWT_AUTH_HEADER_PREFIX'] = 'Bearer'
with client as c:
resp, jdata = post_json(
client, '/auth', {'username': user.username, 'password': user.password})
token = jdata['access_token']
# Custom Bearer auth header prefix
resp = c.get('/protected', headers={'authorization': 'Bearer ' + token})
assert resp.status_code == 200
assert resp.data == b'success'
# Not custom Bearer auth header prefix
resp = c.get('/protected', headers={'authorization': 'JWT ' + token})
assert_error_response(resp, 401, 'Invalid JWT header', 'Unsupported authorization type')
def test_custom_auth_handler():
def custom_auth_request_handler():
return jsonify({'hello': 'world'})
jwt = flask_jwt.JWT()
pytest.deprecated_call(jwt.auth_request_handler, custom_auth_request_handler)
app = Flask(__name__)
jwt.init_app(app)
with app.test_client() as c:
resp, jdata = post_json(c, '/auth', {})
assert jdata == {'hello': 'world'}
def test_default_encode_handler_user_object(app, client, jwt, user):
with app.app_context():
token = jwt.jwt_encode_callback(user)
with client as c:
c.get('/protected', headers={'authorization': 'JWT ' + token.decode('utf-8')})
assert flask_jwt.current_identity == user
def test_default_encode_handler_dictuser(dictuserapp, jwt, dictuser):
with dictuserapp.app_context():
token = jwt.jwt_encode_callback(dictuser)
with dictuserapp.test_client() as c:
c.get('/protected', headers={'authorization': 'JWT ' + token.decode('utf-8')})
assert flask_jwt.current_identity == dictuser