Skip to content

Commit 129890a

Browse files
authored
Fix error in throttling when request.user is None (#8370)
Check to see if request.user is set before proceeding with further authentication checks.
1 parent 2051a79 commit 129890a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

rest_framework/throttling.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class AnonRateThrottle(SimpleRateThrottle):
171171
scope = 'anon'
172172

173173
def get_cache_key(self, request, view):
174-
if request.user.is_authenticated:
174+
if request.user and request.user.is_authenticated:
175175
return None # Only throttle unauthenticated requests.
176176

177177
return self.cache_format % {
@@ -191,7 +191,7 @@ class UserRateThrottle(SimpleRateThrottle):
191191
scope = 'user'
192192

193193
def get_cache_key(self, request, view):
194-
if request.user.is_authenticated:
194+
if request.user and request.user.is_authenticated:
195195
ident = request.user.pk
196196
else:
197197
ident = self.get_ident(request)
@@ -239,7 +239,7 @@ def get_cache_key(self, request, view):
239239
Otherwise generate the unique cache key by concatenating the user id
240240
with the `.throttle_scope` property of the view.
241241
"""
242-
if request.user.is_authenticated:
242+
if request.user and request.user.is_authenticated:
243243
ident = request.user.pk
244244
else:
245245
ident = self.get_ident(request)

0 commit comments

Comments
 (0)