Parcourir la source

Addded is_expired property to Token

Jeremy Stretch il y a 8 ans
Parent
commit
6be465fe9b
2 fichiers modifiés avec 8 ajouts et 2 suppressions
  1. 7 0
      netbox/users/models.py
  2. 1 2
      netbox/utilities/api.py

+ 7 - 0
netbox/users/models.py

@@ -4,6 +4,7 @@ import os
 from django.contrib.auth.models import User
 from django.contrib.auth.models import User
 from django.db import models
 from django.db import models
 from django.utils.encoding import python_2_unicode_compatible
 from django.utils.encoding import python_2_unicode_compatible
+from django.utils import timezone
 
 
 
 
 @python_2_unicode_compatible
 @python_2_unicode_compatible
@@ -33,3 +34,9 @@ class Token(models.Model):
     def generate_key(self):
     def generate_key(self):
         # Generate a random 160-bit key expressed in hexadecimal.
         # Generate a random 160-bit key expressed in hexadecimal.
         return binascii.hexlify(os.urandom(20)).decode()
         return binascii.hexlify(os.urandom(20)).decode()
+
+    @property
+    def is_expired(self):
+        if self.expires is not None and timezone.now() > self.expires:
+            return True
+        return False

+ 1 - 2
netbox/utilities/api.py

@@ -1,5 +1,4 @@
 from django.conf import settings
 from django.conf import settings
-from django.utils import timezone
 
 
 from rest_framework import authentication, exceptions
 from rest_framework import authentication, exceptions
 from rest_framework.exceptions import APIException
 from rest_framework.exceptions import APIException
@@ -31,7 +30,7 @@ class TokenAuthentication(authentication.TokenAuthentication):
             raise exceptions.AuthenticationFailed("Invalid token")
             raise exceptions.AuthenticationFailed("Invalid token")
 
 
         # Enforce the Token's expiration time, if one has been set.
         # Enforce the Token's expiration time, if one has been set.
-        if token.expires and token.expires < timezone.now():
+        if token.expires and not token.is_expired:
             raise exceptions.AuthenticationFailed("Token expired")
             raise exceptions.AuthenticationFailed("Token expired")
 
 
         if not token.user.is_active:
         if not token.user.is_active: