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 _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 config(cls, key_name=None, key=None, pw_path=None, timeout=HTTP_TIMEOUT, connection=None, count=0, service=None, 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(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, settings.API_INTERFACE, settings.api_port, 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, service, cookies, auth_header, url, service_url)
def _update_session_secret(self, session_id): # log.info("Generating new token for next request") self.sessions.update({session_id: APIKey.new(name=session_id)})
def _update_session_secret(self, session_id): self.sessions.update({session_id: APIKey.new(name=session_id)})