Skip to content

Commit ed70a4f

Browse files
Merge pull request #985 from basedosdados/feat/add-uuid-to-token
feat: add user UUID to jwt token
2 parents 07c66bb + d5f4cf3 commit ed70a4f

5 files changed

Lines changed: 40 additions & 1 deletion

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# Generated by Django 4.2.10 on 2026-02-12 15:03
3+
4+
import uuid
5+
6+
from django.db import migrations, models
7+
8+
9+
class Migration(migrations.Migration):
10+
dependencies = [
11+
("account", "0027_rename_has_access_to_chatbot_account_has_chatbot_access"),
12+
]
13+
14+
operations = [
15+
migrations.AlterField(
16+
model_name="account",
17+
name="uuid",
18+
field=models.UUIDField(default=uuid.uuid4, unique=True),
19+
),
20+
]

backend/apps/account/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ class Account(BaseModel, AbstractBaseUser, PermissionsMixin):
208208
(COLABORADOR, "Colaborador"),
209209
)
210210

211-
uuid = models.UUIDField(primary_key=False, default=uuid4)
211+
uuid = models.UUIDField(primary_key=False, default=uuid4, unique=True)
212212

213213
email = models.EmailField("Email", unique=True)
214214
gcp_email = models.EmailField("GCP email", null=True, blank=True) # Google Cloud Platform email

backend/apps/core/management/commands/_disable_unhealthy_flow_schedules/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Querys(Enum):
2929
where: {
3030
flow_id: { _eq: $flow_id }
3131
state: { _in: ["Success", "Failed"] }
32+
start_time: { _is_null: false }
3233
}
3334
order_by: { start_time: desc }
3435
limit: 2

backend/custom/graphql_jwt.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from functools import wraps
33
from re import findall
4+
from typing import Any
45

56
from django.db.models import Q
67
from graphene import Field, ObjectType, String
@@ -9,6 +10,22 @@
910
from graphql_jwt.decorators import context
1011
from graphql_jwt.relay import JSONWebTokenMutation
1112
from graphql_jwt.settings import jwt_settings
13+
from graphql_jwt.utils import jwt_payload
14+
15+
16+
def jwt_payload_with_uuid(user, context=None) -> dict[str, Any]:
17+
"""Custom JWT payload handler that adds the user's UUID to the token payload.
18+
19+
Args:
20+
user (Account): An instance of backend.apps.account.models.Account
21+
context (Any, optional): Django request context. Defaults to None.
22+
23+
Returns:
24+
dict[str, Any]: JWT token payload with the user's UUID included.
25+
"""
26+
payload = jwt_payload(user, context)
27+
payload["uuid"] = str(user.uuid)
28+
return payload
1229

1330

1431
class User(ObjectType):

backend/settings/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
"JWT_EXPIRATION_DELTA": timedelta(days=7),
203203
"JWT_REFRESH_EXPIRATION_DELTA": timedelta(days=14),
204204
"JWT_ALLOW_ANY_HANDLER": "backend.custom.graphql_jwt.allow_any",
205+
"JWT_PAYLOAD_HANDLER": "backend.custom.graphql_jwt.jwt_payload_with_uuid",
205206
}
206207

207208
# Translations

0 commit comments

Comments
 (0)