Skip to content

Commit 858c6d9

Browse files
committed
Split the AUTHENTICATE_PAYLOAD function away
1 parent e436562 commit 858c6d9

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

rest_framework_sso/authentication.py

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from __future__ import absolute_import, unicode_literals
33

44
import jwt
5-
from django.contrib.auth import get_user_model
65
from django.utils.encoding import smart_text
76
from django.utils.translation import ugettext as _
87
from rest_framework import exceptions
@@ -13,6 +12,7 @@
1312
from rest_framework_sso.settings import api_settings
1413

1514
decode_jwt_token = api_settings.DECODE_JWT_TOKEN
15+
authenticate_payload = api_settings.AUTHENTICATE_PAYLOAD
1616

1717

1818
class JWTAuthentication(BaseAuthentication):
@@ -59,28 +59,7 @@ def authenticate(self, request):
5959
return self.authenticate_credentials(payload=payload)
6060

6161
def authenticate_credentials(self, payload):
62-
from rest_framework_sso.models import SessionToken
63-
64-
user_model = get_user_model()
65-
66-
if api_settings.VERIFY_SESSION_TOKEN:
67-
try:
68-
session_token = SessionToken.objects.\
69-
active().\
70-
select_related('user').\
71-
get(pk=payload.get('sid'), user_id=payload.get('uid'))
72-
user = session_token.user
73-
except SessionToken.DoesNotExist:
74-
raise exceptions.AuthenticationFailed(_('Invalid token.'))
75-
else:
76-
try:
77-
user = user_model.objects.get(pk=payload.get('uid'))
78-
except user_model.DoesNotExist:
79-
raise exceptions.AuthenticationFailed(_('Invalid token.'))
80-
81-
if not user.is_active:
82-
raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))
83-
62+
user = authenticate_payload(payload=payload)
8463
return user, payload
8564

8665
def authenticate_header(self, request):

rest_framework_sso/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
'CREATE_AUTHORIZATION_PAYLOAD': 'rest_framework_sso.utils.create_authorization_payload',
1515
'ENCODE_JWT_TOKEN': 'rest_framework_sso.utils.encode_jwt_token',
1616
'DECODE_JWT_TOKEN': 'rest_framework_sso.utils.decode_jwt_token',
17+
'AUTHENTICATE_PAYLOAD': 'rest_framework_sso.utils.authenticate_payload',
1718

1819
'ENCODE_ALGORITHM': 'RS256',
1920
'DECODE_ALGORITHMS': None,
@@ -40,6 +41,7 @@
4041
'CREATE_AUTHORIZATION_PAYLOAD',
4142
'ENCODE_JWT_TOKEN',
4243
'DECODE_JWT_TOKEN',
44+
'AUTHENTICATE_PAYLOAD',
4345
)
4446

4547
api_settings = APISettings(USER_SETTINGS, DEFAULTS, IMPORT_STRINGS)

rest_framework_sso/utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
from datetime import datetime
55

66
import jwt
7+
from django.contrib.auth import get_user_model
78
from django.core.serializers.json import DjangoJSONEncoder
89
from django.utils import six
10+
from django.utils.translation import gettext_lazy as _
911
from jwt.exceptions import MissingRequiredClaimError, InvalidIssuerError
12+
from rest_framework import exceptions
1013

1114
from rest_framework_sso.settings import api_settings
1215

@@ -100,3 +103,29 @@ def decode_jwt_token(token):
100103
audience=api_settings.IDENTITY,
101104
issuer=unverified_issuer,
102105
)
106+
107+
108+
def authenticate_payload(payload):
109+
from rest_framework_sso.models import SessionToken
110+
111+
user_model = get_user_model()
112+
113+
if api_settings.VERIFY_SESSION_TOKEN:
114+
try:
115+
session_token = SessionToken.objects.\
116+
active().\
117+
select_related('user').\
118+
get(pk=payload.get('sid'), user_id=payload.get('uid'))
119+
user = session_token.user
120+
except SessionToken.DoesNotExist:
121+
raise exceptions.AuthenticationFailed(_('Invalid token.'))
122+
else:
123+
try:
124+
user = user_model.objects.get(pk=payload.get('uid'))
125+
except user_model.DoesNotExist:
126+
raise exceptions.AuthenticationFailed(_('Invalid token.'))
127+
128+
if not user.is_active:
129+
raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))
130+
131+
return user

0 commit comments

Comments
 (0)