ungleich-otp/otpauth/views.py

63 lines
1.9 KiB
Python
Raw Normal View History

2018-10-26 19:08:01 +00:00
from django.shortcuts import render
2018-11-17 21:15:17 +00:00
2018-11-17 10:21:35 +00:00
from rest_framework import viewsets
2018-11-17 21:15:17 +00:00
from rest_framework.decorators import action
from rest_framework.response import Response
2018-11-18 12:42:16 +00:00
from django.http import JsonResponse
from otpauth.serializer import VerifySerializer, OTPSerializer, TokenSerializer
from otpauth.models import OTPSeed
2018-11-17 10:39:42 +00:00
# class OTPVerifyViewSet(viewsets.ModelViewSet):
# serializer_class = OTPSerializer
# queryset = OTPSeed.objects.all()
# @action(detail=False, methods=['post'])
# def verify(self, request):
# """the standard serializer above already verified that
# (name, realm, token) is valid.
# Now we inspect the verify-prefixed names and return ok,
# if they also verify
# """
# serializer = VerifySerializer(data=request.data)
# if serializer.is_valid():
# serializer.save()
# return Response({'status': 'OK'})
# return JsonResponse(serializer.errors, status=400)
2018-12-24 19:28:21 +00:00
class OTPVerifyViewSet(viewsets.ModelViewSet):
serializer_class = TokenSerializer
queryset = OTPSeed.objects.all()
2018-11-17 17:48:12 +00:00
def list(self, request):
print("Liiiiiiiisting")
data = serializers.serialize('json', self.get_queryset())
return HttpResponse(data, content_type="application/json")
obj = [o.name for o in OTPSeed.objects.all()]
obj = OTPSeed.objects.all()
return Response(obj)
# return Response({'LISTstatus': 'OK'})
2018-11-17 21:15:17 +00:00
@action(detail=False, methods=['post'])
def verify(self, request):
2018-12-24 19:58:08 +00:00
"""the standard serializer above already verified that
(name, realm, token) is valid.
Now we inspect the verify-prefixed names and return ok,
if they also verify
"""
2018-11-17 21:15:17 +00:00
serializer = VerifySerializer(data=request.data)
if serializer.is_valid():
2018-11-17 21:53:51 +00:00
serializer.save()
2018-11-17 21:15:17 +00:00
return Response({'status': 'OK'})
return JsonResponse(serializer.errors, status=400)