Beispiel #1
0
def async_setup(hass, config):
    api_password = config[DOMAIN][KEY_API_PASSWORD]

    @middleware
    async def auth_middleware(request, handler):
        if KEY_API_PASSWORD in request.query and \
               request.query[KEY_API_PASSWORD] == api_password:

            auth_type = 'api_password'
            _LOGGER.debug(
                "Authenticated %s for %s using %s",
                request,
                request.path,
                auth_type,
            )

            request[KEY_AUTHENTICATED] = True

        return await handler(request)

    # NOTE: This hack is an accident waiting to happen, however HASS
    # doesn't seem to offer a way to add a middleware to the http
    # component. aiohttp "freezes" app state after start so it's
    # impossible to use public API to add a middleware there directly.
    hass.http.app._middlewares = FrozenList(
        [*hass.http.app.middlewares, auth_middleware])
    hass.http.app._middlewares_handlers = tuple(
        hass.http.app._prepare_middleware())
    return True
Beispiel #2
0
async def async_setup(hass: HomeAssistant, hass_config: dict):
    @middleware
    async def fix_middleware(request, handler):
        resp: Response = await handler(request)
        resp._compression = False
        return resp

    hass.http.app._middlewares = FrozenList(
        list(hass.http.app.middlewares) + [fix_middleware])
    hass.http.app._middlewares_handlers = tuple(
        hass.http.app._prepare_middleware())
    return True
Beispiel #3
0
class SignalManager:
    _signals = FrozenList()

    @classmethod
    def register(cls, sig):
        cls._signals.append(sig)

    @classmethod
    def freeze(cls):
        cls._signals.freeze()
        for sig in cls._signals:
            sig.freeze()
Beispiel #4
0
def add_oauth_middleware(app,
                         cookie_key=None,
                         cookie_name='aiohttp_oauth',
                         cookie_is_secure=False,
                         **kwargs):
    if cookie_key is None:
        print('creating new cookie secret')
        cookie_key = os.urandom(16).hex()

    app._middlewares = FrozenList([
        session_middleware(
            EncryptedCookieStorage(cookie_key.encode(),
                                   cookie_name=cookie_name,
                                   secure=cookie_is_secure,
                                   max_age=7200)),  # two hours
        oauth_middleware(**kwargs)
    ] + list(app._middlewares))