Skip to content

Commit fc67fc7

Browse files
duzumakidopry
authored andcommitted
Create device authorization view
This view is to be used in an authorization server in order to provide a /device endpoint
1 parent 40ddf1e commit fc67fc7

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

oauth2_provider/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
path("token/", views.TokenView.as_view(), name="token"),
1212
path("revoke_token/", views.RevokeTokenView.as_view(), name="revoke-token"),
1313
path("introspect/", views.IntrospectTokenView.as_view(), name="introspect"),
14+
path("device_authorization/", views.DeviceAuthorizationView.as_view(), name="device-authorization")
1415
]
1516

1617

oauth2_provider/views/device.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json
2+
3+
from django import http
4+
from django.utils.decorators import method_decorator
5+
from django.views.decorators.csrf import csrf_exempt
6+
from django.views.generic import View
7+
from oauthlib.oauth2 import DeviceApplicationServer
8+
9+
from oauth2_provider.compat import login_not_required
10+
from oauth2_provider.models import DeviceCodeResponse, DeviceRequest, create_device
11+
from oauth2_provider.views.mixins import OAuthLibMixin
12+
13+
14+
@method_decorator(csrf_exempt, name="dispatch")
15+
@method_decorator(login_not_required, name="dispatch")
16+
class DeviceAuthorizationView(OAuthLibMixin, View):
17+
server_class = DeviceApplicationServer
18+
19+
def post(self, request, *args, **kwargs):
20+
headers, response, status = self.create_device_authorization_response(request)
21+
22+
device_request = DeviceRequest(client_id=request.POST["client_id"], scope=request.POST.get("scope"))
23+
24+
if status != 200:
25+
return http.JsonResponse(data=json.loads(response), status=status, headers=headers)
26+
27+
device_response = DeviceCodeResponse(**response)
28+
create_device(device_request, device_response)
29+
30+
return http.JsonResponse(data=response, status=status, headers=headers)

0 commit comments

Comments
 (0)