1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Add last_used to Token model and update when used

This commit is contained in:
Pieter Lambrecht
2022-06-17 14:45:56 +02:00
parent bb2235b05e
commit c04b4bbbfa
4 changed files with 28 additions and 1 deletions

View File

@ -1,4 +1,5 @@
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
@ -18,6 +19,10 @@ class TokenAuthentication(authentication.TokenAuthentication):
except model.DoesNotExist:
raise exceptions.AuthenticationFailed("Invalid token")
# Update last used.
token.last_used = timezone.now()
token.save()
# Enforce the Token's expiration time, if one has been set.
if token.is_expired:
raise exceptions.AuthenticationFailed("Token expired")

View File

@ -58,7 +58,7 @@ class UserAdmin(UserAdmin_):
class TokenAdmin(admin.ModelAdmin):
form = forms.TokenAdminForm
list_display = [
'key', 'user', 'created', 'expires', 'write_enabled', 'description'
'key', 'user', 'created', 'expires', 'last_used', 'write_enabled', 'description'
]

View File

@ -0,0 +1,18 @@
# Generated by Django 4.0.4 on 2022-06-16 15:26
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('users', '0002_standardize_id_fields'),
]
operations = [
migrations.AddField(
model_name='token',
name='last_used',
field=models.DateTimeField(blank=True, null=True),
),
]

View File

@ -203,6 +203,10 @@ class Token(models.Model):
blank=True,
null=True
)
last_used = models.DateTimeField(
blank=True,
null=True
)
key = models.CharField(
max_length=40,
unique=True,