2022-11-12 20:14:21 -08:00
|
|
|
import json
|
|
|
|
|
|
|
|
from asgiref.sync import async_to_sync
|
2022-11-18 07:28:15 -08:00
|
|
|
from django.conf import settings
|
2022-11-21 07:34:02 -08:00
|
|
|
from django.http import HttpResponse, HttpResponseBadRequest, JsonResponse
|
2022-11-12 20:14:21 -08:00
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
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
|
2022-11-20 13:20:28 -08:00
|
|
|
from core import exceptions
|
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-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
|
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-11-12 20:14:21 -08:00
|
|
|
content_type="application/xml",
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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
|
|
|
|
local_identities = Identity.objects.filter(local=True).count()
|
|
|
|
local_posts = Post.objects.filter(local=True).count()
|
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"version": "2.0",
|
|
|
|
"software": {"name": "takahe", "version": __version__},
|
|
|
|
"protocols": ["activitypub"],
|
|
|
|
"services": {"outbound": [], "inbound": []},
|
|
|
|
"usage": {
|
|
|
|
"users": {"total": local_identities},
|
|
|
|
"localPosts": local_posts,
|
|
|
|
},
|
|
|
|
"openRegistrations": Config.system.signup_allowed
|
|
|
|
and not Config.system.signup_invite_only,
|
|
|
|
"metadata": {},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
handle = actor.handle
|
|
|
|
|
|
|
|
return JsonResponse(
|
|
|
|
{
|
|
|
|
"subject": f"acct:{handle}",
|
|
|
|
"aliases": [
|
|
|
|
actor.absolute_profile_uri(),
|
|
|
|
],
|
|
|
|
"links": [
|
|
|
|
{
|
|
|
|
"rel": "http://webfinger.net/rel/profile-page",
|
|
|
|
"type": "text/html",
|
|
|
|
"href": actor.absolute_profile_uri(),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"rel": "self",
|
|
|
|
"type": "application/activity+json",
|
|
|
|
"href": actor.actor_uri,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
}
|
|
|
|
)
|
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-11-12 20:14:21 -08:00
|
|
|
# Load the LD
|
|
|
|
document = canonicalise(json.loads(request.body), include_security=True)
|
|
|
|
# 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.
|
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 (
|
|
|
|
document["type"] == "Delete"
|
|
|
|
and document["actor"] == document["object"]
|
|
|
|
and not identity.pk
|
|
|
|
):
|
|
|
|
# We don't have an Identity record for the user. No-op
|
|
|
|
exceptions.capture_message(
|
|
|
|
f"Inbox: Discarded delete message for unknown actor {document['actor']}"
|
|
|
|
)
|
|
|
|
return HttpResponse(status=202)
|
|
|
|
|
2022-11-12 20:14:21 -08:00
|
|
|
if not identity.public_key:
|
|
|
|
# See if we can fetch it right now
|
|
|
|
async_to_sync(identity.fetch_actor)()
|
2022-12-05 21:23:35 -08:00
|
|
|
|
2022-11-12 20:14:21 -08:00
|
|
|
if not identity.public_key:
|
2022-11-20 13:20:28 -08:00
|
|
|
exceptions.capture_message(
|
|
|
|
f"Inbox error: cannot fetch actor {document['actor']}"
|
|
|
|
)
|
2022-11-12 20:14:21 -08:00
|
|
|
return HttpResponseBadRequest("Cannot retrieve actor")
|
2022-12-16 18:42:48 -08:00
|
|
|
|
|
|
|
# See if it's from a blocked user or domain
|
|
|
|
if identity.blocked or identity.domain.blocked:
|
2022-11-25 17:11:31 -08:00
|
|
|
# I love to lie! Throw it away!
|
|
|
|
exceptions.capture_message(
|
2022-12-16 18:42:48 -08:00
|
|
|
f"Inbox: Discarded message from {identity.actor_uri}"
|
2022-11-25 17:11:31 -08:00
|
|
|
)
|
|
|
|
return HttpResponse(status=202)
|
2022-12-05 21:23:35 -08:00
|
|
|
|
2022-11-12 20:14:21 -08:00
|
|
|
# If there's a "signature" payload, verify against that
|
|
|
|
if "signature" in document:
|
|
|
|
try:
|
|
|
|
LDSignature.verify_signature(document, identity.public_key)
|
|
|
|
except VerificationFormatError as e:
|
2022-11-20 13:20:28 -08:00
|
|
|
exceptions.capture_message(
|
|
|
|
f"Inbox error: Bad LD signature format: {e.args[0]}"
|
|
|
|
)
|
2022-11-12 20:14:21 -08:00
|
|
|
return HttpResponseBadRequest(e.args[0])
|
|
|
|
except VerificationError:
|
2022-11-20 13:20:28 -08:00
|
|
|
exceptions.capture_message("Inbox error: Bad LD signature")
|
2022-11-12 20:14:21 -08:00
|
|
|
return HttpResponseUnauthorized("Bad signature")
|
2022-12-16 18:42:48 -08:00
|
|
|
|
2022-11-12 20:14:21 -08:00
|
|
|
# Otherwise, verify against the header (assuming it's the same actor)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
HttpSignature.verify_request(
|
|
|
|
request,
|
|
|
|
identity.public_key,
|
|
|
|
)
|
|
|
|
except VerificationFormatError as e:
|
2022-11-20 13:20:28 -08:00
|
|
|
exceptions.capture_message(
|
|
|
|
f"Inbox error: Bad HTTP signature format: {e.args[0]}"
|
|
|
|
)
|
2022-11-12 20:14:21 -08:00
|
|
|
return HttpResponseBadRequest(e.args[0])
|
|
|
|
except VerificationError:
|
2022-11-20 13:20:28 -08:00
|
|
|
exceptions.capture_message("Inbox error: Bad HTTP signature")
|
2022-11-12 20:14:21 -08:00
|
|
|
return HttpResponseUnauthorized("Bad signature")
|
2022-12-16 18:42:48 -08:00
|
|
|
|
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-05 09:55:30 -08:00
|
|
|
@method_decorator(cache_page(), name="dispatch")
|
2022-11-20 17:29:19 -08:00
|
|
|
class SystemActorView(View):
|
|
|
|
"""
|
|
|
|
Special endpoint for the overall system actor
|
|
|
|
"""
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return JsonResponse(
|
|
|
|
canonicalise(
|
|
|
|
SystemActor().to_ap(),
|
|
|
|
include_security=True,
|
2022-12-17 11:00:47 -08:00
|
|
|
),
|
|
|
|
content_type="application/activity+json",
|
2022-11-20 17:29:19 -08:00
|
|
|
)
|