Skip to content

Commit 53732c2

Browse files
committed
Start recording last_used_at on SessionToken
1 parent b5e235e commit 53732c2

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.1.11 on 2019-08-14 07:17
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('rest_framework_sso', '0001_initial'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='sessiontoken',
15+
name='last_used_at',
16+
field=models.DateTimeField(blank=True, db_index=True, null=True),
17+
),
18+
]

rest_framework_sso/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class SessionToken(models.Model):
3232
user = models.ForeignKey(to=AUTH_USER_MODEL, related_name="+", on_delete=models.CASCADE, verbose_name=_("user"))
3333
ip_address = models.GenericIPAddressField(null=True, blank=True, db_index=True)
3434
user_agent = models.CharField(max_length=1000, blank=True)
35+
last_used_at = models.DateTimeField(null=True, blank=True, db_index=True)
3536
created_at = models.DateTimeField(auto_now_add=True, db_index=True)
3637
revoked_at = models.DateTimeField(null=True, blank=True, db_index=True)
3738

rest_framework_sso/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import jwt
77
from django.contrib.auth import get_user_model
88
from django.core.serializers.json import DjangoJSONEncoder
9-
from django.utils import six
9+
from django.utils import six, timezone
1010
from django.utils.translation import gettext_lazy as _
1111
from jwt.exceptions import MissingRequiredClaimError, InvalidIssuerError, InvalidTokenError
1212
from rest_framework import exceptions
@@ -147,7 +147,8 @@ def authenticate_payload(payload, request=None):
147147
)
148148
if request is not None:
149149
session_token.update_attributes(request=request)
150-
session_token.save()
150+
session_token.last_used_at = timezone.now()
151+
session_token.save()
151152
user = session_token.user
152153
except SessionToken.DoesNotExist:
153154
raise exceptions.AuthenticationFailed(_("Invalid token."))

0 commit comments

Comments
 (0)