Skip to content

Commit 1c2d996

Browse files
dawidwolski-identtpre-commit-ci[bot]dopry
authored
Fix: AttributeError in IntrospectTokenView when token not provided (#1562)
When token is not provided explicitly respond with a 400 status and properly structured JSON error. Before this a 500 was being returned for an unhandled exception. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Darrel O'Pry <darrel.opry@spry-group.com>
1 parent 121abd4 commit 1c2d996

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
* #1512 client_secret not marked sensitive
1919
* #1521 Fix 0012 migration loading access token table into memory
2020
* #1584 Fix IDP container in docker compose environment could not find templates and static files.
21+
* #1562 Fix: Handle AttributeError in IntrospectTokenView
2122
<!--
2223
### Security
2324
-->

oauth2_provider/views/introspect.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class IntrospectTokenView(ClientProtectedScopedResourceView):
2626

2727
@staticmethod
2828
def get_token_response(token_value=None):
29+
if token_value is None:
30+
return JsonResponse(
31+
{"error": "invalid_request", "error_description": "Token parameter is missing."},
32+
status=400,
33+
)
2934
try:
3035
token_checksum = hashlib.sha256(token_value.encode("utf-8")).hexdigest()
3136
token = (

tests/test_introspection_view.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,20 @@ def test_view_post_notexisting_token(self):
279279
},
280280
)
281281

282+
def test_view_post_no_token(self):
283+
"""
284+
Test that when you pass no token HTTP 400 is returned
285+
"""
286+
auth_headers = {
287+
"HTTP_AUTHORIZATION": "Bearer " + self.resource_server_token.token,
288+
}
289+
response = self.client.post(reverse("oauth2_provider:introspect"), **auth_headers)
290+
291+
self.assertEqual(response.status_code, 400)
292+
content = response.json()
293+
self.assertIsInstance(content, dict)
294+
self.assertEqual(content["error"], "invalid_request")
295+
282296
def test_view_post_valid_client_creds_basic_auth(self):
283297
"""Test HTTP basic auth working"""
284298
auth_headers = get_basic_auth_header(self.application.client_id, CLEARTEXT_SECRET)

tox.ini

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ envlist =
55
docs,
66
lint,
77
sphinxlint,
8-
py{38,39,310,311,312}-dj42,
9-
py{310,311,312}-dj50,
10-
py{310,311,312}-dj51,
11-
py{310,311,312}-djmain,
8+
py{38,39,310,311,312,313}-dj42,
9+
py{310,311,312,313}-dj50,
10+
py{310,311,312,313}-dj51,
11+
py{310,311,312,313}-djmain,
1212
py39-multi-db-dj-42
1313

1414
[gh-actions]
@@ -18,6 +18,7 @@ python =
1818
3.10: py310
1919
3.11: py311
2020
3.12: py312
21+
3.13: py313
2122

2223
[gh-actions:env]
2324
DJANGO =
@@ -54,7 +55,7 @@ deps =
5455
passenv =
5556
PYTEST_ADDOPTS
5657

57-
[testenv:py{310,311,312}-djmain]
58+
[testenv:py{310,311,312,313}-djmain]
5859
ignore_errors = true
5960
ignore_outcome = true
6061

0 commit comments

Comments
 (0)