mirror of https://gitlab.federez.net/re2o/re2o
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
979 B
25 lines
979 B
import datetime
|
|
from django.conf import settings
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from rest_framework.authentication import TokenAuthentication
|
|
from rest_framework import exceptions
|
|
|
|
class ExpiringTokenAuthentication(TokenAuthentication):
|
|
def authenticate_credentials(self, key):
|
|
model = self.get_model()
|
|
try:
|
|
token = model.objects.select_related('user').get(key=key)
|
|
except model.DoesNotExist:
|
|
raise exceptions.AuthenticationFailed(_('Invalid token.'))
|
|
|
|
if not token.user.is_active:
|
|
raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))
|
|
|
|
token_duration = datetime.timedelta(
|
|
seconds=settings.API_TOKEN_DURATION
|
|
)
|
|
utc_now = datetime.datetime.now(datetime.timezone.utc)
|
|
if token.created < utc_now - token_duration:
|
|
raise exceptions.AuthenticationFailed(_('Token has expired'))
|
|
|
|
return (token.user, token)
|
|
|