def authenticate_credentials(self, key): try: token = JWTToken(key) payload = token.decode() except PyJWTError as err: raise AuthenticationFailed('Invalid token.') from err # validate request path request_path = self.request.get_full_path() if payload.check_path_params: token_path = payload.path request_path = self.strip_token_from_url(request_path) else: token_path = strip_url_params(payload.path) request_path = strip_url_params(request_path) # compare using Url class to account for acceptable variations if Url(token_path) != Url(request_path): raise AuthenticationFailed('Invalid path.') # check user user = get_user_model().objects.filter( pk=payload.user_pk).select_related('userprofile').first() if not user: raise AuthenticationFailed('Invalid user ID.') # check verification token if user.userprofile.jwt_verification_token != payload.verification_token: raise AuthenticationFailed('Invalid verification token.') return user, key
def uris(rabbit_config): amqp_uri = rabbit_config['AMQP_URI'] scheme, auth, host, port, path, _, _ = parse_url(amqp_uri) bad_port = Url(scheme, auth, host, port + 1, path).url bad_user = Url(scheme, 'invalid:invalid', host, port, path).url bad_vhost = Url(scheme, auth, host, port, '/unknown').url return { 'good': amqp_uri, 'bad_port': bad_port, 'bad_user': bad_user, 'bad_vhost': bad_vhost, }
def uris(request, rabbit_config): amqp_uri = rabbit_config['AMQP_URI'] scheme, auth, host, port, path, _, _ = parse_url(amqp_uri) bad_port = Url(scheme, auth, host, port + 1, path).url bad_user = Url(scheme, 'invalid:invalid', host, port, path).url bad_vhost = Url(scheme, auth, host, port, '/unknown').url ssl_port = request.config.getoption('AMQP_SSL_PORT') amqp_ssl_uri = Url(scheme, auth, host, ssl_port, path).url return { 'good': amqp_uri, 'bad_port': bad_port, 'bad_user': bad_user, 'bad_vhost': bad_vhost, 'ssl': amqp_ssl_uri, }
def build_url(self, path) -> Url: if not path.startswith('/'): path = self._url.path + '/' + path return Url(scheme=self._url.scheme, host=self._url.host, port=self._url.port, path=path)
def __init__(self, **custom_settings): self._ENV_PREFIX = 'SN_NETWORK_' self.GATEWAY = '0.0.0.0' self.CLIENT_URL = 'http://testrpc:8545' self.ACCOUNT_PASSWORD = Required(str) self.CLASS = 'sn_agent.network.sn.SNNetwork' self.WEB_HOST = "0.0.0.0" self.WEB_PORT = 8000 self.SSL_CERTIFICATE_FILE = None self.SSL_KEYFILE = None self.AGENT_URL_LOOKUP_FILE = os.path.join(THIS_DIR, 'data', 'agent_to_url_lookup.json') super().__init__(**custom_settings) # Must place after the init so as to pick up the proper gateway value self.WEB_URL = Url(scheme='http', host=self.GATEWAY, port=self.WEB_PORT, path='/api').url
def url_get(url): url_part = parse_url(url) UrlObj = Url(url_part.scheme, url_part.auth, url_part.host, url_part.port, url_part.path or None, url_part.query or None, url_part.fragment or None) conn = urllib3.connection_from_url(url) response = conn.request('GET', UrlObj.request_uri) return response
def service_submit(service, name, values): conf = ConfigLoader() try: cli = SlivkaClient(Url(host=conf['host'], port=int(conf['port']))) except KeyError as e: raise ClickException( "You must set \"{}\" in configuration.".format(e.args[0]) ) service = cli.get_service(service) form = service.get_form() for arg in values: k, v = arg.split('=', 1) field = form[k] if isinstance(field, IntegerField): v = int(v) elif isinstance(field, DecimalField): v = float(v) elif isinstance(field, BooleanField): v = v.lower() not in {'no', 'false', '0', 'null', 'n', 'f'} elif isinstance(field, FileField): v = cli.get_remote_file(v) form.insert({k: v}) job_id = form.submit() conf.add_task(job_id, name) conf.save() click.echo('Task {} submitted successfully.'.format(job_id))
def _ensure_endpoint(configuration): """Normalize lakefs connection endpoint found in configuration's host""" if configuration.host: try: # prefix http scheme if missing if not configuration.host.startswith( 'http://') and not configuration.host.startswith( 'https://'): configuration.host = 'http://' + configuration.host # if 'host' not set any 'path', format the endpoint url with default 'path' based on the generated code o = parse_url(configuration.host) if not o.path or o.path == '/': settings = configuration.get_host_settings() if settings: base_path = parse_url(settings[0].get('url')).path configuration.host = Url(scheme=o.scheme, auth=o.auth, host=o.host, port=o.port, path=base_path, query=o.query, fragment=o.fragment).url except ValueError: pass return configuration
def task(show_all, task_name, task_uuid, show_files): conf = ConfigLoader() try: cli = SlivkaClient(Url(host=conf['host'], port=int(conf['port']))) except KeyError as e: raise ClickException( "You must set \"{}\" in configuration.".format(e.args[0]) ) def print_task(name, job_id): state = cli.get_job_state(job_id) click.echo('{} [{}]: {}'.format(name, job_id, state.name)) if show_files: for file in cli.get_job_results(job_id): click.echo(" + {file.title} [{file.uuid}]".format(file=file)) if task_name: for name, job_id in conf.get_task_by_name(task_name): print_task(name, job_id) elif task_uuid: name, job_id = conf.get_task_by_uuid(task_uuid.hex) print_task(name, job_id) elif show_all: for name, job_id in conf.all_tasks(): print_task(name, job_id)
def correct_url(url: str, refer_url: str = '') -> str: """ 校正url: # => None javascript:xxx => None /abc.htm => http://xxx/abc.htm :param url: :param refer_url: :return: """ refer_scheme, _, refer_host, refer_port, _, _, _ = parse_url(refer_url) refer_scheme = refer_scheme if refer_scheme and refer_scheme.lower() in [ 'http', 'https' ] else None scheme, auth, host, port, path, query, fragment = parse_url(url) if not host: if not path or not refer_host: return '' else: host = refer_host port = refer_port scheme = scheme or refer_scheme or 'http' return Url(scheme=scheme, auth=auth, host=host, port=port, path=path, query=query, fragment=fragment).url
def test_bad_user(rabbit_config): scheme, auth, host, port, path, _, _ = parse_url(rabbit_config['AMQP_URI']) amqp_uri = Url(scheme, 'invalid:invalid', host, port, path).url with pytest.raises(AccessRefused) as exc_info: verify_amqp_uri(amqp_uri) message = str(exc_info.value) assert 'Login was refused' in message
def getPolicyDetail(policyid: str): path = "/getPolicy/" + policyid serviceURL = Url(scheme=urls.SA_HOST.scheme, host=urls.SA_HOST.host, port=urls.SA_HOST.port, path=path) data = callRestGet(serviceURL) return data['policy']
def test_bad_vhost(rabbit_config): scheme, auth, host, port, path, _, _ = parse_url(rabbit_config['AMQP_URI']) amqp_uri = Url(scheme, auth, host, port, '/unknown').url with pytest.raises(NotAllowed) as exc_info: verify_amqp_uri(amqp_uri) message = str(exc_info.value) assert 'access to vhost' in message
def test_bad_vhost(rabbit_config): scheme, auth, host, port, path, _, _ = parse_url(rabbit_config['AMQP_URI']) amqp_uri = Url(scheme, auth, host, port, '/unknown').url with pytest.raises(IOError) as exc_info: verify_amqp_uri(amqp_uri) message = str(exc_info.value) assert 'Error connecting to broker' in message assert 'invalid or unauthorized vhost' in message
def test_bad_user(rabbit_config): scheme, auth, host, port, path, _, _ = parse_url(rabbit_config['AMQP_URI']) amqp_uri = Url(scheme, 'invalid:invalid', host, port, path).url with pytest.raises(IOError) as exc_info: verify_amqp_uri(amqp_uri) message = str(exc_info.value) assert 'Error connecting to broker' in message assert 'invalid credentials' in message
def __init__(self): self.SCHEME = "http" self.HOSTNAME = "ipinfo.io" self.PATH = "/loc" self.COORDINATES_URL = Url(scheme=self.SCHEME, host=self.HOSTNAME, path=self.PATH) super().__init__() self.update()
def getTracker(policyid: str): path = "/getTracker/" + policyid serviceURL = Url(scheme=urls.SA_HOST.scheme, host=urls.SA_HOST.host, port=urls.SA_HOST.port, path=path) data = callRestGet(serviceURL) pprint(data) return data['tracker']
def getPolicies(): path = "/getPolicies" serviceURL = Url(scheme=urls.SA_HOST.scheme, host=urls.SA_HOST.host, port=urls.SA_HOST.port, path=path) data = callRestGet(serviceURL) policies = data['policies'] return policies
def getBrokenPolicyTrackers(policyid: str): path = "/getBrokenPolicyTrackers/" + policyid serviceURL = Url(scheme=urls.SA_HOST.scheme, host=urls.SA_HOST.host, port=urls.SA_HOST.port, path=path) data = callRestGet(serviceURL) trackers = data['trackers'] return trackers
def file(uuid, output): conf = ConfigLoader() try: cli = SlivkaClient(Url(conf['host'], int(conf['port']))) except KeyError as e: raise ClickException( "You must set \"{}\" in configuration.".format(e.args[0]) ) remote_file = cli.get_remote_file(uuid.hex) remote_file.dump(output or sys.stdout)
def _get_url(self, url: str) -> 'Url': """ Builds the url for the request Args: url: the relative endpoint for the request (e.g. /qrs/app/) Returns: the url as a Url object """ return Url(scheme=self._scheme, host=self._host, port=self._port, path=url)
def request(self, verb, url, input, headers): full_url = Url(scheme=self.__protocol, host=self.__host, port=self.__port, path=url) httpretty.register_uri(verb, full_url.url, body=self.__request_callback) self.__cnx.request(verb, url, input, headers)
def alter_url(self, path: str = None) -> Url: """Returns a url which uses this object's identifying url as basis and alters it if needed. :param path: An alternative path for the url. Defaults to this object's identifying path. :rtype: Url :returns: A new url with a possibly altered path """ if path is None: path = self._url.path return Url(self._url.scheme, host=self._url.hostname, port=self._url.port, path=path)
def get_parsed_url(parsed: Url, auth_items: Optional[List[Optional[str]]]) -> str: """ parsed: Url instance auth_items: Optional list of str ["user": "******"] returns: str e.g: http://user:[email protected]:9999 """ return Url( scheme=parsed.scheme, auth=auth_items and ":".join(auth_items) or None, host=parsed.host, port=parsed.port, path=parsed.path, query=parsed.query, fragment=parsed.fragment, ).url
def service_list(show_all, name, show_fields): conf = ConfigLoader() try: cli = SlivkaClient(Url(host=conf['host'], port=int(conf['port']))) except KeyError as e: raise ClickException( "You must set \"{}\" in configuration.".format(e.args[0]) ) if show_all: for service in cli.get_services(): click.echo(service.name) elif name: service = cli.get_service(name) click.echo(service.name) if show_fields: form = service.get_form() for field in form.fields: click.echo('{}: {}'.format(field.name, field))
def policyRemove(): try: loop = True while loop: clearScreen() policy = getPolicy('remove') policyid = policy[TCONST.ID] path = "/removeWindowPolicy/" + policyid serviceURL = Url(scheme=urls.SA_HOST.scheme, host=urls.SA_HOST.host, port=urls.SA_HOST.port, path=path) data = callRestGet(serviceURL) pprint(data) loop = question('Delete another policy?') except Exception: pass #if the policy you entered does not exist then bonus.. its deleted haha [thumbs up]
def policyAdd(): path = "/addWindowPolicy" serviceURL = Url(scheme=urls.SA_HOST.scheme, host=urls.SA_HOST.host, port=urls.SA_HOST.port, path=path) questions = [ createNumQuestion(TCONST.WIN_SIZE, 'Enter the ' + TCONST.WIN_SIZE), createNumQuestion(TCONST.NOISE_THRES, 'Enter the ' + TCONST.NOISE_THRES) ] loop = True while loop: clearScreen() answers = prompt(questions, style=custom_style_3) jsonData = json.dumps(answers) data = callRestPost(serviceURL, jsonData) pprint("Policy Added:") pprint(data) loop = question('Add another policy?')
def __init__(self, **custom_settings): self._ENV_PREFIX = 'SN_NETWORK_' self.GATEWAY = '0.0.0.0' self.CLIENT_URL = 'http://testrpc:8545' self.CLASS = 'sn_agent.network.sn.SNNetwork' self.WEB_HOST = "0.0.0.0" self.WEB_PORT = 8000 self.AGENT_URL_LOOKUP_FILE = os.path.join(THIS_DIR, 'data', 'agent_to_url_lookup.json') self.COINBASE = '0x633a490e1d3022a90e49cfb79ff8789d264ae753' super().__init__(**custom_settings) # Must place after the init so as to pick up the proper gateway value self.WEB_URL = Url(scheme='http', host=self.GATEWAY, port=self.WEB_PORT, path='/api').url
def from_url_params(cls, **kwargs): return cls(Url(**kwargs))
def test_other_error(rabbit_config): scheme, auth, host, port, path, _, _ = parse_url(rabbit_config['AMQP_URI']) amqp_uri = Url(scheme, auth, host, port + 1, path).url # closed port with pytest.raises(socket.error): verify_amqp_uri(amqp_uri)