def auth_middleware(policy):
    """Returns a aiohttp_auth middleware factory for use by the aiohttp
    application object.

    Args:
        policy: A authentication policy with a base class of
            AbstractAuthentication.
    """
    assert isinstance(policy, AbstractAuthentication)

    async def _auth_middleware_factory(app, handler):

        async def _middleware_handler(request):
            # Save the policy in the request
            request[POLICY_KEY] = policy

            # Call the next handler in the chain
            response = await handler(request)

            # Give the policy a chance to handle the response
            await policy.process_response(request, response)

            return response

        return _middleware_handler

    return _auth_middleware_factory
Returns a aiohttp_auth middleware factory for use by the aiohttp
    application object.

    Args:
        policy: A authentication policy with a base class of
            AbstractAuthentication.