|
12 | 12 | from jwt import InvalidIssuerError
|
13 | 13 | from jwt import InvalidSignatureError
|
14 | 14 | from jwt import MissingRequiredClaimError
|
| 15 | +from jwt.exceptions import InvalidSubjectError |
15 | 16 |
|
16 | 17 | from flask_jwt_extended import create_access_token
|
17 | 18 | from flask_jwt_extended import create_refresh_token
|
@@ -279,6 +280,35 @@ def test_verify_no_aud(app, default_access_token, token_aud):
|
279 | 280 | assert decoded["aud"] == token_aud
|
280 | 281 |
|
281 | 282 |
|
| 283 | +@pytest.mark.parametrize("token_sub", [123, {}, [], False]) |
| 284 | +def test_invalid_sub_values(app, default_access_token, token_sub): |
| 285 | + """Verifies that invalid values for the sub claim fail decoding, the |
| 286 | + default behavior of JWT_VERIFY_SUB = True |
| 287 | + """ |
| 288 | + |
| 289 | + default_access_token["sub"] = token_sub |
| 290 | + invalid_token = encode_token(app, default_access_token) |
| 291 | + with pytest.raises(InvalidSubjectError): |
| 292 | + with app.test_request_context(): |
| 293 | + decode_token(invalid_token) |
| 294 | + |
| 295 | + |
| 296 | +@pytest.mark.parametrize("token_sub", [123, {}, [], False]) |
| 297 | +def test_invalid_sub_values_allowed_with_no_verify( |
| 298 | + app, default_access_token, token_sub |
| 299 | +): |
| 300 | + """Verifies that invalid values for the sub claim succeed at decoding, if |
| 301 | + the user configures JWT_VERIFY_SUB = False |
| 302 | + """ |
| 303 | + |
| 304 | + app.config["JWT_VERIFY_SUB"] = False |
| 305 | + default_access_token["sub"] = token_sub |
| 306 | + valid_token = encode_token(app, default_access_token) |
| 307 | + with app.test_request_context(): |
| 308 | + decoded = decode_token(valid_token) |
| 309 | + assert decoded["sub"] == token_sub |
| 310 | + |
| 311 | + |
282 | 312 | def test_encode_iss(app, default_access_token):
|
283 | 313 | app.config["JWT_ENCODE_ISSUER"] = "foobar"
|
284 | 314 |
|
|
0 commit comments