Skip to content

Commit f86dfb8

Browse files
george-pearsonauvipy
authored andcommitted
Use getattr for oauth2_error access (#633) (#716)
* Use getattr for oauth2_error access (#633) If the request doesn't have a oauth2_error property the authenticate_header method errors. This can happen when the oauthlib_core.verify_request method raises exceptions in authenticate. It is useful to be able to raise AuthenticationFailed exceptions from within a custom validate_bearer_token method which causes this. * Add Test OAuth2Authentication authenticate override Added a test for if an authenticate method that returns None is used. This should result in a HTTP 401 response for any request.
1 parent d5da62b commit f86dfb8

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

oauth2_provider/contrib/rest_framework/authentication.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def authenticate_header(self, request):
3939
www_authenticate_attributes = OrderedDict([
4040
("realm", self.www_authenticate_realm,),
4141
])
42-
www_authenticate_attributes.update(request.oauth2_error)
42+
oauth2_error = getattr(request, "oauth2_error", {})
43+
www_authenticate_attributes.update(oauth2_error)
4344
return "Bearer {attributes}".format(
4445
attributes=self._dict_to_string(www_authenticate_attributes),
4546
)

tests/test_rest_framework.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ class TokenHasScopeViewWrongAuth(BrokenOAuth2View):
9898
class MethodScopeAltViewWrongAuth(BrokenOAuth2View):
9999
permission_classes = [TokenMatchesOASRequirements]
100100

101+
class AuthenticationNone(OAuth2Authentication):
102+
def authenticate(self, request):
103+
return None
104+
105+
class AuthenticationNoneOAuth2View(MockView):
106+
authentication_classes = [AuthenticationNone]
107+
101108

102109
urlpatterns = [
103110
url(r"^oauth2/", include("oauth2_provider.urls")),
@@ -110,6 +117,7 @@ class MethodScopeAltViewWrongAuth(BrokenOAuth2View):
110117
url(r"^oauth2-method-scope-test/.*$", MethodScopeAltView.as_view()),
111118
url(r"^oauth2-method-scope-fail/$", MethodScopeAltViewBad.as_view()),
112119
url(r"^oauth2-method-scope-missing-auth/$", MethodScopeAltViewWrongAuth.as_view()),
120+
url(r"^oauth2-authentication-none/$", AuthenticationNoneOAuth2View.as_view()),
113121
]
114122

115123

@@ -399,3 +407,8 @@ def test_method_scope_alt_missing_scope_attr(self):
399407
with self.assertRaises(AssertionError) as e:
400408
self.client.get("/oauth2-method-scope-missing-auth/", HTTP_AUTHORIZATION=auth)
401409
self.assertTrue("`oauth2_provider.rest_framework.OAuth2Authentication`" in str(e.exception))
410+
411+
def test_authentication_none(self):
412+
auth = self._create_authorization_header(self.access_token.token)
413+
response = self.client.get("/oauth2-authentication-none/", HTTP_AUTHORIZATION=auth)
414+
self.assertEqual(response.status_code, 401)

0 commit comments

Comments
 (0)