From e357f4a7a6da7478739aff22699e7930a2cef079 Mon Sep 17 00:00:00 2001 From: Mouse Reeve Date: Sat, 25 Jan 2020 16:24:22 -0800 Subject: [PATCH] Adds webfinger --- fedireads/activitystream.py | 26 +++++++++++++++++++++++++- fedireads/settings.py | 1 + fedireads/urls.py | 2 +- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/fedireads/activitystream.py b/fedireads/activitystream.py index 06c8425be..99ce4eeb5 100644 --- a/fedireads/activitystream.py +++ b/fedireads/activitystream.py @@ -1,4 +1,28 @@ ''' activitystream api ''' +from django.http import HttpResponseBadRequest, HttpResponseNotFound, JsonResponse +from fedireads.settings import DOMAIN +from fedireads.models import User def webfinger(request): - return 'hi' + ''' allow other servers to ask about a user ''' + resource = request.GET.get('resource') + if not resource and not resource.startswith('acct:'): + return HttpResponseBadRequest() + account = resource.replace('acct:', '') + account = account.replace('@' + DOMAIN, '') + user = User.objects.filter(username=account).first() + if not user: + return HttpResponseNotFound('No account found') + return JsonResponse(format_webfinger(user)) + +def format_webfinger(user): + return { + 'subject': 'acct:%s@%s' % (user.username, DOMAIN), + 'links': [ + { + 'rel': 'self', + 'type': 'application/activity+json', + 'href': 'https://%s/user/%s' % (DOMAIN, user.username), + } + ] + } diff --git a/fedireads/settings.py b/fedireads/settings.py index c08cb7c2d..1359408c7 100644 --- a/fedireads/settings.py +++ b/fedireads/settings.py @@ -27,6 +27,7 @@ DEBUG = True ALLOWED_HOSTS = [] +DOMAIN = 'localhost' # Application definition diff --git a/fedireads/urls.py b/fedireads/urls.py index 9c9644823..cc145cb44 100644 --- a/fedireads/urls.py +++ b/fedireads/urls.py @@ -23,5 +23,5 @@ urlpatterns = [ path('login/', views.user_login), path('logout/', views.user_logout), path('api/book/', openlibrary.get_book), - path('webfinger/', activitystream.webfinger), + path('.well-known/webfinger', activitystream.webfinger), ]