@@ -150,10 +150,10 @@ def dummy_get_response(request): # pragma: no cover
150
150
151
151
class TokenAuthentication (BaseAuthentication ):
152
152
"""
153
- Simple token based authentication.
153
+ Simple token- based authentication.
154
154
155
155
Clients should authenticate by passing the token key in the "Authorization"
156
- HTTP header, prepended with the string "Token ". For example:
156
+ HTTP header, prepended with the string "Token ". For example:
157
157
158
158
Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
159
159
"""
@@ -167,31 +167,19 @@ def get_model(self):
167
167
from rest_framework .authtoken .models import Token
168
168
return Token
169
169
170
- """
171
- A custom token model may be used, but must have the following properties.
172
-
173
- * key -- The string identifying the token
174
- * user -- The user to which the token belongs
175
- """
176
-
177
170
def authenticate (self , request ):
178
171
auth = get_authorization_header (request ).split ()
179
172
180
- if not auth or auth [0 ].lower () != self .keyword .lower ().encode ():
173
+ if len ( auth ) != 2 or auth [0 ].lower () != self .keyword .lower ().encode ():
181
174
return None
182
-
183
- if len (auth ) == 1 :
184
- msg = _ ('Invalid token header. No credentials provided.' )
185
- raise exceptions .AuthenticationFailed (msg )
186
- elif len (auth ) > 2 :
187
- msg = _ ('Invalid token header. Token string should not contain spaces.' )
188
- raise exceptions .AuthenticationFailed (msg )
189
-
175
+
176
+ token = auth [1 ]
190
177
try :
191
- token = auth [ 1 ] .decode ()
178
+ token = token .decode ()
192
179
except UnicodeError :
193
- msg = _ ('Invalid token header. Token string should not contain invalid characters.' )
194
- raise exceptions .AuthenticationFailed (msg )
180
+ raise exceptions .AuthenticationFailed (
181
+ _ ('Invalid token header. Token string should not contain invalid characters.' )
182
+ )
195
183
196
184
return self .authenticate_credentials (token )
197
185
@@ -205,7 +193,7 @@ def authenticate_credentials(self, key):
205
193
if not token .user .is_active :
206
194
raise exceptions .AuthenticationFailed (_ ('User inactive or deleted.' ))
207
195
208
- return ( token .user , token )
196
+ return token .user , token
209
197
210
198
def authenticate_header (self , request ):
211
199
return self .keyword
0 commit comments