Skip to content

Commit 8a7586b

Browse files
authored
Build API key: don't fetch and validate key twice (#10488)
Instead of relying on the parent method, we just now override the whole method to have access to the key itself.
1 parent 64196ad commit 8a7586b

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

readthedocs/api/v2/permissions.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class HasBuildAPIKey(BaseHasAPIKey):
8585
"""
8686
Custom permission to inject the build API key into the request.
8787
88-
This avoids having to parse the key again on each view.
88+
We completely override the ``has_permission`` method
89+
to avoid having to parse and validate the key again on each view.
8990
The key is injected in the ``request.build_api_key`` attribute
9091
only if it's valid, otherwise it's set to ``None``.
9192
"""
@@ -94,10 +95,18 @@ class HasBuildAPIKey(BaseHasAPIKey):
9495
key_parser = TokenKeyParser()
9596

9697
def has_permission(self, request, view):
97-
build_api_key = None
98-
has_permission = super().has_permission(request, view)
99-
if has_permission:
100-
key = self.get_key(request)
98+
request.build_api_key = None
99+
key = self.get_key(request)
100+
if not key:
101+
return False
102+
103+
try:
101104
build_api_key = self.model.objects.get_from_key(key)
105+
except self.model.DoesNotExist:
106+
return False
107+
108+
if build_api_key.has_expired:
109+
return False
110+
102111
request.build_api_key = build_api_key
103-
return has_permission
112+
return True

0 commit comments

Comments
 (0)