async def get_client(cls, key_name=None): api_key_name = key_name or API_KEY_NAME pw_path = os.path.join(conf.settings['data_dir'], ".api_keys") keys = load_api_keys(pw_path) api_key = keys.get(api_key_name, False) login_url = "http://{}:{}@{}:{}".format(api_key_name, api_key.secret, conf.settings['api_host'], conf.settings['api_port']) url = urlparse(login_url) headers = { 'Host': url.hostname, 'User-Agent': USER_AGENT, 'Content-type': 'application/json' } session = aiohttp.ClientSession() async with session.post(login_url, headers=headers) as r: cookies = r.cookies uid = cookies.get(TWISTED_SESSION).value api_key = APIKey.new(seed=uid.encode()) return cls(api_key, session, cookies, url, login_url)
def config(cls, key_name=None, key=None, pw_path=None, timeout=HTTP_TIMEOUT, connection=None, count=0, cookies=None, auth=None, url=None, login_url=None): api_key_name = API_KEY_NAME if not key_name else key_name pw_path = os.path.join(conf.settings['data_dir'], ".api_keys") if not pw_path else pw_path if not key: keys = load_api_keys(pw_path) api_key = keys.get(api_key_name, False) else: api_key = APIKey(name=api_key_name, secret=key) if login_url is None: service_url = "http://%s:%s@%s:%i/%s" % (api_key_name, api_key.secret, conf.settings['api_host'], conf.settings['api_port'], conf.settings['API_ADDRESS']) else: service_url = login_url id_count = count if auth is None and connection is None and cookies is None and url is None: # This is a new client instance, initialize the auth header and start a session url = urlparse.urlparse(service_url) (user, passwd) = (url.username, url.password) try: user = user.encode('utf8') except AttributeError: pass try: passwd = passwd.encode('utf8') except AttributeError: pass authpair = user + b':' + passwd auth_header = b'Basic ' + base64.b64encode(authpair) conn = requests.Session() conn.auth = (user, passwd) req = requests.Request(method='POST', url=service_url, auth=conn.auth, headers={'Host': url.hostname, 'User-Agent': USER_AGENT, 'Authorization': auth_header, 'Content-type': 'application/json'},) r = req.prepare() http_response = conn.send(r) cookies = http_response.cookies uid = cookies.get(TWISTED_SESSION) api_key = APIKey.new(seed=uid) else: # This is a client that already has a session, use it auth_header = auth conn = connection assert cookies.get(LBRY_SECRET, False), "Missing cookie" secret = cookies.get(LBRY_SECRET) api_key = APIKey(secret, api_key_name) return cls(api_key, timeout, conn, id_count, cookies, auth_header, url, service_url)
def initialize_api_key_file(key_path): context = service.getApplicationContext() secret = lbrynet_utils.loadApiSecret(context, ks) if secret is None: keys = {} new_api_key = APIKey.new(name=API_KEY_NAME) keys.update({new_api_key.name: new_api_key}) save_api_keys(keys, key_path)
def _register_user_session(self, session_id): """ Add or update a HMAC secret for a session @param session_id: @return: secret """ log.info("Register api session") token = APIKey.new(seed=session_id) self.sessions.update({session_id: token})
def _update_session_secret(self, session_id): self.sessions.update({session_id: APIKey.new(name=session_id)})