13
13
jwt_get_user_id_from_payload = api_settings .JWT_PAYLOAD_GET_USER_ID_HANDLER
14
14
15
15
16
- class JSONWebTokenAuthentication (BaseAuthentication ):
16
+ class JSONWebTokenAuthenticationBase (BaseAuthentication ):
17
17
"""
18
18
Token based authentication using the JSON Web Token standard.
19
-
20
- Clients should authenticate by passing the token key in the "Authorization"
21
- HTTP header, prepended with the string specified in the setting
22
- `JWT_AUTH_HEADER_PREFIX`. For example:
23
-
24
- Authorization: JWT eyJhbGciOiAiSFMyNTYiLCAidHlwIj
25
19
"""
26
- www_authenticate_realm = 'api'
27
20
28
21
def authenticate (self , request ):
29
22
"""
30
23
Returns a two-tuple of `User` and token if a valid signature has been
31
24
supplied using JWT-based authentication. Otherwise returns `None`.
32
25
"""
33
- auth = get_authorization_header (request ).split ()
34
- auth_header_prefix = api_settings .JWT_AUTH_HEADER_PREFIX .lower ()
35
-
36
- if not auth or smart_text (auth [0 ].lower ()) != auth_header_prefix :
37
- return None
38
-
39
- if len (auth ) == 1 :
40
- msg = _ ('Invalid Authorization header. No credentials provided.' )
41
- raise exceptions .AuthenticationFailed (msg )
42
- elif len (auth ) > 2 :
43
- msg = _ ('Invalid Authorization header. Credentials string '
44
- 'should not contain spaces.' )
45
- raise exceptions .AuthenticationFailed (msg )
26
+ jwt_value = self .get_jwt_value (request )
46
27
47
28
try :
48
- payload = jwt_decode_handler (auth [ 1 ] )
29
+ payload = jwt_decode_handler (jwt_value )
49
30
except jwt .ExpiredSignature :
50
31
msg = _ ('Signature has expired.' )
51
32
raise exceptions .AuthenticationFailed (msg )
@@ -55,28 +36,56 @@ def authenticate(self, request):
55
36
56
37
user = self .authenticate_credentials (payload )
57
38
58
- return (user , auth [ 1 ] )
39
+ return (user , jwt_value )
59
40
60
41
def authenticate_credentials (self , payload ):
61
42
"""
62
43
Returns an active user that matches the payload's user id and email.
63
44
"""
64
45
User = utils .get_user_model ()
65
46
66
- try :
67
- user_id = jwt_get_user_id_from_payload (payload )
47
+ user_id = jwt_get_user_id_from_payload (payload )
68
48
69
- if user_id is not None :
49
+ if user_id is not None :
50
+ try :
70
51
user = User .objects .get (pk = user_id , is_active = True )
71
- else :
72
- msg = _ ('Invalid payload .' )
52
+ except User . DoesNotExist :
53
+ msg = _ ('Invalid signature .' )
73
54
raise exceptions .AuthenticationFailed (msg )
74
- except User . DoesNotExist :
75
- msg = _ ('Invalid signature .' )
55
+ else :
56
+ msg = _ ('Invalid payload .' )
76
57
raise exceptions .AuthenticationFailed (msg )
77
58
78
59
return user
79
60
61
+
62
+ class JSONWebTokenAuthentication (JSONWebTokenAuthenticationBase ):
63
+ """
64
+ Clients should authenticate by passing the token key in the "Authorization"
65
+ HTTP header, prepended with the string specified in the setting
66
+ `JWT_AUTH_HEADER_PREFIX`. For example:
67
+
68
+ Authorization: JWT eyJhbGciOiAiSFMyNTYiLCAidHlwIj
69
+ """
70
+ www_authenticate_realm = 'api'
71
+
72
+ def get_jwt_value (self , request ):
73
+ auth = get_authorization_header (request ).split ()
74
+ auth_header_prefix = api_settings .JWT_AUTH_HEADER_PREFIX .lower ()
75
+
76
+ if not auth or smart_text (auth [0 ].lower ()) != auth_header_prefix :
77
+ return None
78
+
79
+ if len (auth ) == 1 :
80
+ msg = _ ('Invalid Authorization header. No credentials provided.' )
81
+ raise exceptions .AuthenticationFailed (msg )
82
+ elif len (auth ) > 2 :
83
+ msg = _ ('Invalid Authorization header. Credentials string '
84
+ 'should not contain spaces.' )
85
+ raise exceptions .AuthenticationFailed (msg )
86
+
87
+ return auth [1 ]
88
+
80
89
def authenticate_header (self , request ):
81
90
"""
82
91
Return a string to be used as the value of the `WWW-Authenticate`
0 commit comments