Ejemplo n.º 1
0
def create_app(handler: _Handler,
               key: bytes,
               max_age: Optional[int] = None) -> web.Application:
    middleware = session_middleware(NaClCookieStorage(key, max_age=max_age))
    app = web.Application(middlewares=[middleware])
    app.router.add_route('GET', '/', handler)
    return app
Ejemplo n.º 2
0
def startup(setup_mode):
    # Entry point. Return an awaitable to be run.

    # encrypted cookies for session logic
    from aiohttp_session.nacl_storage import NaClCookieStorage
    from aiohttp_session import session_middleware
    todays_key = os.urandom(32)
    sml = session_middleware(NaClCookieStorage(todays_key, cookie_name='X'))

    # create app: order of middlewares matters
    app = web.Application(middlewares=[sml, auth_everything])
    app.add_routes(routes)
    app.router.add_static('/static', './static')

    # hack to obfuscate our identity a little
    # - from <https://w3techs.com/technologies/details/ws-nginx/1>  23% market share
    import aiohttp.web_response as ht
    ht.SERVER_SOFTWARE = 'nginx/1.14.1'

    j_env = aiohttp_jinja2.setup(app,
                                 loader=jinja2.FileSystemLoader('templates'),
                                 filters=extra_filters(app))
    j_env.globals.update(default_context())
    j_env.policies['json.dumps_function'] = json_dumps

    my_url = f"http://localhost:{settings.PORT_NUMBER}" + ('/setup' if
                                                           setup_mode else '')
    logging.info(f"Web server at:    {my_url}")

    # meh; kinda annoying.
    if 0:

        def popup():
            try:
                # see <https://github.com/jupyter/notebook/issues/3746#issuecomment-444957821>
                # if this doesn't happen on MacOS
                from webbrowser import open as open_browser
                open_browser(my_url)
            except:
                logging.error("Unable to pop browser open", exc_info=1)

        asyncio.get_running_loop().call_later(3, popup)

    from aiohttp.abc import AbstractAccessLogger

    class AccessLogger(AbstractAccessLogger):
        def log(self, request, response, time):
            self.logger.info(
                f'{response.status} <= {request.method} {request.path}')

    return web._run_app(app,
                        port=settings.PORT_NUMBER,
                        print=None,
                        access_log_class=AccessLogger)
Ejemplo n.º 3
0
    def create_server(self, method, path, handler):
        middleware = session_middleware(NaClCookieStorage(self.key))
        app = web.Application(middlewares=[middleware], loop=self.loop)
        app.router.add_route(method, path, handler)

        port = self.find_unused_port()
        handler = app.make_handler()
        srv = yield from self.loop.create_server(handler, '127.0.0.1', port)
        url = "http://127.0.0.1:{}".format(port) + path
        self.srv = srv
        self.handler = handler
        return app, srv, url
Ejemplo n.º 4
0
def create_app(handler, key):
    middleware = session_middleware(NaClCookieStorage(key))
    app = web.Application(middlewares=[middleware])
    app.router.add_route('GET', '/', handler)
    return app
Ejemplo n.º 5
0
def test_invalid_key():
    with pytest.raises(ValueError):
        NaClCookieStorage(b'123')  # short key
Ejemplo n.º 6
0
 def test_invalid_key(self):
     with self.assertRaises(ValueError):
         NaClCookieStorage(b'123')  # short key