import logging from aiohttp.web import HTTPUnauthorized from json.decoder import JSONDecodeError from . import misc async def http_signatures_middleware(app, handler): async def http_signatures_handler(request): request['validated'] = False request['actor'] = None try: request['data'] = await request.json() if app['config'].is_banned(request['data']['actor']): raise HTTPUnauthorized(body='banned') except JSONDecodeError: request['data'] = None if 'signature' in request.headers and request.method == 'POST': if 'actor' not in request['data']: raise HTTPUnauthorized(body='signature check failed, no actor in message') request['actor'] = await misc.request(request['data']['actor']) if not request['actor']: logging.warning('Failed to fetch actor:', request['data']['actor']) raise HTTPUnauthorized('failed to fetch actor') actor_id = request['actor']['id'] if not (await misc.validate_signature(actor_id, request)): logging.warning(f'signature validation failed for: {actor_id}') raise HTTPUnauthorized(body='signature check failed, signature did not match key') return (await handler(request)) return (await handler(request)) return http_signatures_handler