2022-04-13 12:24:56 +00:00
|
|
|
from django.utils import timezone
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
|
|
|
|
from authentication.models import TempToken
|
2024-12-09 06:13:44 +00:00
|
|
|
from .base import JMSBaseAuthBackend
|
2022-04-13 12:24:56 +00:00
|
|
|
|
|
|
|
|
2024-12-09 06:13:44 +00:00
|
|
|
class TempTokenAuthBackend(JMSBaseAuthBackend):
|
2022-04-13 12:24:56 +00:00
|
|
|
model = TempToken
|
|
|
|
|
2024-12-09 06:13:44 +00:00
|
|
|
@staticmethod
|
|
|
|
def is_enabled():
|
|
|
|
return settings.AUTH_TEMP_TOKEN
|
|
|
|
|
|
|
|
def authenticate(self, request, username='', password=''):
|
2022-04-13 12:24:56 +00:00
|
|
|
token = self.model.objects.filter(username=username, secret=password).first()
|
|
|
|
if not token:
|
|
|
|
return None
|
|
|
|
if not token.is_valid:
|
|
|
|
raise PermissionDenied('Token is invalid, expired at {}'.format(token.date_expired))
|
|
|
|
|
|
|
|
token.verified = True
|
|
|
|
token.date_verified = timezone.now()
|
|
|
|
token.save()
|
|
|
|
return token.user
|
|
|
|
|