Browse Source

Fix /api/v1/badges/ to handle requests with missing X-Api-Key header

pull/563/head
Pēteris Caune 3 years ago
parent
commit
51f996ab4b
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
2 changed files with 10 additions and 2 deletions
  1. +6
    -2
      hc/api/decorators.py
  2. +4
    -0
      hc/api/tests/test_get_badges.py

+ 6
- 2
hc/api/decorators.py View File

@ -16,8 +16,10 @@ def authorize(f):
def wrapper(request, *args, **kwds):
if "HTTP_X_API_KEY" in request.META:
api_key = request.META["HTTP_X_API_KEY"]
else:
elif hasattr(request, "json"):
api_key = str(request.json.get("api_key", ""))
else:
api_key = ""
if len(api_key) != 32:
return error("missing api key", 401)
@ -38,8 +40,10 @@ def authorize_read(f):
def wrapper(request, *args, **kwds):
if "HTTP_X_API_KEY" in request.META:
api_key = request.META["HTTP_X_API_KEY"]
else:
elif hasattr(request, "json"):
api_key = str(request.json.get("api_key", ""))
else:
api_key = ""
if len(api_key) != 32:
return error("missing api key", 401)


+ 4
- 0
hc/api/tests/test_get_badges.py View File

@ -41,3 +41,7 @@ class GetBadgesTestCase(BaseTestCase):
def test_it_rejects_post(self):
r = self.client.post(self.url, HTTP_X_API_KEY="X" * 32)
self.assertEqual(r.status_code, 405)
def test_it_handles_missing_api_key(self):
r = self.client.get(self.url)
self.assertContains(r, "missing api key", status_code=401)

Loading…
Cancel
Save