Skip to content

Commit

Permalink
seperated experiment token from actual token authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
eadwinCode committed Nov 19, 2021
1 parent 9ea76aa commit 21fc49c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
27 changes: 12 additions & 15 deletions docs/docs/experimental_features.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
subtitle: JWTTokenUserAuthentication backend
title: Experimental features
---

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

```python
REST_FRAMEWORK = {
...
'DEFAULT_AUTHENTICATION_CLASSES': (
...
'ninja_jwt.authentication.JWTTokenUserAuthentication',
)
...
}
from ninja_extra import APIController, router, route
from ninja_jwt.authentication import JWTTokenUserAuth

@router('')
class MyController(APIController):
@route.get('/some-endpoint', auth=JWTTokenUserAuth())
def some_endpoint(self):
pass

```
2 changes: 1 addition & 1 deletion docs/docs/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ If you wish to customize these routes, you can inherit from these controllers an
```python
from ninja_jwt.controller import TokenObtainPairController, router

@router('token', tags=['Auth']
@router('token', tags=['Auth'])
class MyCustomController(TokenObtainPairController):
"""obtain_token and refresh_token only"
...
Expand Down
1 change: 1 addition & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ nav:
- Token Types: token_types.md
- Blacklist App: blacklist_app.md
- Development and Contributing: development_and_contributing.md
- Experimental Feature: development_and_contributing.md
#- ninja_jwt package: index.md
10 changes: 10 additions & 0 deletions ninja_jwt/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ class JWTAuth(JWTBaseAuthentication, HttpBearer):
def authenticate(self, request: HttpRequest, token: str) -> Any:
return self.jwt_authenticate(request, token)


class JWTTokenUserAuth(JWTBaseAuthentication, HttpBearer):
"""
Experimental features
JWTTokenUserAuth backend
"""

def authenticate(self, request: HttpRequest, token: str) -> Any:
return self.jwt_authenticate(request, token)

def get_user(self, validated_token: Any) -> Type[AbstractUser]:
"""
Returns a stateless user object which is backed by the given validated
Expand Down
8 changes: 4 additions & 4 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
AuthToken = api_settings.AUTH_TOKEN_CLASSES[0]


class TestJWTBaseAuthentication:
class TestJWTAuth:
@pytest.fixture(autouse=True)
def setUp(self):
self.backend = authentication.JWTBaseAuthentication()
self.backend = authentication.JWTAuth()

@pytest.mark.django_db
def test_get_validated_token(self, monkeypatch):
Expand Down Expand Up @@ -93,10 +93,10 @@ def test_get_user(self):
assert self.backend.get_user(payload).id == u.id


class TestJWTAuth:
class TestJWTTokenUserAuth:
@pytest.fixture(autouse=True)
def setUp(self):
self.backend = authentication.JWTAuth()
self.backend = authentication.JWTTokenUserAuth()

@pytest.mark.django_db
def test_get_user(self):
Expand Down

0 comments on commit 21fc49c

Please sign in to comment.