async def test_basic_auth_white_path(test_client): async def handler(request): return web.Response() app = web.Application() app.router.add_get('/', handler) await _setup(app, BasicAuth('user', 'pass', 'realm', white_paths=['/'])) cl = await test_client(app) resp = await cl.get('/') assert resp.status == 200
async def test_basic_auth_ok(aiohttp_client): async def handler(request): return web.Response() app = web.Application() app.router.add_get('/', handler) await _setup(app, BasicAuth('user', 'pass', 'realm')) cl = await aiohttp_client(app) resp = await cl.get('/', auth=aiohttp.BasicAuth('user', 'pass')) assert resp.status == 200
async def test_basic_auth_malformed_req2(test_client): async def handler(request): return web.Response() app = web.Application() app.router.add_get('/', handler) await _setup(app, BasicAuth('user', 'pass', 'realm')) cl = await test_client(app) resp = await cl.get('/', headers={'Authorization': 'Basic nonbase64'}) assert resp.status == 401 assert resp.headers['WWW-Authenticate'] == 'Basic realm=realm'
async def test_basic_auth_wrong_creds(test_client): async def handler(request): return web.Response() app = web.Application() app.router.add_get('/', handler) await _setup(app, BasicAuth('user', 'pass', 'realm')) cl = await test_client(app) resp = await cl.get('/', auth=aiohttp.BasicAuth('user', 'badpass')) assert resp.status == 401 assert resp.headers['WWW-Authenticate'] == 'Basic realm=realm'
async def test_basic_auth_malformed_req3(test_client): async def handler(request): return web.Response() app = web.Application() app.router.add_get('/', handler) await _setup(app, BasicAuth('user', 'pass', 'realm')) cl = await test_client(app) creds = base64.encodestring(b'a:b:c').decode('utf-8') resp = await cl.get('/', headers={'Authorization': 'Basic ' + creds}) assert resp.status == 401 assert resp.headers['WWW-Authenticate'] == 'Basic realm=realm'
async def init(loop): conf = load_config(PROJ_ROOT / 'config' / 'config.yml') app = web.Application(loop=loop) app.router.add_route('GET', "/api/v2/{item}", method) app.router.add_route('GET', "/api/{item}", method) cache = Cache(plugins=[HitMissRatioPlugin(), TimingPlugin()]) print(conf['allowed_items']) if 'host' in conf: host = conf['host'] else: host = '127.0.0.1' if 'port' in conf: port = conf['port'] else: port = '443' if 'access_log_format' in conf: access_log_format = conf['access_log_format'] else: access_log_format = '%a %t "%r" %s %b "%{Referer}i" "%{User-Agent}i"' if 'scheme' in conf: if conf['scheme'] == 'https': if 'sslcertchain' and 'sslkey' in conf: ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ssl_context.load_verify_locations(conf['sslcertchain'], conf['sslkey']) ssl_context.load_cert_chain(conf['sslcertchain'], conf['sslkey']) else: raise NameError( 'sslcertchain / sslkey missing in the configuration') else: ssl_context = None else: ssl_context = None app['config'] = conf user, password, realm = conf['authentication']['user'], conf[ 'authentication']['password'], conf['authentication']['realm'] await setup(app, AllowedHosts(conf['allowed_hosts']), BasicAuth(user, password, realm)) app['cache'] = cache return app, cache, host, port, access_log_format, ssl_context
async def cache(): conf = load_config(PROJ_ROOT / 'config' / 'config-gunicorn.yml') logging.basicConfig(level=logging.DEBUG) app = web.Application() app.router.add_route('GET', "/api/v2/{item}", method) app.router.add_route('GET', "/api/v2/{item}/{domain}", method) app.router.add_route('GET', "/api/{item}", method) app.router.add_route('GET', "/api/{item}/{domain}", method) memcached_host = conf['cache']['memcached_host'] memcached_port = conf['cache']['memcached_port'] #cache = Cache(plugins=[HitMissRatioPlugin(), TimingPlugin()]) lookup_type = {} cache = Cache(Cache.MEMCACHED, endpoint=memcached_host, port=memcached_port, serializer=JsonSerializer(), plugins=[HitMissRatioPlugin(), TimingPlugin()]) if 'statsd' in conf: if conf['statsd']['enable']: hostname = socket.gethostname().split('.', 1)[0] c = statsd.StatsClient(conf['statsd']['host'], conf['statsd']['port'], prefix=conf['statsd']['prefix']) t = MetricsTimer(conf['statsd']['interval'], cache_metrics, cache, lookup_type, c, hostname) app['config'] = conf user, password, realm = conf['authentication']['user'], conf[ 'authentication']['password'], conf['authentication']['realm'] await setup(app, AllowedHosts(conf['allowed_hosts']), BasicAuth(user, password, realm)) app['cache'] = cache app['lookup_type'] = lookup_type return app
def __init__(self, loop, syncer, config): self.host = config['rpc']['host'] self.port = config['rpc']['port'] self.loop = loop self.syncer = syncer self.global_message_queue = self.syncer.queues['RPCManager'] #self.allowed_clients self.requests = {} self.up = True self.logger = logging.getLogger("RPCManager") default_log_level = logging.INFO if "logging" in config: #debug, info, warning, error, critical loglevels = { "debug": logging.DEBUG, "info": logging.INFO, "warning": logging.WARNING, "error": logging.ERROR, "critical": logging.CRITICAL } if "base" in config["logging"] and config["logging"][ "base"] in loglevels: self.logger.setLevel(loglevels[config["logging"]["base"]]) if "rpc" in config["logging"] and config["logging"][ "rpc"] in loglevels: #its ok to rewrite self.logger.setLevel(loglevels[config["logging"]["rpc"]]) rpc_manager_location = __file__ web_wallet_dir = join( path_split(rpc_manager_location)[0], "web_wallet") self.app = web.Application(loop=self.loop) self.loop.run_until_complete( setup( self.app, BasicAuth(config['rpc']['login'], config['rpc']['password'], "realm", white_paths=['/']))) self.app.router.add_route('*', '/rpc', self.handle) self.app.router.add_route('*', '/', self.handle_root) self.app.router.add_static('/', web_wallet_dir) self.server = self.loop.create_server(self.app.make_handler(), self.host, self.port) asyncio.ensure_future(self.server, loop=loop) asyncio.ensure_future(self.check_queue()) methods.add(self.ping) methods.add(self.getconnectioncount) methods.add(self.getheight) methods.add(self.getblocktemplate) methods.add(self.getwork) methods.add(self.submitwork) methods.add(self.validatesolution) methods.add(self.getbalancestats) methods.add(self.getbalancelist) methods.add(self.getbalance) methods.add(self.sendtoaddress) methods.add(self.getnewaddress) methods.add(self.dumpprivkey) methods.add(self.importprivkey) methods.add(self.getsyncstatus) methods.add(self.getblock) methods.add(self.getnodes) methods.add(self.connecttonode) methods.add(self.gettransactions) methods.add(self.getversion) methods.add(self.shutdown) methods.add(self.eth_getWork) methods.add(self.eth_submitWork) methods.add(self.eth_getBlockByNumber) self.no_auth_methods = [ "eth_getWork", "eth_submitWork", "eth_getBlockByNumber" ]
result = {} if request.match_info['item']: if int(request.rel_url.query['per_page']) == 1: if request.match_info['item'] == 'fact_values': with open('hosts-perpage1-facts.json') as json_file: result = json.load(json_file) else: with open('hosts-perpage1.json') as json_file: result = json.load(json_file) else: await asyncio.sleep(randrange(0, 20)) with open('hosts-perpagefull.json') as json_file: result = json.load(json_file) #return web.json_response(await time()) #return web.Response(text=str(result)) return web.json_response(result) if __name__ == '__main__': app = web.Application() app.router.add_route('GET', "/api/v2/{item}", method) app.router.add_route('GET', "/api/{item}", method) logging.basicConfig(level=logging.DEBUG) ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) ssl_context.load_verify_locations('../ssl/foreman_cache-localhost.crt', '../ssl/foreman_cache-localhost.key') ssl_context.load_cert_chain('../ssl/foreman_cache-localhost.crt', '../ssl/foreman_cache-localhost.key') setup(app, BasicAuth('foreman', 'foreman', 'fback')) web.run_app(app, host='localhost', port=8233, ssl_context=ssl_context)
def test_props(): m = BasicAuth('user', 'pass', 'realm', white_paths=['/path/to']) assert m.username == 'user' assert m.password == 'pass' assert m.realm == 'realm' assert m.white_paths == {'/path/to'}