ungleich-otp/otpauth/models.py
2018-12-30 22:41:17 +01:00

41 lines
1.2 KiB
Python

from django.db import models
from django.contrib.auth.models import AbstractUser
from rest_framework import exceptions
from rest_framework import authentication
class OTPSeed(AbstractUser):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=128)
realm = models.CharField(max_length=128)
seed = models.CharField(max_length=128)
class Meta:
unique_together = (('name', 'realm'),)
def __str__(self):
return "'{}'@{}".format(self.name, self.realm)
@property
def auth_name(self):
print("authname: {}".format(self))
@auth_name.setter
def auth_name(self):
print("authname: {}".format(self))
from otpauth.serializer import TokenSerializer
class OTPAuthentication(authentication.BaseAuthentication):
def authenticate(self, request):
serializer = TokenSerializer(data=request.data)
if serializer.is_valid():
print("trying to save... {}".format(serializer))
user, token = serializer.save()
else:
print("Invalide serialize,")
raise exceptions.AuthenticationFailed()
print("AUTH DONE")
return (user, token)