summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Godwin2022-12-05 20:02:35 -0700
committerAndrew Godwin2022-12-05 20:02:35 -0700
commit9fe2e6676c32b93023ae301be587a8ab0d93120f (patch)
treecc1e144ed61785431a98019d6e62f283645bbd40
parente2d28a4be08ccc629def8c85327b657b0bf36f90 (diff)
downloadtakahe-9fe2e6676c32b93023ae301be587a8ab0d93120f.tar.gz
takahe-9fe2e6676c32b93023ae301be587a8ab0d93120f.tar.bz2
takahe-9fe2e6676c32b93023ae301be587a8ab0d93120f.zip
Add a middleware to detect LD Accept headers
-rw-r--r--activities/views/posts.py7
-rw-r--r--core/middleware.py19
-rw-r--r--takahe/settings.py1
-rw-r--r--users/views/identity.py7
4 files changed, 22 insertions, 12 deletions
diff --git a/activities/views/posts.py b/activities/views/posts.py
index 1962274..adc732c 100644
--- a/activities/views/posts.py
+++ b/activities/views/posts.py
@@ -21,12 +21,7 @@ class Individual(TemplateView):
self.identity = by_handle_or_404(self.request, handle, local=False)
self.post_obj = get_object_or_404(self.identity.posts, pk=post_id)
# If they're coming in looking for JSON, they want the actor
- accept = request.headers.get("accept", "text/html").lower()
- if (
- "application/json" in accept
- or "application/ld" in accept
- or "application/activity" in accept
- ):
+ if request.ap_json:
# Return post JSON
return self.serve_object()
else:
diff --git a/core/middleware.py b/core/middleware.py
index 274f672..de35c4c 100644
--- a/core/middleware.py
+++ b/core/middleware.py
@@ -6,6 +6,25 @@ from core import sentry
from core.models import Config
+class AcceptMiddleware:
+ """
+ Detects any Accept headers signifying a fellow AP server is trying to get JSON.
+ """
+
+ def __init__(self, get_response):
+ self.get_response = get_response
+
+ def __call__(self, request):
+ accept = request.headers.get("accept", "text/html").lower()
+ request.ap_json = (
+ "application/json" in accept
+ or "application/ld" in accept
+ or "application/activity" in accept
+ )
+ response = self.get_response(request)
+ return response
+
+
class ConfigLoadingMiddleware:
"""
Caches the system config every request
diff --git a/takahe/settings.py b/takahe/settings.py
index d46ec73..f263153 100644
--- a/takahe/settings.py
+++ b/takahe/settings.py
@@ -178,6 +178,7 @@ MIDDLEWARE = [
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_htmx.middleware.HtmxMiddleware",
+ "core.middleware.AcceptMiddleware",
"core.middleware.ConfigLoadingMiddleware",
"users.middleware.IdentityMiddleware",
]
diff --git a/users/views/identity.py b/users/views/identity.py
index 27d2e4e..4996ad7 100644
--- a/users/views/identity.py
+++ b/users/views/identity.py
@@ -42,12 +42,7 @@ class ViewIdentity(ListView):
):
self.identity.transition_perform(IdentityStates.outdated)
# If they're coming in looking for JSON, they want the actor
- accept = request.headers.get("accept", "text/html").lower()
- if (
- "application/json" in accept
- or "application/ld" in accept
- or "application/activity" in accept
- ):
+ if request.ap_json:
# Return actor info
return self.serve_actor(self.identity)
else: