Skip to content

Commit d392c60

Browse files
authored
Fix #847 Log an exception when response status is not OK (#848)
* Fix #847 Log an exception if the response from authentication server is not successful By providing the status code and its reason to logger, the error message would be more explicit for debugger whenever there is an issue related to resource token or other errors. * Test to check logger information when auth response is not correct * Lint code to change bad quotes and add author information * Change import position * Add CHANGLOG for fix #847
1 parent f412b0b commit d392c60

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ pySilver
2929
Rodney Richardson
3030
Silvano Cerza
3131
Stéphane Raimbault
32+
Jun Zhou

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
### added
2020
* added `select_related` in intospect view for better query performance
2121

22+
### Fixed
23+
* #847: Fix inappropriate message when response from authentication server is not OK.
2224

2325
## [1.3.2] 2020-03-24
2426

oauth2_provider/oauth2_validators.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import base64
22
import binascii
3+
import http.client
34
import logging
45
from collections import OrderedDict
56
from datetime import datetime, timedelta
@@ -304,6 +305,14 @@ def _get_token_from_authentication_server(
304305
log.exception("Introspection: Failed POST to %r in token lookup", introspection_url)
305306
return None
306307

308+
# Log an exception when response from auth server is not successful
309+
if response.status_code != http.client.OK:
310+
log.exception("Introspection: Failed to get a valid response "
311+
"from authentication server. Status code: {}, "
312+
"Reason: {}.".format(response.status_code,
313+
response.reason))
314+
return None
315+
307316
try:
308317
content = response.json()
309318
except ValueError:

tests/test_oauth2_validators.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import datetime
33

44
from django.contrib.auth import get_user_model
5-
from django.test import TransactionTestCase
5+
from django.test import TestCase, TransactionTestCase
66
from django.utils import timezone
77
from oauthlib.common import Request
88

@@ -392,3 +392,26 @@ def test_validate_bearer_token_adds_error_to_the_request_when_a_invalid_custom_t
392392
self.assertDictEqual(self.request.oauth2_error, {
393393
"error": "invalid_token",
394394
})
395+
396+
397+
class TestOAuth2ValidatorErrorResourceToken(TestCase):
398+
"""The following tests check logger information when response from oauth2
399+
is unsuccessful.
400+
"""
401+
402+
def setUp(self):
403+
self.token = "test_token"
404+
self.introspection_url = "http://example.com/token/introspection/"
405+
self.introspection_token = "test_introspection_token"
406+
self.validator = OAuth2Validator()
407+
408+
def test_response_when_auth_server_response_return_404(self):
409+
with self.assertLogs(logger="oauth2_provider") as mock_log:
410+
self.validator._get_token_from_authentication_server(
411+
self.token, self.introspection_url,
412+
self.introspection_token, None)
413+
self.assertIn("ERROR:oauth2_provider:Introspection: Failed to "
414+
"get a valid response from authentication server. "
415+
"Status code: 404, Reason: "
416+
"Not Found.\nNoneType: None",
417+
mock_log.output)

0 commit comments

Comments
 (0)