Skip to content

Commit f32c57d

Browse files
committed
Bugfix: handle missing client_id
This commit fixes a bug where the application crashes if the client_id parameter is not sent in the body, by checking the status returned by the oauthlib function before any further processing. Test for this usecase is added.
1 parent 5b83ac6 commit f32c57d

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

oauth2_provider/views/device.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ class DeviceAuthorizationView(OAuthLibMixin, View):
2929
def post(self, request, *args, **kwargs):
3030
headers, response, status = self.create_device_authorization_response(request)
3131

32-
device_request = DeviceRequest(client_id=request.POST["client_id"], scope=request.POST.get("scope"))
33-
3432
if status != 200:
3533
return http.JsonResponse(data=json.loads(response), status=status, headers=headers)
3634

35+
device_request = DeviceRequest(client_id=request.POST["client_id"], scope=request.POST.get("scope"))
3736
device_response = DeviceCodeResponse(**response)
3837
create_device_grant(device_request, device_response)
3938

tests/test_device.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,28 @@ def test_incorrect_client_id_sent(self):
497497
"error_description": "Invalid client_id parameter value.",
498498
}
499499

500+
def test_missing_client_id(self):
501+
"""
502+
Ensure the correct error is returned when the client id is missing.
503+
"""
504+
request_data: dict[str, str] = {
505+
"not_client_id": "client_id_that_does_not_exist",
506+
}
507+
request_as_x_www_form_urlencoded: str = urlencode(request_data)
508+
509+
response: django.http.response.JsonResponse = self.client.post(
510+
reverse("oauth2_provider:device-authorization"),
511+
data=request_as_x_www_form_urlencoded,
512+
content_type="application/x-www-form-urlencoded",
513+
)
514+
515+
assert response.status_code == 400
516+
517+
assert response.json() == {
518+
"error": "invalid_request",
519+
"error_description": "Missing client_id parameter.",
520+
}
521+
500522
def test_device_confirm_and_user_code_views_require_login(self):
501523
URLs = [
502524
reverse("oauth2_provider:device-confirm", kwargs={"user_code": None, "client_id": "abc"}),

0 commit comments

Comments
 (0)