|
|
@@ -1,7 +1,11 @@
|
|
|
+import logging
|
|
|
+
|
|
|
from django.conf import settings
|
|
|
+from django.utils import timezone
|
|
|
from rest_framework import authentication, exceptions
|
|
|
from rest_framework.permissions import BasePermission, DjangoObjectPermissions, SAFE_METHODS
|
|
|
|
|
|
+from netbox.config import get_config
|
|
|
from users.models import Token
|
|
|
from utilities.request import get_client_ip
|
|
|
|
|
|
@@ -40,6 +44,16 @@ class TokenAuthentication(authentication.TokenAuthentication):
|
|
|
except model.DoesNotExist:
|
|
|
raise exceptions.AuthenticationFailed("Invalid token")
|
|
|
|
|
|
+ # Update last used, but only once per minute at most. This reduces write load on the database
|
|
|
+ if not token.last_used or (timezone.now() - token.last_used).total_seconds() > 60:
|
|
|
+ # If maintenance mode is enabled, assume the database is read-only, and disable updating the token's
|
|
|
+ # last_used time upon authentication.
|
|
|
+ if get_config().MAINTENANCE_MODE:
|
|
|
+ logger = logging.getLogger('netbox.auth.login')
|
|
|
+ logger.debug("Maintenance mode enabled: Disabling update of token's last used timestamp")
|
|
|
+ else:
|
|
|
+ Token.objects.filter(pk=token.pk).update(last_used=timezone.now())
|
|
|
+
|
|
|
# Enforce the Token's expiration time, if one has been set.
|
|
|
if token.is_expired:
|
|
|
raise exceptions.AuthenticationFailed("Token expired")
|