Newer
Older
import logging
from django.contrib.auth.models import AnonymousUser
from rest_framework import authentication, exceptions as rest_exceptions
from funkwhale_api.moderation import models as moderation_models
from . import actors, exceptions, keys, signing, utils
logger = logging.getLogger(__name__)
class SignatureAuthentication(authentication.BaseAuthentication):
def authenticate_actor(self, request):
headers = utils.clean_wsgi_headers(request.META)
key_id = keys.get_key_id_from_signature_header(signature)
except KeyError:
actor_url = key_id.split("#")[0]
except (TypeError, IndexError, AttributeError):
policies = (
moderation_models.InstancePolicy.objects.active()
.filter(block_all=True)
.matching_url(actor_url)
)
if policies.exists():
raise exceptions.BlockedActorOrDomain()
try:
actor = actors.get_actor(actor_url)
logger.info(
"Discarding HTTP request from blocked actor/domain %s", actor_url
)
if not actor.public_key:
raise rest_exceptions.AuthenticationFailed("No public key found")
signing.verify_django(request, actor.public_key.encode("utf-8"))
except cryptography.exceptions.InvalidSignature:
def authenticate(self, request):
actor = self.authenticate_actor(request)