Skip to content

Commit b58e122

Browse files
committed
code refactor
1 parent fce1fc4 commit b58e122

File tree

12 files changed

+127
-55
lines changed

12 files changed

+127
-55
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
* Fix BrowsableAPIRenderer needing `media_type` ([#426](https://github.com/jazzband/django-rest-framework-simplejwt/pull/426))
3232
* Fix blacklist migrations for multiple databases ([#429](https://github.com/jazzband/django-rest-framework-simplejwt/pull/429))
3333
* Fix Django 3.2 `default_app_config` deprecation ([#415](https://github.com/jazzband/django-rest-framework-simplejwt/pull/415))
34-
* Fix docs specifying `INSTALLED_APPS` for SimpleJWT iff you want translations ([#420](https://github.com/jazzband/django-rest-framework-simplejwt/pull/420))
34+
* Fix docs specifying `INSTALLED_APPS` for NinjaJWT if you want translations ([#420](https://github.com/jazzband/django-rest-framework-simplejwt/pull/420))
3535
* Fix drf-yasg API Schema generation for `TokenRefreshSerializer` ([#396](https://github.com/jazzband/django-rest-framework-simplejwt/pull/396))
3636
* Fix invalid syntax in docs for `INSTALLED_APPS` ([#416](https://github.com/jazzband/django-rest-framework-simplejwt/pull/416))
3737

README.md

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Ninja JWT
22
![Test](https://github.com/eadwinCode/django-ninja-jwt/workflows/Test/badge.svg)
33
[![PyPI version](https://badge.fury.io/py/django-ninja-jwt.svg)](https://badge.fury.io/py/django-ninja-jwt)
4-
[![PyPI version](https://img.shields.io/pypi/v/djangorestframework-simplejwt.svg)](https://pypi.python.org/pypi/django-ninja-jwt)
4+
[![PyPI version](https://img.shields.io/pypi/v/django-ninja-jwt.svg)](https://pypi.python.org/pypi/django-ninja-jwt)
55
[![PyPI version](https://img.shields.io/pypi/pyversions/django-ninja-jwt.svg)](https://pypi.python.org/pypi/django-ninja-jwt)
66
[![PyPI version](https://img.shields.io/pypi/djversions/django-ninja-jwt.svg)](https://pypi.python.org/pypi/django-ninja-jwt)
77

@@ -12,3 +12,98 @@ Ninja JWT is JSON Web Token plugin for Django-Ninja. The library is a fork of [S
1212
This library does not fix any issues from the parent source. It only added support for Django-Ninja and removed restframework dependency, but be rest assured that updates from SIMPLE JWT will reflect here.
1313

1414
For full documentation, [visit](https://eadwincode.github.io/django-ninja-jwt/).
15+
16+
#### Requirements
17+
- Python >= 3.6
18+
- Django >= 2.1
19+
- Django-Ninja >= 0.16.1
20+
- Ninja-Schema >= 0.12.2
21+
- Django-Ninja-Extra >= 0.11.0
22+
23+
24+
Installation
25+
============
26+
27+
Ninja JWT can be installed with pip:
28+
29+
pip install django-ninja-jwt
30+
31+
Also, you need to register `NinjaJWTDefaultController` controller to you Django-Ninja api.
32+
The `NinjaJWTDefaultController` comes with three routes `obtain_token`, `refresh_token` and `verify_token`
33+
34+
``` {.sourceCode .python}
35+
from ninja_jwt.controller import NinjaJWTDefaultController
36+
from ninja_extra import NinjaExtraAPI
37+
38+
api = NinjaExtraAPI()
39+
api.register_controller(NinjaJWTDefaultController)
40+
41+
```
42+
43+
The `NinjaJWTDefaultController` comes with three routes `obtain_token`, `refresh_token` and `verify_token`.
44+
It is a combination of two subclass `TokenVerificationController` and `TokenObtainPairController`.
45+
If you wish to customize these routes, you can inherit from these controllers and change its implementation
46+
47+
``` {.sourceCode .python}
48+
from ninja_jwt.controller import TokenObtainPairController, router
49+
50+
@router('token', tags=['Auth']
51+
class MyCustomController(TokenObtainPairController):
52+
"""obtain_token and refresh_token only"
53+
...
54+
api.register_controller(MyCustomController)
55+
```
56+
57+
If you wish to use localizations/translations, simply add `ninja_jwt` to
58+
`INSTALLED_APPS`.
59+
60+
``` {.sourceCode .python}
61+
INSTALLED_APPS = [
62+
...
63+
'ninja_jwt',
64+
...
65+
]
66+
```
67+
68+
Usage
69+
=====
70+
71+
To verify that Ninja JWT is working, you can use curl to issue a couple
72+
of test requests:
73+
74+
``` {.sourceCode .bash}
75+
curl \
76+
-X POST \
77+
-H "Content-Type: application/json" \
78+
-d '{"username": "davidattenborough", "password": "boatymcboatface"}' \
79+
http://localhost:8000/api/token/pair
80+
81+
...
82+
{
83+
"access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNDU2LCJqdGkiOiJmZDJmOWQ1ZTFhN2M0MmU4OTQ5MzVlMzYyYmNhOGJjYSJ9.NHlztMGER7UADHZJlxNG0WSi22a2KaYSfd1S-AuT7lU",
84+
"refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImNvbGRfc3R1ZmYiOiLimIMiLCJleHAiOjIzNDU2NywianRpIjoiZGUxMmY0ZTY3MDY4NDI3ODg5ZjE1YWMyNzcwZGEwNTEifQ.aEoAYkSJjoWH1boshQAaTkf8G3yn0kapko6HFRt7Rh4"
85+
}
86+
```
87+
88+
You can use the returned access token to prove authentication for a
89+
protected view:
90+
91+
``` {.sourceCode .bash}
92+
curl \
93+
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNDU2LCJqdGkiOiJmZDJmOWQ1ZTFhN2M0MmU4OTQ5MzVlMzYyYmNhOGJjYSJ9.NHlztMGER7UADHZJlxNG0WSi22a2KaYSfd1S-AuT7lU" \
94+
http://localhost:8000/api/some-protected-view/
95+
```
96+
97+
When this short-lived access token expires, you can use the longer-lived
98+
refresh token to obtain another access token:
99+
100+
``` {.sourceCode .bash}
101+
curl \
102+
-X POST \
103+
-H "Content-Type: application/json" \
104+
-d '{"refresh":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImNvbGRfc3R1ZmYiOiLimIMiLCJleHAiOjIzNDU2NywianRpIjoiZGUxMmY0ZTY3MDY4NDI3ODg5ZjE1YWMyNzcwZGEwNTEifQ.aEoAYkSJjoWH1boshQAaTkf8G3yn0kapko6HFRt7Rh4"}' \
105+
http://localhost:8000/api/token/refresh/
106+
107+
...
108+
{"access":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX3BrIjoxLCJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiY29sZF9zdHVmZiI6IuKYgyIsImV4cCI6MTIzNTY3LCJqdGkiOiJjNzE4ZTVkNjgzZWQ0NTQyYTU0NWJkM2VmMGI0ZGQ0ZSJ9.ekxRxgb9OKmHkfy-zs1Ro_xs1eMLXiR17dIDBVxeT-w"}
109+
```

docs/docs/customizing_token_claims.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
If you wish to customize the claims contained in web tokens which are
3-
generated by the `SimpleJWTDefaultController` and `SimpleJWTSlidingController`
3+
generated by the `NinjaJWTDefaultController` and `NinjaJWTSlidingController`
44
views, create a subclass for the desired controller as well as a subclass for
55
its corresponding serializer. Here\'s an example :
66

docs/docs/getting_started.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77
- Django-Ninja-Extra >= 0.11.0
88

99
These are the officially supported python and package versions. Other
10-
versions will probably work. You\'re free to modify the tox config and
10+
versions will probably work. You 're free to modify the tox config and
1111
see what is possible.
1212

1313
Installation
1414
============
1515

1616
Ninja JWT can be installed with pip:
1717

18-
pip install ninja-jwt
18+
pip install django-ninja-jwt
1919

20-
Also, you need to register `SimpleJWTDefaultController` controller to you Django-Ninja api.
21-
The `SimpleJWTDefaultController` comes with three routes `obtain_token`, `refresh_token` and `verify_token`
20+
Also, you need to register `NinjaJWTDefaultController` controller to you Django-Ninja api.
21+
The `NinjaJWTDefaultController` comes with three routes `obtain_token`, `refresh_token` and `verify_token`
2222

2323
``` {.sourceCode .python}
24-
from ninja_jwt.controller import SimpleJWTDefaultController
24+
from ninja_jwt.controller import NinjaJWTDefaultController
2525
from ninja_extra import NinjaExtraAPI
2626
2727
api = NinjaExtraAPI()
28-
api.register_controller(SimpleJWTDefaultController)
28+
api.register_controller(NinjaJWTDefaultController)
2929
3030
```
3131

32-
The `SimpleJWTDefaultController` comes with three routes `obtain_token`, `refresh_token` and `verify_token`.
32+
The `NinjaJWTDefaultController` comes with three routes `obtain_token`, `refresh_token` and `verify_token`.
3333
It is a combination of two subclass `TokenVerificationController` and `TokenObtainPairController`.
3434
If you wish to customize these routes, you can inherit from these controllers and change its implementation
3535

docs/docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
![Test](https://github.com/eadwinCode/django-ninja-jwt/workflows/Test/badge.svg)
33
[![PyPI version](https://badge.fury.io/py/django-ninja-jwt.svg)](https://badge.fury.io/py/django-ninja-jwt)
4-
[![PyPI version](https://img.shields.io/pypi/v/djangorestframework-simplejwt.svg)](https://pypi.python.org/pypi/django-ninja-jwt)
4+
[![PyPI version](https://img.shields.io/pypi/v/django-ninja-jwt.svg)](https://pypi.python.org/pypi/django-ninja-jwt)
55
[![PyPI version](https://img.shields.io/pypi/pyversions/django-ninja-jwt.svg)](https://pypi.python.org/pypi/django-ninja-jwt)
66
[![PyPI version](https://img.shields.io/pypi/djversions/django-ninja-jwt.svg)](https://pypi.python.org/pypi/django-ninja-jwt)
77

docs/docs/settings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ from datetime import timedelta
99
from django.conf import settings
1010
...
1111
12-
SIMPLE_JWT = {
12+
NINJA_JWT = {
1313
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
1414
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
1515
'ROTATE_REFRESH_TOKENS': False,

docs/docs/token_types.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ setting to `('ninja_jwt.tokens.SlidingToken',)`. (Alternatively, the
3737
module if you want to allow both token types to be used for
3838
authentication.)
3939

40-
Also, register `SimpleJWTSlidingController` to the `api`:
40+
Also, register `NinjaJWTSlidingController` to the `api`:
4141
``` {.sourceCode .python}
42-
from ninja_jwt.controller import SimpleJWTSlidingController
42+
from ninja_jwt.controller import NinjaJWTSlidingController
4343
from ninja_extra import NinjaExtraAPI
4444
4545
api = NinjaExtraAPI()
46-
api.register_controller(SimpleJWTSlidingController)
46+
api.register_controller(NinjaJWTSlidingController)
4747
4848
```
4949

ninja_jwt/controller.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,20 @@ def refresh_token(self, refresh_token: schema.TokenRefreshSlidingSchema):
6060

6161

6262
@router("/token", permissions=[AllowAny], tags=["token"])
63-
class SimpleJWTDefaultController(
63+
class NinjaJWTDefaultController(
6464
TokenVerificationController, TokenObtainPairController
6565
):
66-
"""SimpleJWT Default controller for obtaining and refreshing tokens"""
66+
"""NinjaJWT Default controller for obtaining and refreshing tokens"""
6767

6868
auto_import = False
6969

7070

7171
@router("/token", permissions=[AllowAny], tags=["token"])
72-
class SimpleJWTSlidingController(
72+
class NinjaJWTSlidingController(
7373
TokenVerificationController, TokenObtainSlidingController
7474
):
7575
"""
76-
SimpleJWT Sliding controller for obtaining and refreshing tokens
76+
NinjaJWT Sliding controller for obtaining and refreshing tokens
7777
Add 'ninja_jwt.tokens.SlidingToken' in AUTH_TOKEN_CLASSES in Settings
7878
"""
7979

ninja_jwt/settings.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
from pydantic import AnyUrl, Field, root_validator
99

1010

11-
class SimpleJWTUserDefinedSettings:
11+
class NinjaJWTUserDefinedSettingsMapper:
1212
def __init__(self, data: dict) -> None:
1313
self.__dict__ = data
1414

1515

16-
SimpleJWT_SETTINGS_DEFAULTS = dict(
16+
NinjaJWT_SETTINGS_DEFAULTS = dict(
1717
USER_AUTHENTICATION_RULE="ninja_jwt.authentication.default_user_authentication_rule",
1818
AUTH_TOKEN_CLASSES=["ninja_jwt.tokens.AccessToken"],
1919
TOKEN_USER_CLASS="ninja_jwt.models.TokenUser",
2020
)
2121

22-
USER_SETTINGS = SimpleJWTUserDefinedSettings(
23-
getattr(settings, "SIMPLE_JWT", SimpleJWT_SETTINGS_DEFAULTS)
22+
USER_SETTINGS = NinjaJWTUserDefinedSettingsMapper(
23+
getattr(settings, "NINJA_JWT", NinjaJWT_SETTINGS_DEFAULTS)
2424
)
2525

2626

27-
class SimpleJWTSettings(Schema):
27+
class NinjaJWTSettings(Schema):
2828
class Config:
2929
orm_mode = True
3030
validate_assignment = True
@@ -61,8 +61,8 @@ class Config:
6161
SLIDING_TOKEN_REFRESH_LIFETIME: timedelta = Field(timedelta(minutes=5))
6262

6363
@root_validator
64-
def validate_settings(cls, values):
65-
for item in SimpleJWT_SETTINGS_DEFAULTS.keys():
64+
def validate_ninja_jwt_settings(cls, values):
65+
for item in NinjaJWT_SETTINGS_DEFAULTS.keys():
6666
if isinstance(values[item], (tuple, list)) and isinstance(
6767
values[item][0], str
6868
):
@@ -73,16 +73,16 @@ def validate_settings(cls, values):
7373

7474

7575
# convert to lazy object
76-
api_settings = SimpleJWTSettings.from_orm(USER_SETTINGS)
76+
api_settings = NinjaJWTSettings.from_orm(USER_SETTINGS)
7777

7878

7979
def reload_api_settings(*args: Any, **kwargs: Any) -> None:
8080
global api_settings
8181

8282
setting, value = kwargs["setting"], kwargs["value"]
8383

84-
if setting == "SIMPLE_JWT":
85-
api_settings = SimpleJWTSettings.from_orm(SimpleJWTUserDefinedSettings(value))
84+
if setting == "NINJA_JWT":
85+
api_settings = NinjaJWTSettings.from_orm(NinjaJWTUserDefinedSettingsMapper(value))
8686

8787

8888
setting_changed.connect(reload_api_settings)

tests/test_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ninja_jwt.settings import api_settings
88
from ninja_jwt.tokens import AccessToken
99

10-
from .utils import APIViewTestCase, override_api_settings
10+
from .utils import APIViewTestCase
1111

1212
User = get_user_model()
1313

0 commit comments

Comments
 (0)