Newer
Older
import cryptography
from django.contrib.auth.models import AnonymousUser
from rest_framework import authentication
from rest_framework import exceptions
from . import actors
from . import keys
from . import signing
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:
except ValueError as e:
raise exceptions.AuthenticationFailed(str(e))
try:
except Exception as e:
raise exceptions.AuthenticationFailed(str(e))
if not actor.public_key:
raise exceptions.AuthenticationFailed("No public key found")
signing.verify_django(request, actor.public_key.encode("utf-8"))
except cryptography.exceptions.InvalidSignature:
raise exceptions.AuthenticationFailed("Invalid signature")
def authenticate(self, request):
actor = self.authenticate_actor(request)