def __init__(self): this_directory = os.path.dirname(__file__) self.config = load_config( os.path.join(this_directory, '../config/config.yaml')) logger.info("Init CraftBeerPI") policy = auth.SessionTktAuthentication(urandom(32), 60, include_ip=True) middlewares = [ session_middleware(EncryptedCookieStorage(urandom(32))), auth.auth_middleware(policy) ] self.app = web.Application(middlewares=middlewares) self.initializer = [] setup(self.app, self) self.bus = EventBus(self.app.loop, self) self.ws = WebSocket(self) self.actor = ActorController(self) self.sensor = SensorController(self) self.plugin = PluginController(self) self.system = SystemController(self) self.config2 = ConfigController(self) self.kettle = KettleController(self) self.step = StepController(self) self.notification = NotificationController(self) self.login = Login(self) self.register_events(self.ws)
def __init__(self): self.static_config = load_config("./config/config.yaml") self.database_file = "./craftbeerpi.db" logger.info("Init CraftBeerPI") policy = auth.SessionTktAuthentication(urandom(32), 60, include_ip=True) middlewares = [web.normalize_path_middleware(), session_middleware(EncryptedCookieStorage(urandom(32))), auth.auth_middleware(policy), error_middleware] self.app = web.Application(middlewares=middlewares) self._setup_shutdownhook() self.initializer = [] self.bus = CBPiEventBus(self.app.loop, self) self.ws = CBPiWebSocket(self) self.job = JobController(self) self.actor = ActorController(self) self.sensor = SensorController(self) self.plugin = PluginController(self) self.system = SystemController(self) self.config = ConfigController(self) self.kettle = KettleController(self) self.step = StepController(self) self.dashboard = DashboardController(self) self.http_step = StepHttpEndpoints(self) self.http_sensor = SensorHttpEndpoints(self) self.http_config = ConfigHttpEndpoints(self) self.http_actor = ActorHttpEndpoints(self) self.http_kettle = KettleHttpEndpoints(self) self.http_dashboard = DashBoardHttpEndpoints(self) self.notification = NotificationController(self) self.login = Login(self)
def app(loop): """Default app fixture for tests.""" async def handler_remember(request): user_identity = request.match_info['user'] await auth.remember(request, user_identity) return web.Response(text='remember') @autz_required('admin') async def handler_admin(request): return web.Response(text='admin') @autz_required('guest') async def handler_guest(request): return web.Response(text='guest') application = web.Application(loop=loop) secret = b'01234567890abcdef' storage = aiohttp_session.SimpleCookieStorage() policy = auth.SessionTktAuthentication(secret, 15, cookie_name='auth') aiohttp_session.setup(application, storage) auth.setup(application, policy) autz_policy = CustomAutzPolicy(admin_user_identity='alex') autz.setup(application, autz_policy) application.router.add_get('/remember/{user}', handler_remember) application.router.add_get('/admin', handler_admin) application.router.add_get('/guest', handler_guest) yield application
def init(loop, host='0.0.0.0', port=10000): from ircb.storeclient import initialize initialize() load_config() policy = auth.SessionTktAuthentication(settings.WEB_SALT, 60, include_ip=True) middlewares = [ session_middleware(EncryptedCookieStorage(settings.WEB_SALT)), auth.auth_middleware(policy) ] app = web.Application(middlewares=middlewares) app.router.add_route('GET', '/', index) app.router.add_route('*', '/api/v1/signup', SignupView, name='signup') app.router.add_route('*', '/api/v1/signin', SigninView, name='signin') app.router.add_route('*', '/api/v1/signout', SignoutView, name='signout') app.router.add_route('*', '/api/v1/networks', NetworkListView, name='networks') app.router.add_route('*', '/api/v1/network/{id}', NetworkView, name='network') app.router.add_route('PUT', '/api/v1/network/{id}/{action}', NetworkConnectionView, name='network_connection') srv = yield from loop.create_server( app.make_handler(logger=logger, access_log=logger), host, port) return srv
async def test_middleware_installed_no_session(self): middlewares = [ session_middleware(SimpleCookieStorage()), auth_middleware(auth.SessionTktAuthentication(urandom(16), 15)) ] request = await make_request('GET', '/', middlewares) user_id = await auth.get_auth(request) self.assertIsNone(user_id)
def __init__(self): self.path = os.sep.join(os.path.abspath(__file__).split( os.sep)[:-1]) # The path to the package dir self.version = __version__ self.static_config = load_config( os.path.join(".", 'config', "config.yaml")) self.database_file = os.path.join(".", 'config', "craftbeerpi.db") logger.info("Init CraftBeerPI") policy = auth.SessionTktAuthentication(urandom(32), 60, include_ip=True) middlewares = [ web.normalize_path_middleware(), session_middleware(EncryptedCookieStorage(urandom(32))), auth.auth_middleware(policy), error_middleware ] self.app = web.Application(middlewares=middlewares) self.app["cbpi"] = self self._setup_shutdownhook() self.initializer = [] self.bus = CBPiEventBus(self.app.loop, self) self.job = JobController(self) self.config = ConfigController(self) self.ws = CBPiWebSocket(self) self.actor = ActorController(self) self.sensor = SensorController(self) self.plugin = PluginController(self) self.log = LogController(self) self.system = SystemController(self) self.kettle = KettleController(self) self.step: StepController = StepController(self) self.recipe: RecipeController = RecipeController(self) self.notification: NotificationController = NotificationController( self) self.satellite = None if self.static_config.get("mqtt", False) is True: self.satellite: SatelliteController = SatelliteController(self) self.dashboard = DashboardController(self) self.http_step = StepHttpEndpoints(self) self.http_recipe = RecipeHttpEndpoints(self) self.http_sensor = SensorHttpEndpoints(self) self.http_config = ConfigHttpEndpoints(self) self.http_actor = ActorHttpEndpoints(self) self.http_kettle = KettleHttpEndpoints(self) self.http_dashboard = DashBoardHttpEndpoints(self) self.http_plugin = PluginHttpEndpoints(self) self.http_system = SystemHttpEndpoints(self) self.http_log = LogHttpEndpoints(self) self.http_notification = NotificationHttpEndpoints(self) self.login = Login(self)
async def test_middleware_stores_auth_in_session(self): secret = b'01234567890abcdef' storage = SimpleCookieStorage() auth_ = auth.SessionTktAuthentication(secret, 15, cookie_name='auth') middlewares = [session_middleware(storage), auth_middleware(auth_)] request = await make_request('GET', '/', middlewares) await auth.remember(request, 'some_user') response = await make_response(request, middlewares) self.assertTrue(auth_.cookie_name in \ response.cookies.get(storage.cookie_name).value)
async def test_middleware_gets_auth_from_session(self): secret = b'01234567890abcdef' storage = SimpleCookieStorage() auth_ = auth.SessionTktAuthentication(secret, 15, cookie_name='auth') middlewares = [session_middleware(storage), auth_middleware(auth_)] session_data = make_auth_session(secret, 'some_user', auth_.cookie_name) request = await make_request('GET', '/', middlewares, \ [(storage.cookie_name, json.dumps(session_data))]) user_id = await auth.get_auth(request) self.assertEqual(user_id, 'some_user')
async def test_middleware_installed_no_session(app, client): async def handler_test(request): user_id = await auth.get_auth(request) assert user_id is None return web.Response(text='test') app.router.add_get('/test', handler_test) aiohttp_session.setup(app, aiohttp_session.SimpleCookieStorage()) auth.setup(app, auth.SessionTktAuthentication(urandom(16), 15)) cli = await client(app) await assert_response(cli.get('/test'), 'test')
async def test_middleware_gets_auth_from_session(app, client): secret = b'01234567890abcdef' storage = aiohttp_session.SimpleCookieStorage() policy = auth.SessionTktAuthentication(secret, 15, cookie_name='auth') aiohttp_session.setup(app, storage) auth.setup(app, policy) cli = await client(app) response = await cli.get('/remember') assert await response.text() == 'remember' await assert_response(cli.get('/auth'), 'auth')
async def test_middleware_stores_auth_in_session(app, client): secret = b'01234567890abcdef' storage = aiohttp_session.SimpleCookieStorage() policy = auth.SessionTktAuthentication(secret, 15, cookie_name='auth') aiohttp_session.setup(app, storage) auth.setup(app, policy) cli = await client(app) response = await cli.get('/remember') text = await response.text() assert text == 'remember' value = response.cookies.get(storage.cookie_name).value assert policy.cookie_name in value
def setupAuth(self, app): # setup session middleware in aiohttp fashion storage = EncryptedCookieStorage(urandom(32)) aiohttp_session.setup(app, storage) # Create an auth ticket mechanism that expires after 1 minute (60 # seconds), and has a randomly generated secret. Also includes the # optional inclusion of the users IP address in the hash policy = auth.SessionTktAuthentication(urandom(32), 60, include_ip=True) # setup aiohttp_auth.auth middleware in aiohttp fashion auth.setup(app, policy) app.middlewares.append(self.checkAuth) app.router.add_route('GET', '/logout', self.logout)
def app(loop): """Default app fixture for tests.""" async def handler_remember(request): await auth.remember(request, 'some_user') return web.Response(text='remember') application = web.Application(loop=loop) secret = b'01234567890abcdef' storage = aiohttp_session.SimpleCookieStorage() policy = auth.SessionTktAuthentication(secret, 15, cookie_name='auth') aiohttp_session.setup(application, storage) auth.setup(application, policy) application.router.add_get('/remember', handler_remember) yield application
async def test_middleware_forget_with_session(app, client): secret = b'01234567890abcdef' storage = aiohttp_session.SimpleCookieStorage() policy = auth.SessionTktAuthentication(secret, 15, cookie_name='auth') aiohttp_session.setup(app, storage) auth.setup(app, policy) cli = await client(app) response = await assert_response(cli.get('/remember'), 'remember') value = response.cookies.get(storage.cookie_name).value assert policy.cookie_name in value response = await assert_response(cli.get('/forget'), 'forget') value = response.cookies.get(storage.cookie_name).value assert policy.cookie_name not in value with pytest.raises(AssertionError): await assert_response(cli.get('/auth'), 'auth')
async def test_aiohttp_auth_middleware_setup(loop): app = web.Application(loop=loop) secret = b'01234567890abcdef' storage = aiohttp_session.SimpleCookieStorage() aiohttp_session.setup(app, storage) auth_policy = auth.SessionTktAuthentication(secret, 15, cookie_name='auth') class ACLAutzPolicy(acl.AbstractACLAutzPolicy): async def acl_groups(self, user_identity): return None # pragma: no cover autz_policy = ACLAutzPolicy() aiohttp_auth.setup(app, auth_policy, autz_policy) middleware = auth.auth_middleware(auth_policy) assert app.middlewares[-2].__name__ == middleware.__name__ middleware = autz.autz_middleware(autz_policy) assert app.middlewares[-1].__name__ == middleware.__name__
async def web_server(): web_app = web.Application(client_max_size=30000000, middlewares=[error_middleware]) storage = EncryptedCookieStorage(urandom(32)) aiohttp_session.setup(web_app, storage) policy = auth.SessionTktAuthentication(urandom(32), 86400000, include_ip=True) auth.setup(web_app, policy) web_app.add_routes(routes) aiohttp_jinja2.setup( web_app, loader=jinja2.FileSystemLoader('book/webserver/template')) web_app['static_root_url'] = '/static' web_app.router.add_static("/static", "book/webserver/template/static") web_app.router.add_route("*", "/ws/", WebSocketAsync) web_app.router.add_static("/js", STATIC_DIR) web_app.router.add_static("/", ".") WebSocketAsync.add_route("list_people", ws_routes.list_people.list_people) WebSocketAsync.add_route("get_people", ws_routes.get_people.get_people) WebSocketAsync.add_route("delete_people", ws_routes.delete_people.delete_people) WebSocketAsync.add_route("create_people", ws_routes.create_people.create_people) WebSocketAsync.add_route("update_people", ws_routes.update_people.update_people) WebSocketAsync.add_route("update_password", ws_routes.user_profile.update_password) WebSocketAsync.add_route("list_users", ws_routes.user_profile.list_all_users) WebSocketAsync.add_route("create_user", ws_routes.user_profile.create_user) WebSocketAsync.add_route("edit_user", ws_routes.user_profile.edit_user) WebSocketAsync.add_route("remove_user", ws_routes.user_profile.remove_user) return web_app
def setUp(self): """Creates the storage and middlewares objects""" self.storage = SimpleCookieStorage() self.auth = auth.SessionTktAuthentication(self.SECRET, 15, cookie_name='auth')
def __init__(self, configFolder): operationsystem= sys.platform if operationsystem.startswith('win'): set_event_loop_policy(WindowsSelectorEventLoopPolicy()) self.path = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-1]) # The path to the package dir self.version = __version__ self.codename = __codename__ self.config_folder = configFolder self.static_config = load_config(configFolder.get_file_path("config.yaml")) logger.info("Init CraftBeerPI") policy = auth.SessionTktAuthentication(urandom(32), 60, include_ip=True) middlewares = [web.normalize_path_middleware(), session_middleware(EncryptedCookieStorage(urandom(32))), auth.auth_middleware(policy), error_middleware] # max upload size increased to 5 Mb. Default is 1 Mb -> config and svg upload self.app = web.Application(middlewares=middlewares, client_max_size=5*1024*1024) self.app["cbpi"] = self self._setup_shutdownhook() self.initializer = [] self.bus = CBPiEventBus(self.app.loop, self) self.job = JobController(self) self.config = ConfigController(self) self.ws = CBPiWebSocket(self) self.actor = ActorController(self) self.sensor = SensorController(self) self.plugin = PluginController(self) self.log = LogController(self) self.system = SystemController(self) self.kettle = KettleController(self) self.fermenter : FermentationController = FermentationController(self) self.step : StepController = StepController(self) self.recipe : RecipeController = RecipeController(self) self.fermenterrecipe : FermenterRecipeController = FermenterRecipeController(self) self.upload : UploadController = UploadController(self) self.notification : NotificationController = NotificationController(self) self.satellite = None if str(self.static_config.get("mqtt", False)).lower() == "true": self.satellite: SatelliteController = SatelliteController(self) self.dashboard = DashboardController(self) self.http_step = StepHttpEndpoints(self) self.http_recipe = RecipeHttpEndpoints(self) self.http_fermenterrecipe = FermenterRecipeHttpEndpoints(self) self.http_sensor = SensorHttpEndpoints(self) self.http_config = ConfigHttpEndpoints(self) self.http_actor = ActorHttpEndpoints(self) self.http_kettle = KettleHttpEndpoints(self) self.http_dashboard = DashBoardHttpEndpoints(self) self.http_plugin = PluginHttpEndpoints(self) self.http_system = SystemHttpEndpoints(self) self.http_log = LogHttpEndpoints(self) self.http_notification = NotificationHttpEndpoints(self) self.http_upload = UploadHttpEndpoints(self) self.http_fermenter = FermentationHttpEndpoints(self) self.login = Login(self)