2018-11-18 13:33:30 +00:00
|
|
|
from django.contrib.auth.models import User
|
2018-11-18 14:41:47 +00:00
|
|
|
from rest_framework import authentication, exceptions, serializers
|
2018-11-18 13:33:30 +00:00
|
|
|
|
2018-11-18 14:41:47 +00:00
|
|
|
import urllib.request
|
|
|
|
import pyotp
|
|
|
|
import json
|
|
|
|
|
|
|
|
# For parsing
|
|
|
|
class TokenSerializer(serializers.Serializer):
|
|
|
|
name = serializers.CharField(max_length=128)
|
|
|
|
token = serializers.CharField(max_length=128)
|
|
|
|
realm = serializers.CharField(max_length=128)
|
|
|
|
|
|
|
|
token_name = 'token'
|
|
|
|
name_name = 'name'
|
|
|
|
realm_name = 'realm'
|
|
|
|
|
|
|
|
def __init__(self, name, realm, seed, serverurl, *args, **kwargs):
|
|
|
|
self.name = name
|
|
|
|
self.realm = realm
|
|
|
|
self.seed = seed
|
|
|
|
self.serverurl = serverurl
|
|
|
|
|
|
|
|
super(serializers.Serializer, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
to_send = {}
|
|
|
|
|
|
|
|
# Client credentials to be verified
|
|
|
|
to_send['verifytoken'] = self.validated_data.get(self.token_name)
|
|
|
|
to_send['verifyname'] = self.validated_data.get(self.name_name)
|
|
|
|
to_send['verifyrealm'] = self.validated_data.get(self.real_name)
|
|
|
|
|
|
|
|
# Our credentials
|
|
|
|
to_send['token'] = pyotp.TOTP(self.seed)
|
|
|
|
to_send['name'] = self.name
|
|
|
|
to_send['realm'] = self.realm
|
|
|
|
|
|
|
|
data = json.dumps(to_send).encode("utf-8")
|
|
|
|
|
|
|
|
req = urllib.request.Request(url=serverurl,
|
|
|
|
data=data,
|
|
|
|
headers={'Content-Type': 'application/json'},
|
|
|
|
method='POST')
|
|
|
|
|
|
|
|
f = urllib.request.urlopen(req)
|
|
|
|
|
|
|
|
if not f.status == 200:
|
|
|
|
raise exceptions.AuthenticationFailed()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class OTPAuthentication(authentication.BaseAuthentication):
|
2018-11-18 13:33:30 +00:00
|
|
|
def authenticate(self, request):
|
2018-11-18 14:41:47 +00:00
|
|
|
serializer = TokenSerializer(data=request.data)
|
2018-11-18 13:33:30 +00:00
|
|
|
|
2018-11-18 14:41:47 +00:00
|
|
|
if serializer.is_valid():
|
|
|
|
print("trying to save... {}".format(serializer))
|
|
|
|
user, token = serializer.save()
|
|
|
|
else:
|
|
|
|
raise exceptions.AuthenticationFailed()
|
2018-11-18 13:33:30 +00:00
|
|
|
|
2018-11-18 14:41:47 +00:00
|
|
|
return (user, token)
|