26789ff11b
To prevent the following exception: File "/home/nico/vcs/ungleich-otp/venv/lib/python3.5/site-packages/django/db/models/base.py", line 87, in __new__ app_config = apps.get_containing_app_config(module) File "/home/nico/vcs/ungleich-otp/venv/lib/python3.5/site-packages/django/apps/registry.py", line 249, in get_containing_app_config self.check_apps_ready() File "/home/nico/vcs/ungleich-otp/venv/lib/python3.5/site-packages/django/apps/registry.py", line 132, in check_apps_ready raise AppRegistryNotReady("Apps aren't loaded yet.") django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. (venv) [12:41] line:ungleichotp%
46 lines
1.3 KiB
Python
46 lines
1.3 KiB
Python
from django.db import models
|
|
from django.contrib.auth.models import AbstractUser
|
|
|
|
# Create your models here.
|
|
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)
|
|
|
|
# @classmethod
|
|
# def get_username(cls):
|
|
# pass
|
|
|
|
# @classmethod
|
|
# def check_password(cls, raw_password):
|
|
# """ receives a time based token"""
|
|
# pass
|
|
|
|
# @classmethod
|
|
# def has_usable_password(cls):
|
|
# pass
|
|
|
|
|
|
from rest_framework import exceptions
|
|
from rest_framework import authentication
|
|
from otpauth.models import OTPSeed
|
|
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 = serializer.save()
|
|
else:
|
|
raise exceptions.AuthenticationFailed()
|
|
|
|
return (user, None)
|