Skip to content

Commit 3329871

Browse files
author
Landon Gilbert-Bland
committed
Remove JWT_BLOCKLIST_TOKEN_CHECKS configuration option
This was a pretty bad idea from the get go. However if someone still wants this functionality, they can check the `jwt_payload["type"]` in their blocklist callback to ignore access or refresh tokens.
1 parent a0fa761 commit 3329871

File tree

6 files changed

+4
-112
lines changed

6 files changed

+4
-112
lines changed

4.0.0_changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Breaking Changes
88
to enable the blocklist, do not use the `@jwt.token_in_blocklist_loader` to
99
register a callback function.
1010

11+
* `app.config['JWT_BLACKLIST_TOKEN_CHEKCS']` option has been removed. If you don't want
12+
to check a given token type against the blocklist, do it by comparing the `jwt_payload["type"]`
13+
in your callback function.
14+
1115
* Rename all occurrences of blacklist to blocklist
1216
- `JWT_BLACKLIST_TOKEN_CHECKS` option is now `JWT_BLOCKLIST_TOKEN_CHECKS`
1317
- `@jwt.token_in_blacklist_loader` callback is now `@jwt.token_in_blocklist_loader`

flask_jwt_extended/config.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -213,27 +213,6 @@ def decode_algorithms(self):
213213
algorithms.append(self.algorithm)
214214
return algorithms
215215

216-
@property
217-
def blocklist_checks(self):
218-
check_type = current_app.config["JWT_BLOCKLIST_TOKEN_CHECKS"]
219-
if isinstance(check_type, str):
220-
check_type = (check_type,)
221-
elif not isinstance(check_type, (Sequence, Set)):
222-
raise RuntimeError("JWT_BLOCKLIST_TOKEN_CHECKS must be a sequence or a set")
223-
for item in check_type:
224-
if item not in ("access", "refresh"):
225-
err = 'JWT_BLOCKLIST_TOKEN_CHECKS must be "access" or "refresh"'
226-
raise RuntimeError(err)
227-
return check_type
228-
229-
@property
230-
def blocklist_access_tokens(self):
231-
return "access" in self.blocklist_checks
232-
233-
@property
234-
def blocklist_refresh_tokens(self):
235-
return "refresh" in self.blocklist_checks
236-
237216
@property
238217
def _secret_key(self):
239218
key = current_app.config["JWT_SECRET_KEY"]

flask_jwt_extended/internal_utils.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from flask import current_app
22

3-
from flask_jwt_extended.config import config
43
from flask_jwt_extended.exceptions import RevokedTokenError
54
from flask_jwt_extended.exceptions import UserClaimsVerificationError
65
from flask_jwt_extended.exceptions import WrongTokenError
@@ -32,11 +31,6 @@ def verify_token_type(decoded_token, expected_type):
3231

3332

3433
def verify_token_not_blocklisted(jwt_header, jwt_data, request_type):
35-
if request_type == "access" and not config.blocklist_access_tokens:
36-
return
37-
if request_type == "refresh" and not config.blocklist_refresh_tokens:
38-
return
39-
4034
jwt_manager = get_jwt_manager()
4135
if jwt_manager._token_in_blocklist_callback(jwt_header, jwt_data):
4236
raise RevokedTokenError(jwt_header, jwt_data)

flask_jwt_extended/jwt_manager.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ def _set_default_configuration_options(app):
165165
app.config.setdefault("JWT_ACCESS_CSRF_FIELD_NAME", "csrf_token")
166166
app.config.setdefault("JWT_ACCESS_CSRF_HEADER_NAME", "X-CSRF-TOKEN")
167167
app.config.setdefault("JWT_ALGORITHM", "HS256")
168-
app.config.setdefault("JWT_BLOCKLIST_TOKEN_CHECKS", ("access", "refresh"))
169168
app.config.setdefault("JWT_COOKIE_CSRF_PROTECT", True)
170169
app.config.setdefault("JWT_COOKIE_DOMAIN", None)
171170
app.config.setdefault("JWT_COOKIE_SAMESITE", None)

tests/test_blocklist.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ def refresh_protected():
3232
@pytest.mark.parametrize("blocklist_type", [["access"], ["refresh", "access"]])
3333
def test_non_blocklisted_access_token(app, blocklist_type):
3434
jwt = get_jwt_manager(app)
35-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = blocklist_type
3635

3736
@jwt.token_in_blocklist_loader
3837
def check_blocklisted(jwt_header, jwt_data):
@@ -52,7 +51,6 @@ def check_blocklisted(jwt_header, jwt_data):
5251
@pytest.mark.parametrize("blocklist_type", [["access"], ["refresh", "access"]])
5352
def test_blocklisted_access_token(app, blocklist_type):
5453
jwt = get_jwt_manager(app)
55-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = blocklist_type
5654

5755
@jwt.token_in_blocklist_loader
5856
def check_blocklisted(jwt_header, jwt_data):
@@ -70,7 +68,6 @@ def check_blocklisted(jwt_header, jwt_data):
7068
@pytest.mark.parametrize("blocklist_type", [["refresh"], ["refresh", "access"]])
7169
def test_non_blocklisted_refresh_token(app, blocklist_type):
7270
jwt = get_jwt_manager(app)
73-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = blocklist_type
7471

