|
| 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