Url and views for instance actor and webfinger
This commit is contained in:
parent
e793f8365f
commit
75710638de
5 changed files with 181 additions and 0 deletions
21
api/funkwhale_api/federation/serializers.py
Normal file
21
api/funkwhale_api/federation/serializers.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
from django.urls import reverse
|
||||
from django.conf import settings
|
||||
|
||||
from . import utils
|
||||
|
||||
|
||||
def repr_instance_actor():
|
||||
"""
|
||||
We do not use a serializer here, since it's pretty static
|
||||
"""
|
||||
return {
|
||||
'@context': [
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
'https://w3id.org/security/v1',
|
||||
{},
|
||||
],
|
||||
'id': utils.full_url(reverse('federation:instance-actor')),
|
||||
'type': 'Service',
|
||||
'inbox': utils.full_url(reverse('federation:instance-inbox')),
|
||||
'outbox': utils.full_url(reverse('federation:instance-outbox')),
|
||||
}
|
||||
15
api/funkwhale_api/federation/urls.py
Normal file
15
api/funkwhale_api/federation/urls.py
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
from rest_framework import routers
|
||||
|
||||
from . import views
|
||||
|
||||
router = routers.SimpleRouter(trailing_slash=False)
|
||||
router.register(
|
||||
r'instance',
|
||||
views.InstanceViewSet,
|
||||
'instance')
|
||||
router.register(
|
||||
r'.well-known',
|
||||
views.WellKnownViewSet,
|
||||
'well-known')
|
||||
|
||||
urlpatterns = router.urls
|
||||
73
api/funkwhale_api/federation/views.py
Normal file
73
api/funkwhale_api/federation/views.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.http import HttpResponse
|
||||
|
||||
from rest_framework import viewsets
|
||||
from rest_framework import views
|
||||
from rest_framework import response
|
||||
from rest_framework.decorators import list_route
|
||||
|
||||
from . import serializers
|
||||
from . import webfinger
|
||||
|
||||
|
||||
class FederationMixin(object):
|
||||
def dispatch(self, request, *args, **kwargs):
|
||||
if not settings.FEDERATION_ENABLED:
|
||||
return HttpResponse(status=405)
|
||||
return super().dispatch(request, *args, **kwargs)
|
||||
|
||||
|
||||
class InstanceViewSet(FederationMixin, viewsets.GenericViewSet):
|
||||
authentication_classes = []
|
||||
permission_classes = []
|
||||
|
||||
@list_route(methods=['get'])
|
||||
def actor(self, request, *args, **kwargs):
|
||||
return response.Response(serializers.repr_instance_actor())
|
||||
|
||||
@list_route(methods=['get'])
|
||||
def inbox(self, request, *args, **kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
@list_route(methods=['get'])
|
||||
def outbox(self, request, *args, **kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class WellKnownViewSet(FederationMixin, viewsets.GenericViewSet):
|
||||
authentication_classes = []
|
||||
permission_classes = []
|
||||
|
||||
@list_route(methods=['get'])
|
||||
def webfinger(self, request, *args, **kwargs):
|
||||
try:
|
||||
resource_type, resource = webfinger.clean_resource(
|
||||
request.GET['resource'])
|
||||
cleaner = getattr(webfinger, 'clean_{}'.format(resource_type))
|
||||
result = cleaner(resource)
|
||||
except forms.ValidationError as e:
|
||||
return response.Response({
|
||||
'errors': {
|
||||
'resource': e.message
|
||||
}
|
||||
}, status=400)
|
||||
except KeyError:
|
||||
return response.Response({
|
||||
'errors': {
|
||||
'resource': 'This field is required',
|
||||
}
|
||||
}, status=400)
|
||||
|
||||
handler = getattr(self, 'handler_{}'.format(resource_type))
|
||||
data = handler(result)
|
||||
|
||||
return response.Response(
|
||||
data,
|
||||
content_type='application/jrd+json; charset=utf-8')
|
||||
|
||||
def handler_acct(self, clean_result):
|
||||
username, hostname = clean_result
|
||||
if username == 'service':
|
||||
return webfinger.serialize_system_acct()
|
||||
return {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue