ungleich-otp/otpauth/views.py

63 lines
2.0 KiB
Python

from django.shortcuts import render
from rest_framework import viewsets, serializers
from rest_framework.decorators import action
from rest_framework.response import Response
from django.http import JsonResponse
from otpauth.serializer import VerifySerializer, OTPSerializer, TokenSerializer
from otpauth.models import OTPSeed
# 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)
class OTPVerifyViewSet(viewsets.ModelViewSet):
serializer_class = OTPSerializer
queryset = OTPSeed.objects.all()
def list(self, request):
print("Liiiiiiiisting")
# data = serializers.serialize('json', self.get_queryset())
# return HttpResponse(data, content_type="application/json")
obj = [(o.name, o.realm, o.seed) for o in OTPSeed.objects.all()]
# obj = OTPSeed.objects.all()
return Response(obj)
# return Response({'LISTstatus': 'OK'})
@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)