2022-11-12 20:14:21 -08:00
|
|
|
import json
|
2023-10-23 09:33:55 -07:00
|
|
|
import logging
|
2023-10-26 09:01:03 -07:00
|
|
|
from urllib.parse import urldefrag, urlparse
|
2022-11-12 20:14:21 -08:00
|
|
|
|
2022-11-18 07:28:15 -08:00
|
|
|
from django.conf import settings
|
2022-12-18 18:47:35 -08:00
|
|
|
from django.http import Http404, HttpResponse, HttpResponseBadRequest, JsonResponse
|
2022-11-12 20:14:21 -08:00
|
|
|
from django.utils.decorators import method_decorator
|
2022-12-25 13:26:15 -08:00
|
|
|
from django.views.decorators.cache import cache_control
|
2022-11-12 20:14:21 -08:00
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
from django.views.generic import View
|
|
|
|
|
2022-11-18 07:28:15 -08:00
|
|
|
from activities.models import Post
|
2023-05-13 09:01:27 -07:00
|
|
|
from activities.services import TimelineService
|
2022-12-05 09:55:30 -08:00
|
|
|
from core.decorators import cache_page
|
2022-11-12 20:14:21 -08:00
|
|
|
from core.ld import canonicalise
|
2022-11-18 07:28:15 -08:00
|
|
|
from core.models import Config
|
2022-11-12 20:14:21 -08:00
|
|
|
from core.signatures import (
|
|
|
|
HttpSignature,
|
|
|
|
LDSignature,
|
|
|
|
VerificationError,
|
|
|
|
VerificationFormatError,
|
|
|
|
)
|
2022-12-25 13:26:15 -08:00
|
|
|
from core.views import StaticContentView
|
2022-11-18 07:28:15 -08:00
|
|
|
from takahe import __version__
|
2022-11-20 17:29:19 -08:00
|
|
|
from users.models import Identity, InboxMessage, SystemActor
|
2023-10-26 09:01:03 -07:00
|
|
|
from users.models.domain import Domain
|
2022-11-12 20:14:21 -08:00
|
|
|
from users.shortcuts import by_handle_or_404
|
|
|
|
|
|
|
|
|
|
|
|
class HttpResponseUnauthorized(HttpResponse):
|
|
|
|
status_code = 401
|
|
|
|
|
|
|
|
|
|
|
|
class HostMeta(View):
|
|
|
|
"""
|
|
|
|
Returns a canned host-meta response
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return HttpResponse(
|
|
|
|
"""<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
|
|
|
|
<Link rel="lrdd" template="https://%s/.well-known/webfinger?resource={uri}"/>
|
|
|
|
</XRD>"""
|
2022-12-05 13:44:50 -08:00
|
|
|
% request.headers["host"],
|
2022-12-30 09:12:48 -08:00
|
|
|
content_type="application/xrd+xml",
|
2022-11-12 20:14:21 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-11-18 07:28:15 -08:00
|
|
|
class NodeInfo(View):
|
|
|
|
"""
|
|
|
|
Returns the well-known nodeinfo response, pointing to the 2.0 one
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
host = request.META.get("HOST", settings.MAIN_DOMAIN)
|
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"links": [
|
|
|
|
{
|
|
|
|
"rel": "http://nodeinfo.diaspora.software/ns/schema/2.0",
|
|
|
|
"href": f"https://{host}/nodeinfo/2.0/",
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-05 09:55:30 -08:00
|
|
|
@method_decorator(cache_page(), name="dispatch")
|
2022-11-18 07:28:15 -08:00
|
|
|
class NodeInfo2(View):
|
|
|
|
"""
|
|
|
|
Returns the nodeinfo 2.0 response
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
# Fetch some user stats
|
2023-08-11 08:34:25 -07:00
|
|
|
if request.domain:
|
|
|
|
domain_config = Config.load_domain(request.domain)
|
|
|
|
local_identities = Identity.objects.filter(
|
|
|
|
local=True, domain=request.domain
|
|
|
|
).count()
|
|
|
|
local_posts = Post.objects.filter(
|
|
|
|
local=True, author__domain=request.domain
|
|
|
|
).count()
|
|
|
|
metadata = {"nodeName": domain_config.site_name}
|
|
|
|
else:
|
|
|
|
local_identities = Identity.objects.filter(local=True).count()
|
|
|
|
local_posts = Post.objects.filter(local=True).count()
|
|
|
|
metadata = {}
|
2022-11-18 07:28:15 -08:00
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"version": "2.0",
|
|
|
|
"software": {"name": "takahe", "version": __version__},
|
|
|
|
"protocols": ["activitypub"],
|
|
|
|
"services": {"outbound": [], "inbound": []},
|
|
|
|
"usage": {
|
|
|
|
"users": {"total": local_identities},
|
|
|
|
"localPosts": local_posts,
|
|
|
|
},
|
2022-12-21 23:03:21 -08:00
|
|
|
"openRegistrations": Config.system.signup_allowed,
|
2023-08-11 08:34:25 -07:00
|
|
|
"metadata": metadata,
|
2022-11-18 07:28:15 -08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-05 09:55:30 -08:00
|
|
|
@method_decorator(cache_page(), name="dispatch")
|
2022-11-12 20:14:21 -08:00
|
|
|
class Webfinger(View):
|
|
|
|
"""
|
|
|
|
Services webfinger requests
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
resource = request.GET.get("resource")
|
2022-11-21 07:34:02 -08:00
|
|
|
if not resource:
|
|
|
|
return HttpResponseBadRequest("No resource specified")
|
2022-11-12 20:14:21 -08:00
|
|
|
if not resource.startswith("acct:"):
|
2022-11-21 07:34:02 -08:00
|
|
|
return HttpResponseBadRequest("Not an account resource")
|
2022-11-20 17:29:19 -08:00
|
|
|
handle = resource[5:]
|
2022-11-22 20:53:02 -08:00
|
|
|
|
2022-11-20 17:29:19 -08:00
|
|
|
if handle.startswith("__system__@"):
|
|
|
|
# They are trying to webfinger the system actor
|
2022-11-22 20:53:02 -08:00
|
|
|
actor = SystemActor()
|
2022-11-20 17:29:19 -08:00
|
|
|
else:
|
2022-11-22 20:53:02 -08:00
|
|
|
actor = by_handle_or_404(request, handle)
|
|
|
|
|
2022-12-30 09:12:48 -08:00
|
|
|
return JsonResponse(actor.to_webfinger(), content_type="application/jrd+json")
|
2022-11-12 20:14:21 -08:00
|
|
|
|
|
|
|
|
|
|
|
@method_decorator(csrf_exempt, name="dispatch")
|
|
|
|
class Inbox(View):
|
|
|
|
"""
|
|
|
|
AP Inbox endpoint
|
|
|
|
"""
|
|
|
|
|
2022-12-10 12:24:49 -08:00
|
|
|
def post(self, request, handle=None):
|
2022-12-27 19:01:00 -08:00
|
|
|
# Reject bodies that are unfeasibly big
|
|
|
|
if len(request.body) > settings.JSONLD_MAX_SIZE:
|
|
|
|
return HttpResponseBadRequest("Payload size too large")
|
2022-11-12 20:14:21 -08:00
|
|
|
# Load the LD
|
|
|
|
document = canonicalise(json.loads(request.body), include_security=True)
|
2023-11-12 11:09:09 -08:00
|
|
|
document_type = document["type"]
|
|
|
|
document_subtype = None
|
|
|
|
if isinstance(document.get("object"), dict):
|
|
|
|
document_subtype = document["object"].get("type")
|
|
|
|
|
2022-11-12 20:14:21 -08:00
|
|
|
# Find the Identity by the actor on the incoming item
|
|
|
|
# This ensures that the signature used for the headers matches the actor
|
|
|
|
# described in the payload.
|
2023-10-23 09:33:55 -07:00
|
|
|
if "actor" not in document:
|
|
|
|
logging.warning("Inbox error: unspecified actor")
|
|
|
|
return HttpResponseBadRequest("Unspecified actor")
|
|
|
|
|
2022-11-20 13:20:28 -08:00
|
|
|
identity = Identity.by_actor_uri(document["actor"], create=True, transient=True)
|
2022-12-05 21:23:35 -08:00
|
|
|
if (
|
2023-11-12 11:09:09 -08:00
|
|
|
document_type == "Delete"
|
2022-12-05 21:23:35 -08:00
|
|
|
and document["actor"] == document["object"]
|
2023-01-08 22:06:09 -08:00
|
|
|
and identity._state.adding
|
2022-12-05 21:23:35 -08:00
|
|
|
):
|
|
|
|
# We don't have an Identity record for the user. No-op
|
|
|
|
return HttpResponse(status=202)
|
|
|
|
|
2023-10-26 09:01:03 -07:00
|
|
|
# See if it's from a blocked user or domain - without calling
|
|
|
|
# fetch_actor, which would fetch data from potentially bad actor
|
|
|
|
domain = identity.domain
|
|
|
|
if not domain:
|
|
|
|
actor_url_parts = urlparse(document["actor"])
|
|
|
|
domain = Domain.get_remote_domain(actor_url_parts.hostname)
|
|
|
|
if identity.blocked or domain.recursively_blocked():
|
2022-11-25 17:11:31 -08:00
|
|
|
# I love to lie! Throw it away!
|
2023-10-26 09:01:03 -07:00
|
|
|
logging.info(
|
|
|
|
"Inbox: Discarded message from blocked %s %s",
|
|
|
|
"domain" if domain.recursively_blocked() else "user",
|
|
|
|
identity.actor_uri,
|
|
|
|
)
|
2022-11-25 17:11:31 -08:00
|
|
|
return HttpResponse(status=202)
|
2022-12-05 21:23:35 -08:00
|
|
|
|
2023-11-12 11:09:09 -08:00
|
|
|
# See if it's a type of message we know we want to ignore right now
|
|
|
|
# (e.g. Lemmy likes/dislikes, which we can't process anyway)
|
2023-11-12 11:35:11 -08:00
|
|
|
if document_type == "Announce" and document_subtype in [
|
|
|
|
"Like",
|
|
|
|
"Dislike",
|
|
|
|
"Create",
|
|
|
|
"Undo",
|
|
|
|
"Update",
|
|
|
|
]:
|
2023-11-12 11:09:09 -08:00
|
|
|
return HttpResponse(status=202)
|
|
|
|
|
2023-10-26 09:01:03 -07:00
|
|
|
# authenticate HTTP signature first, if one is present and the actor
|
|
|
|
# is already known to us. An invalid signature is an error and message
|
|
|
|
# should be discarded. NOTE: for previously unknown actors, we
|
|
|
|
# don't have their public key yet!
|
|
|
|
if "signature" in request:
|
2022-11-12 20:14:21 -08:00
|
|
|
try:
|
2023-10-26 09:01:03 -07:00
|
|
|
if identity.public_key:
|
|
|
|
HttpSignature.verify_request(
|
|
|
|
request,
|
|
|
|
identity.public_key,
|
|
|
|
)
|
|
|
|
logging.debug(
|
|
|
|
"Inbox: %s from %s has good HTTP signature",
|
2023-11-12 11:09:09 -08:00
|
|
|
document_type,
|
2023-10-26 09:01:03 -07:00
|
|
|
identity,
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
logging.info(
|
|
|
|
"Inbox: New actor, no key available: %s",
|
|
|
|
document["actor"],
|
|
|
|
)
|
2022-11-12 20:14:21 -08:00
|
|
|
except VerificationFormatError as e:
|
2023-10-26 09:01:03 -07:00
|
|
|
logging.warning("Inbox error: Bad HTTP signature format: %s", e.args[0])
|
2022-11-12 20:14:21 -08:00
|
|
|
return HttpResponseBadRequest(e.args[0])
|
|
|
|
except VerificationError:
|
2023-10-26 09:01:03 -07:00
|
|
|
logging.warning("Inbox error: Bad HTTP signature from %s", identity)
|
2022-11-12 20:14:21 -08:00
|
|
|
return HttpResponseUnauthorized("Bad signature")
|
2022-12-16 18:42:48 -08:00
|
|
|
|
2023-10-26 09:01:03 -07:00
|
|
|
# Mastodon advices not implementing LD Signatures, but
|
|
|
|
# they're widely deployed today. Validate it if one exists.
|
|
|
|
# https://docs.joinmastodon.org/spec/security/#ld
|
|
|
|
if "signature" in document:
|
2022-11-12 20:14:21 -08:00
|
|
|
try:
|
2023-10-26 09:01:03 -07:00
|
|
|
# signatures are identified by the signature block
|
|
|
|
creator = urldefrag(document["signature"]["creator"]).url
|
|
|
|
creator_identity = Identity.by_actor_uri(
|
|
|
|
creator, create=True, transient=True
|
2022-11-12 20:14:21 -08:00
|
|
|
)
|
2023-10-26 09:01:03 -07:00
|
|
|
if not creator_identity.public_key:
|
|
|
|
logging.info("Inbox: New actor, no key available: %s", creator)
|
|
|
|
# if we can't verify it, we don't keep it
|
|
|
|
document.pop("signature")
|
|
|
|
else:
|
|
|
|
LDSignature.verify_signature(document, creator_identity.public_key)
|
|
|
|
logging.debug(
|
|
|
|
"Inbox: %s from %s has good LD signature",
|
|
|
|
document["type"],
|
|
|
|
creator_identity,
|
|
|
|
)
|
2022-11-12 20:14:21 -08:00
|
|
|
except VerificationFormatError as e:
|
2023-10-26 09:01:03 -07:00
|
|
|
logging.warning("Inbox error: Bad LD signature format: %s", e.args[0])
|
2022-11-12 20:14:21 -08:00
|
|
|
return HttpResponseBadRequest(e.args[0])
|
|
|
|
except VerificationError:
|
2023-10-26 09:01:03 -07:00
|
|
|
# An invalid LD Signature might also indicate nothing but
|
|
|
|
# a syntactical difference between implementations.
|
|
|
|
# Strip it out if we can't verify it.
|
|
|
|
if "signature" in document:
|
|
|
|
document.pop("signature")
|
|
|
|
logging.info(
|
|
|
|
"Inbox: Stripping invalid LD signature from %s %s",
|
|
|
|
creator_identity,
|
|
|
|
document["id"],
|
|
|
|
)
|
|
|
|
|
|
|
|
if not ("signature" in request or "signature" in document):
|
|
|
|
logging.debug(
|
|
|
|
"Inbox: %s from %s is unauthenticated. That's OK.",
|
|
|
|
document["type"],
|
|
|
|
identity,
|
|
|
|
)
|
2022-12-16 18:42:48 -08:00
|
|
|
|
2022-12-30 10:06:38 -08:00
|
|
|
# Don't allow injection of internal messages
|
|
|
|
if document["type"].startswith("__"):
|
|
|
|
return HttpResponseUnauthorized("Bad type")
|
|
|
|
|
2022-11-12 20:14:21 -08:00
|
|
|
# Hand off the item to be processed by the queue
|
|
|
|
InboxMessage.objects.create(message=document)
|
|
|
|
return HttpResponse(status=202)
|
2022-11-20 17:29:19 -08:00
|
|
|
|
|
|
|
|
2022-12-18 18:47:35 -08:00
|
|
|
class Outbox(View):
|
|
|
|
"""
|
|
|
|
The ActivityPub outbox for an identity
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request, handle):
|
|
|
|
self.identity = by_handle_or_404(
|
|
|
|
self.request,
|
|
|
|
handle,
|
|
|
|
local=False,
|
|
|
|
fetch=True,
|
|
|
|
)
|
|
|
|
# If this not a local actor, 404
|
|
|
|
if not self.identity.local:
|
|
|
|
raise Http404("Not a local identity")
|
|
|
|
# Return an ordered collection with the most recent 10 public posts
|
|
|
|
posts = list(self.identity.posts.not_hidden().public()[:10])
|
|
|
|
return JsonResponse(
|
|
|
|
canonicalise(
|
|
|
|
{
|
|
|
|
"type": "OrderedCollection",
|
|
|
|
"totalItems": len(posts),
|
|
|
|
"orderedItems": [post.to_ap() for post in posts],
|
|
|
|
}
|
|
|
|
),
|
2023-05-13 09:01:27 -07:00
|
|
|
content_type="application/activity+json",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class FeaturedCollection(View):
|
|
|
|
"""
|
|
|
|
An ordered collection of all pinned posts of an identity
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request, handle):
|
|
|
|
self.identity = by_handle_or_404(
|
|
|
|
request,
|
|
|
|
handle,
|
|
|
|
local=False,
|
|
|
|
fetch=True,
|
|
|
|
)
|
|
|
|
if not self.identity.local:
|
|
|
|
raise Http404("Not a local identity")
|
|
|
|
posts = list(TimelineService(self.identity).identity_pinned())
|
|
|
|
return JsonResponse(
|
|
|
|
canonicalise(
|
|
|
|
{
|
|
|
|
"type": "OrderedCollection",
|
|
|
|
"id": self.identity.actor_uri + "collections/featured/",
|
|
|
|
"totalItems": len(posts),
|
|
|
|
"orderedItems": [post.to_ap() for post in posts],
|
|
|
|
}
|
|
|
|
),
|
2022-12-18 18:47:35 -08:00
|
|
|
content_type="application/activity+json",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-25 13:26:15 -08:00
|
|
|
@method_decorator(cache_control(max_age=60 * 15), name="dispatch")
|
|
|
|
class EmptyOutbox(StaticContentView):
|
2022-12-18 18:47:35 -08:00
|
|
|
"""
|
|
|
|
A fixed-empty outbox for the system actor
|
|
|
|
"""
|
|
|
|
|
2022-12-25 13:26:15 -08:00
|
|
|
content_type: str = "application/activity+json"
|
|
|
|
|
|
|
|
def get_static_content(self) -> str | bytes:
|
|
|
|
return json.dumps(
|
2022-12-18 18:47:35 -08:00
|
|
|
canonicalise(
|
|
|
|
{
|
|
|
|
"type": "OrderedCollection",
|
|
|
|
"totalItems": 0,
|
|
|
|
"orderedItems": [],
|
|
|
|
}
|
2022-12-25 13:26:15 -08:00
|
|
|
)
|
2022-12-18 18:47:35 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-25 13:26:15 -08:00
|
|
|
@method_decorator(cache_control(max_age=60 * 15), name="dispatch")
|
|
|
|
class SystemActorView(StaticContentView):
|
2022-11-20 17:29:19 -08:00
|
|
|
"""
|
|
|
|
Special endpoint for the overall system actor
|
|
|
|
"""
|
|
|
|
|
2022-12-25 13:26:15 -08:00
|
|
|
content_type: str = "application/activity+json"
|
|
|
|
|
|
|
|
def get_static_content(self) -> str | bytes:
|
|
|
|
return json.dumps(
|
2022-11-20 17:29:19 -08:00
|
|
|
canonicalise(
|
|
|
|
SystemActor().to_ap(),
|
|
|
|
include_security=True,
|
2022-12-25 13:26:15 -08:00
|
|
|
)
|
2022-11-20 17:29:19 -08:00
|
|
|
)
|