Skip to content

Commit 42efa46

Browse files
committed
Make user field on abstract refresh token nullable
why? DOT currently assume the user will be derived from the django request.user object (from the logic throughout DOT, not the model itself). Since the device flow happens out of band there is no request.user available when the call to token is made, we have to make this field none. How do I handle it in my own custom auth server: In my custom auth server how I associate a refresh token with a user is to have a field (column in the refresh token table) that has the payload of the original JWT what was made when the refresh token was issued and I use the sub claim in the payload to know “this user has the refresh token” which prevents it relying on django solely for the user information but the stateless JWT instead
1 parent 168f921 commit 42efa46

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 4.2.17 on 2025-01-13 17:24
2+
3+
from django.conf import settings
4+
from django.db import migrations, models
5+
import django.db.models.deletion
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
dependencies = [
11+
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
12+
('oauth2_provider', '0013_alter_application_authorization_grant_type_device'),
13+
]
14+
15+
operations = [
16+
migrations.AlterField(
17+
model_name='refreshtoken',
18+
name='user',
19+
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s', to=settings.AUTH_USER_MODEL),
20+
),
21+
]

oauth2_provider/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ class AbstractRefreshToken(models.Model):
503503

504504
id = models.BigAutoField(primary_key=True)
505505
user = models.ForeignKey(
506-
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s"
506+
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="%(app_label)s_%(class)s", null=True
507507
)
508508
token = models.CharField(max_length=255)
509509
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL, on_delete=models.CASCADE)

0 commit comments

Comments
 (0)