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