Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the JWTTokenUserAuthentication from the Experimental Features #546 #547

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Contents
creating_tokens_manually
token_types
blacklist_app
experimental_features
stateless_user_authentication
development_and_contributing
drf_yasg_integration
rest_framework_simplejwt
Expand Down
2 changes: 1 addition & 1 deletion docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ store such a value.
--------------------

A stateless user object which is backed by a validated token. Used only for
the experimental JWTTokenUserAuthentication authentication backend. The value
the JWTStatelessUserAuthentication authentication backend. The value
is a dotted path to your subclass of ``rest_framework_simplejwt.models.TokenUser``,
which also is the default.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
.. _experimental_features:
.. _stateless_user_authentication:

Experimental features
Stateless User Authentication
=====================

JWTTokenUserAuthentication backend
JWTStatelessUserAuthentication backend
----------------------------------

The ``JWTTokenUserAuthentication`` backend's ``authenticate`` method does not
The ``JWTStatelessUserAuthentication`` backend's ``authenticate`` method does not
perform a database lookup to obtain a user instance. Instead, it returns a
``rest_framework_simplejwt.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
``rest_framework_simplejwt.authentication.JWTTokenUserAuthentication`` backend
``rest_framework_simplejwt.authentication.JWTStatelessUserAuthentication`` backend
(instead of the default ``JWTAuthentication`` backend) to the Django REST
Framework's ``DEFAULT_AUTHENTICATION_CLASSES`` config setting:

Expand All @@ -23,7 +23,7 @@ Framework's ``DEFAULT_AUTHENTICATION_CLASSES`` config setting:
...
'DEFAULT_AUTHENTICATION_CLASSES': (
...
'rest_framework_simplejwt.authentication.JWTTokenUserAuthentication',
'rest_framework_simplejwt.authentication.JWTStatelessUserAuthentication',
)
...
}
10 changes: 9 additions & 1 deletion rest_framework_simplejwt/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,12 @@ def get_user(self, validated_token):
return user


class JWTTokenUserAuthentication(JWTAuthentication):
class JWTStatelessUserAuthentication(JWTAuthentication):
Andrew-Chen-Wang marked this conversation as resolved.
Show resolved Hide resolved
"""
An authentication plugin that authenticates requests through a JSON web
token provided in a request header without performing a database lookup to obtain a user instance.
"""

def get_user(self, validated_token):
"""
Returns a stateless user object which is backed by the given validated
Expand All @@ -140,6 +145,9 @@ def get_user(self, validated_token):
return api_settings.TOKEN_USER_CLASS(validated_token)


JWTTokenUserAuthentication = JWTStatelessUserAuthentication


def default_user_authentication_rule(user):
# Prior to Django 1.10, inactive users could be authenticated with the
# default `ModelBackend`. As of Django 1.10, the `ModelBackend`
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_simplejwt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
class TokenUser:
"""
A dummy user class modeled after django.contrib.auth.models.AnonymousUser.
Used in conjunction with the `JWTTokenUserAuthentication` backend to
Used in conjunction with the `JWTStatelessUserAuthentication` backend to
implement single sign-on functionality across services which share the same
secret key. `JWTTokenUserAuthentication` will return an instance of this
secret key. `JWTStatelessUserAuthentication` will return an instance of this
class instead of a `User` model instance. Instances of this class act as
stateless user objects which are backed by validated tokens.
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ def test_get_user(self):
self.assertEqual(self.backend.get_user(payload).id, u.id)


class TestJWTTokenUserAuthentication(TestCase):
class TestJWTStatelessUserAuthentication(TestCase):
def setUp(self):
self.backend = authentication.JWTTokenUserAuthentication()
self.backend = authentication.JWTStatelessUserAuthentication()

def test_get_user(self):
payload = {"some_other_id": "foo"}
Expand Down