Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Add a URL to check if the device has been claimed #220

Merged
merged 1 commit into from
May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions backend/device_registry/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,22 @@ def claim_by_link(request):
return Response(f'Device {device.device_id} claimed!')
return Response('Device not found', status=status.HTTP_404_NOT_FOUND)


@api_view(['GET'])
@permission_classes((permissions.AllowAny,))
def mtls_is_claimed_view(request, format=None):
"""
Return claimed status of a device.
"""
device_id = is_mtls_authenticated(request)

if not device_id:
return Response(
'Invalid request.',
status=status.HTTP_400_BAD_REQUEST
)
if type(device_id) is Response:
return device_id

device = Device.objects.get(device_id=device_id)
return Response({'claimed': device.claimed})
23 changes: 22 additions & 1 deletion backend/device_registry/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.x509.oid import NameOID
from rest_framework.test import APIRequestFactory
from rest_framework.test import APIRequestFactory, APITestCase

from device_registry import ca_helper
from .api_views import mtls_ping_view, claim_by_link, renew_expired_cert_view
Expand Down Expand Up @@ -731,3 +731,24 @@ def test_logins(self):
self.assertEqual(response.status_code, 200)
self.assertContains(response, '<pre>pi:')
self.assertContains(response, 'success: 1')


class APIIsClaimedTest(APITestCase):
def setUp(self):
self.url = reverse('mtls-is_claimed')
User = get_user_model()
self.user = User.objects.create_user('test')
self.device0 = Device.objects.create(device_id='device0.d.wott-dev.local', owner=self.user)
self.headers = {
'HTTP_SSL_CLIENT_SUBJECT_DN': 'CN=device0.d.wott-dev.local',
'HTTP_SSL_CLIENT_VERIFY': 'SUCCESS'
}

def test_get(self):
response = self.client.get(
self.url,
**self.headers,
format='json'
)
self.assertEqual(response.status_code, 200)
self.assertDictEqual(response.json(), {'claimed': True})
5 changes: 4 additions & 1 deletion backend/device_registry/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@
api_views.mtls_tester_view,
name='mtls-tester'),
path('api/{}/action/<int:action_id>/<str:action_name>'.format(api_version),
api_views.action_view, name='action')
api_views.action_view, name='action'),
path('api/{}/claimed'.format(api_version),
api_views.mtls_is_claimed_view,
name='mtls-is_claimed'),
]

# Front-end
Expand Down