7572
@jwt.token_in_blocklist_loader
7673
def check_blocklisted(jwt_header, jwt_data):
@@ -90,7 +87,6 @@ def check_blocklisted(jwt_header, jwt_data):
9087
@pytest.mark.parametrize("blocklist_type", [["refresh"], ["refresh", "access"]])
9188
def test_blocklisted_refresh_token(app, blocklist_type):
9289
jwt = get_jwt_manager(app)
93-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = blocklist_type
9490

9591
@jwt.token_in_blocklist_loader
9692
def check_blocklisted(jwt_header, jwt_data):
@@ -107,31 +103,6 @@ def check_blocklisted(jwt_header, jwt_data):
107103
assert response.status_code == 401
108104

109105

110-
def test_revoked_token_of_different_type(app):
111-
jwt = get_jwt_manager(app)
112-
test_client = app.test_client()
113-
114-
@jwt.token_in_blocklist_loader
115-
def check_blocklisted(jwt_header, jwt_data):
116-
return True
117-
118-
with app.test_request_context():
119-
access_token = create_access_token("username")
120-
refresh_token = create_refresh_token("username")
121-
122-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = ["access"]
123-
response = test_client.get(
124-
"/refresh_protected", headers=make_headers(refresh_token)
125-
)
126-
assert response.get_json() == {"foo": "bar"}
127-
assert response.status_code == 200
128-
129-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = ["refresh"]
130-
response = test_client.get("/protected", headers=make_headers(access_token))
131-
assert response.get_json() == {"foo": "bar"}
132-
assert response.status_code == 200
133-
134-
135106
def test_custom_blocklisted_message(app):
136107
jwt = get_jwt_manager(app)
137108

tests/test_config.py

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ def test_default_configs(app):
5959
assert config.algorithm == "HS256"
6060
assert config.decode_algorithms == ["HS256"]
6161
assert config.is_asymmetric is False
62-
assert config.blocklist_checks == ("access", "refresh")
63-
assert config.blocklist_access_tokens is True
64-
assert config.blocklist_refresh_tokens is True
6562

6663
assert config.cookie_max_age is None
6764

@@ -109,8 +106,6 @@ def test_override_configs(app, delta_func):
109106
app.config["JWT_ALGORITHM"] = "HS512"
110107
app.config["JWT_DECODE_ALGORITHMS"] = ["HS512", "HS256"]
111108

112-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = ("refresh",)
113-
114109
app.config["JWT_IDENTITY_CLAIM"] = "foo"
115110

116111
app.config["JWT_ERROR_MESSAGE_KEY"] = "message"
@@ -160,10 +155,6 @@ class CustomJSONEncoder(JSONEncoder):
160155
assert config.algorithm == "HS512"
161156
assert config.decode_algorithms == ["HS512", "HS256"]
162157

163-
assert config.blocklist_checks == ("refresh",)
164-
assert config.blocklist_access_tokens is False
165-
assert config.blocklist_refresh_tokens is True
166-
167158
assert config.cookie_max_age == 31540000
168159

169160
assert config.identity_claim_key == "foo"
@@ -293,26 +284,6 @@ def test_invalid_config_options(app):
293284
with pytest.raises(RuntimeError):
294285
config.refresh_expires
295286

296-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = "banana"
297-
with pytest.raises(RuntimeError):
298-
config.blocklist_checks
299-
300-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = 1
301-
with pytest.raises(RuntimeError):
302-
config.blocklist_checks
303-
304-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = {"token_type": "access"}
305-
with pytest.raises(RuntimeError):
306-
config.blocklist_checks
307-
308-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = range(99)
309-
with pytest.raises(RuntimeError):
310-
config.blocklist_checks
311-
312-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = ["access", "banana"]
313-
with pytest.raises(RuntimeError):
314-
config.blocklist_checks
315-
316287

317288
def test_jwt_token_locations_config(app):
318289
with app.test_request_context():
@@ -340,32 +311,6 @@ def test_jwt_token_locations_config(app):
340311
assert config.token_location == locations
341312

342313

343-
def test_jwt_blocklist_token_checks_config(app):
344-
with app.test_request_context():
345-
allowed_token_types = ("access", "refresh")
346-
allowed_data_structures = (tuple, list, frozenset, set)
347-
348-
for token_type in allowed_token_types:
349-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = token_type
350-
assert config.blocklist_checks == (token_type,)
351-
352-
for token_types in (
353-
data_structure((token_type,))
354-
for data_structure in allowed_data_structures
355-
for token_type in allowed_token_types
356-
):
357-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = token_types
358-
assert config.blocklist_checks == token_types
359-
360-
for token_types in (
361-
data_structure(allowed_token_types[:i])
362-
for data_structure in allowed_data_structures
363-
for i in range(1, len(allowed_token_types))
364-
):
365-
app.config["JWT_BLOCKLIST_TOKEN_CHECKS"] = token_types
366-
assert config.blocklist_checks == token_types
367-
368-
369314
def test_csrf_protect_config(app):
370315
with app.test_request_context():
371316
app.config["JWT_TOKEN_LOCATION"] = ["headers"]

0 commit comments

Comments
 (0)