Skip to content

Commit 21fc49c

Browse files
committed
seperated experiment token from actual token authentication
1 parent 9ea76aa commit 21fc49c

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

docs/docs/experimental_features.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
subtitle: JWTTokenUserAuthentication backend
2-
title: Experimental features
3-
---
41

5-
The `JWTTokenUserAuthentication` backend\'s `authenticate` method does
2+
The `JWTTokenUserAuth` backend\'s `authenticate` method does
63
not perform a database lookup to obtain a user instance. Instead, it
74
returns a `ninja_jwt.models.TokenUser` instance which acts as a
85
stateless user object backed only by a validated token instead of a
96
record in a database. This can facilitate developing single sign-on
107
functionality between separately hosted Django apps which all share the
118
same token secret key. To use this feature, add the
12-
`ninja_jwt.authentication.JWTTokenUserAuthentication` backend (instead
13-
of the default `JWTAuthentication` backend) to the Django REST
14-
Framework\'s `DEFAULT_AUTHENTICATION_CLASSES` config setting:
9+
`ninja_jwt.authentication.JWTTokenUserAuth` backend (instead
10+
of the default `JWTAuth` backend) to the Django Ninja Extra route definition
1511

1612
```python
17-
REST_FRAMEWORK = {
18-
...
19-
'DEFAULT_AUTHENTICATION_CLASSES': (
20-
...
21-
'ninja_jwt.authentication.JWTTokenUserAuthentication',
22-
)
23-
...
24-
}
13+
from ninja_extra import APIController, router, route
14+
from ninja_jwt.authentication import JWTTokenUserAuth
15+
16+
@router('')
17+
class MyController(APIController):
18+
@route.get('/some-endpoint', auth=JWTTokenUserAuth())
19+
def some_endpoint(self):
20+
pass
21+
2522
```

docs/docs/getting_started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ If you wish to customize these routes, you can inherit from these controllers an
3636
```python
3737
from ninja_jwt.controller import TokenObtainPairController, router
3838

39-
@router('token', tags=['Auth']
39+
@router('token', tags=['Auth'])
4040
class MyCustomController(TokenObtainPairController):
4141
"""obtain_token and refresh_token only"
4242
...

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@ nav:
4242
- Token Types: token_types.md
4343
- Blacklist App: blacklist_app.md
4444
- Development and Contributing: development_and_contributing.md
45+
- Experimental Feature: development_and_contributing.md
4546
#- ninja_jwt package: index.md

ninja_jwt/authentication.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ class JWTAuth(JWTBaseAuthentication, HttpBearer):
7373
def authenticate(self, request: HttpRequest, token: str) -> Any:
7474
return self.jwt_authenticate(request, token)
7575

76+
77+
class JWTTokenUserAuth(JWTBaseAuthentication, HttpBearer):
78+
"""
79+
Experimental features
80+
JWTTokenUserAuth backend
81+
"""
82+
83+
def authenticate(self, request: HttpRequest, token: str) -> Any:
84+
return self.jwt_authenticate(request, token)
85+
7686
def get_user(self, validated_token: Any) -> Type[AbstractUser]:
7787
"""
7888
Returns a stateless user object which is backed by the given validated

tests/test_authentication.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
AuthToken = api_settings.AUTH_TOKEN_CLASSES[0]
1414

1515

16-
class TestJWTBaseAuthentication:
16+
class TestJWTAuth:
1717
@pytest.fixture(autouse=True)
1818
def setUp(self):
19-
self.backend = authentication.JWTBaseAuthentication()
19+
self.backend = authentication.JWTAuth()
2020

2121
@pytest.mark.django_db
2222
def test_get_validated_token(self, monkeypatch):
@@ -93,10 +93,10 @@ def test_get_user(self):
9393
assert self.backend.get_user(payload).id == u.id
9494

9595

96-
class TestJWTAuth:
96+
class TestJWTTokenUserAuth:
9797
@pytest.fixture(autouse=True)
9898
def setUp(self):
99-
self.backend = authentication.JWTAuth()
99+
self.backend = authentication.JWTTokenUserAuth()
100100

101101
@pytest.mark.django_db
102102
def test_get_user(self):

0 commit comments

Comments
 (0)