Skip to content

Commit 92d35e5

Browse files
committed
add basic internal token endpoint, verification and tests
1 parent 30b9831 commit 92d35e5

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

intbot/core/endpoints/webhooks.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1+
import hmac
12
import json
3+
4+
from core.models import Webhook
5+
from django.conf import settings
26
from django.http.response import HttpResponseNotAllowed, JsonResponse
37
from django.views.decorators.csrf import csrf_exempt
48

5-
from core.models import Webhook
69

710
@csrf_exempt
811
def internal_webhook_endpoint(request):
912
if request.method == "POST":
10-
print(request.body)
13+
try:
14+
verify_internal_webhook(request)
15+
except ValueError as e:
16+
return JsonResponse({"status": "bad", "message": str(e)}, status=403)
17+
1118
wh = Webhook.objects.create(
1219
source="internal",
1320
content=json.loads(request.body),
@@ -16,3 +23,15 @@ def internal_webhook_endpoint(request):
1623
return JsonResponse({"status": "created", "guid": wh.uuid})
1724

1825
return HttpResponseNotAllowed("Only POST")
26+
27+
28+
def verify_internal_webhook(request):
29+
"""raise ValueError if incorrect token"""
30+
31+
if not "Authorization" in request.headers:
32+
raise ValueError("Authorization token is missing")
33+
34+
token = request.headers['Authorization']
35+
36+
if not hmac.compare_digest(settings.WEBHOOK_INTERNAL_TOKEN, token):
37+
raise ValueError("Token doesn't match")

intbot/intbot/settings.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@
138138
}
139139
}
140140

141+
WEBHOOK_INTERNAL_TOKEN = "test-random-token"
142+
141143

142144
else:
143145
raise ValueError(f"Unsupported DJANGO_ENV `{DJANGO_ENV}`")

intbot/tests/test_webhooks.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
11
import pytest
2+
from django.conf import settings
23
from core.models import Webhook
34

5+
@pytest.mark.django_db
6+
def test_internal_wh_endpoint_checks_authorization_token(client):
7+
webhook_body = {
8+
"event": "test1",
9+
"content": {
10+
"random": "content",
11+
},
12+
}
13+
14+
response = client.post(
15+
"/webhook/internal/",
16+
json=webhook_body,
17+
content_type="application/json",
18+
)
19+
20+
assert response.status_code == 403
21+
assert response["Content-Type"] == "application/json"
22+
assert response.json()["status"] == "bad"
23+
assert response.json()["message"] == "Authorization token is missing"
24+
25+
@pytest.mark.django_db
26+
def test_internal_wh_endpoint_fails_with_bad_token(client):
27+
webhook_body = {
28+
"event": "test1",
29+
"content": {
30+
"random": "content",
31+
},
32+
}
33+
34+
response = client.post(
35+
"/webhook/internal/",
36+
json=webhook_body,
37+
content_type="application/json",
38+
HTTP_AUTHORIZATION="random-incorrect-token",
39+
)
40+
41+
assert response.status_code == 403
42+
assert response["Content-Type"] == "application/json"
43+
assert response.json()["status"] == "bad"
44+
assert response.json()["message"] == "Token doesn't match"
445

546
@pytest.mark.django_db
6-
def test_database_sanity_check(client):
47+
def test_internal_wh_endpoint_works_with_correct_token(client):
748
webhook_body = {
849
"event": "test1",
950
"content": {
@@ -15,6 +56,7 @@ def test_database_sanity_check(client):
1556
"/webhook/internal/",
1657
json=webhook_body,
1758
content_type="application/json",
59+
HTTP_AUTHORIZATION=settings.WEBHOOK_INTERNAL_TOKEN,
1860
)
1961

2062
wh = Webhook.objects.get()

0 commit comments

Comments
 (0)