Skip to content

Commit e42ca9e

Browse files
committed
Create device authorization view
This view is to be used in an authorization server in order to provide a /device endpoint
1 parent bd8c497 commit e42ca9e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.settings import oauth2_settings
12+
from oauth2_provider.views.mixins import OAuthLibMixin
13+
14+
15+
@method_decorator(csrf_exempt, name="dispatch")
16+
@method_decorator(login_not_required, name="dispatch")
17+
class DeviceAuthorizationView(OAuthLibMixin, View):
18+
server_class = DeviceApplicationServer
19+
20+
def post(self, request, *args, **kwargs):
21+
headers, response, status = self.create_device_authorization_response(request)
22+
23+
device_request = DeviceRequest(
24+
client_id=request.POST["client_id"], scope=request.POST.get("scope", oauth2_settings.READ_SCOPE)
25+
)
26+
27+
if status != 200:
28+
return http.JsonResponse(data=json.loads(response), status=status, headers=headers)
29+
30+
device_response = DeviceCodeResponse(**response)
31+
create_device(device_request, device_response)
32+
33+
return http.JsonResponse(data=response, status=status, headers=headers)

0 commit comments

Comments
 (0)