-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement feature to allow customizing token claim from user attributes
- Loading branch information
1 parent
e23792d
commit 275bf29
Showing
9 changed files
with
94 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from typing import Any | ||
from uuid import UUID | ||
|
||
from django.core.serializers.json import DjangoJSONEncoder | ||
|
||
|
||
class TokenUserEncoder(DjangoJSONEncoder): | ||
def default(self, o: Any) -> Any: | ||
if isinstance(o, UUID): | ||
return str(o) | ||
|
||
return super().default(o) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json | ||
from datetime import datetime | ||
from uuid import uuid4 | ||
|
||
from django.test import TestCase | ||
|
||
from ninja_simple_jwt.jwt.json_encode import TokenUserEncoder | ||
|
||
|
||
class TestDjangoUserEncoder(TestCase): | ||
def test_encoder_can_serialize_datetime(self) -> None: | ||
test_data = datetime(2012, 1, 14, 12, 0, 1) | ||
|
||
result = json.dumps(test_data, cls=TokenUserEncoder) | ||
|
||
self.assertEqual('"2012-01-14T12:00:01"', result) | ||
|
||
def test_encoder_can_serialize_uuid(self) -> None: | ||
test_uuid = uuid4() | ||
|
||
result = json.dumps(test_uuid, cls=TokenUserEncoder) | ||
|
||
self.assertEqual(f'"{str(test_uuid)}"', result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters