ungleich-otp/otpauth/models.py

41 lines
1.2 KiB
Python
Raw Normal View History

2018-10-26 19:08:01 +00:00
from django.db import models
2018-11-17 20:45:53 +00:00
from django.contrib.auth.models import AbstractUser
2018-12-30 16:57:02 +00:00
from rest_framework import exceptions
from rest_framework import authentication
2018-12-30 16:57:02 +00:00
2018-11-17 22:00:36 +00:00
class OTPSeed(AbstractUser):
2018-11-17 09:01:24 +00:00
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=128)
realm = models.CharField(max_length=128)
2018-10-26 19:48:21 +00:00
seed = models.CharField(max_length=128)
class Meta:
unique_together = (('name', 'realm'),)
2018-11-17 09:14:37 +00:00
def __str__(self):
return "'{}'@{}".format(self.name, self.realm)
2018-11-17 20:45:53 +00:00
@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))
2018-11-18 12:05:21 +00:00
user, token = serializer.save()
else:
print("Invalide serialize,")
raise exceptions.AuthenticationFailed()
2018-11-18 12:05:21 +00:00
return (user, token)