def __init__(self, db_name="subscriptors.db", verbose=False): """ Class constructor. :param db_name: The [optional] name ("subscriptors.db" by default) of the file in which subscriptions will be stored in. This is only required if methods like ``newSubscription`` will be used. :type db_name: str :param verbose: An optional value, to enabled or disabled the "verbose mode" (False by default) :type verbose: bool """ self.__verbose__ = verbose self.__db_name__ = db_name if not os.path.exists('private_key.pem'): self.__print__("No private_key.pem file found") Vapid().save_key('private_key.pem') self.__print__("private_key.pem file created") self.__vapid__ = Vapid('private_key.pem') if not os.path.exists('public_key.pem'): self.__print__("No public_key.pem file found") self.__vapid__.save_public_key('public_key.pem') self.__print__("public_key.pem file created") if verbose: self.__print__("PublicKey: %s" % self.getB64PublicKey())
def __init__(self, args, loop, tasks: List[List[Union[AnyStr, Dict]]]=None): self.config = args self.loop = loop self.connection = None self.pushEndpoint = None self.channelID = None self.notifications = [] self.uaid = None self.recv = [] self.tasks = tasks or [] self.output = None self.vapid_cache = {} if args.vapid_key: self.vapid = Vapid().from_file(args.vapid_key) else: self.vapid = Vapid() self.vapid.generate_keys() self.tls_conn = None if args.partner_endpoint_cert: if os.path.isfile(args.partner_endpoint_cert): context = ssl.create_default_context( cafile=args.partner_endpoint_cert) else: context = ssl.create_default_context( cadata=args.partner_endpoint_cert) context.verify_mode = ssl.CERT_REQUIRED context.check_hostname = False self.tls_conn = aiohttp.TCPConnector(ssl_context=context)
def __init__(self, load_runner, websocket_url, statsd_client, scenario, endpoint=None, endpoint_ssl_cert=None, endpoint_ssl_key=None, *scenario_args, **scenario_kw): logging.debug("Connecting to {}".format(websocket_url)) self._factory = WebSocketClientFactory( websocket_url, headers={"Origin": "http://localhost:9000"}) self._factory.protocol = WSClientProtocol self._factory.harness = self if websocket_url.startswith("wss"): self._factory_context = ssl.ClientContextFactory() else: self._factory_context = None # somewhat bogus encryption headers self._crypto_key = "keyid=p256dh;dh=c2VuZGVy" self._encryption = "keyid=p256dh;salt=XZwpw6o37R-6qoZjw6KwAw" # Processor and Websocket client vars self._scenario = scenario self._scenario_args = scenario_args self._scenario_kw = scenario_kw self._processors = 0 self._ws_clients = {} self._connect_waiters = deque() self._load_runner = load_runner self._stat_client = statsd_client self._vapid = Vapid() if "vapid_private_key" in self._scenario_kw: self._vapid = Vapid( private_key=self._scenario_kw.get("vapid_private_key")) else: self._vapid.generate_keys() self._claims = () if "vapid_claims" in self._scenario_kw: self._claims = self._scenario_kw.get("vapid_claims") self._endpoint = urlparse.urlparse(endpoint) if endpoint else None self._agent = None if endpoint_ssl_cert: self._agent = Agent(reactor, contextFactory=UnverifiedHTTPS( endpoint_ssl_cert, endpoint_ssl_key)) if hasattr(endpoint_ssl_cert, 'seek'): endpoint_ssl_cert.seek(0) if endpoint_ssl_key and hasattr(endpoint_ssl_key, 'seek'): endpoint_ssl_key.seek(0)
def gen_application_server_keys(): """ Generate Vapid key pair """ vapid = Vapid() vapid.generate_keys() vapid.save_key(settings.VAPID_PRIVATE_KEY) vapid.save_public_key(settings.VAPID_PUBLIC_KEY)
def test_init(self): args = TrialSettings() args.vapid_key = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex) vapid = Vapid() vapid.generate_keys() vapid.save_key(args.vapid_key) client = PushClient(loop=self.loop, args=args) assert (client.vapid.public_key.public_numbers().encode_point == vapid.public_key.public_numbers().encode_point) os.unlink(args.vapid_key)
def main(): parser = argparse.ArgumentParser(description="VAPID tool") parser.add_argument('--sign', '-s', help='claims file to sign') parser.add_argument('--validate', '-v', help='dashboard token to validate') args = parser.parse_args() if not os.path.exists('private_key.pem'): print "No private_key.pem file found." answer = None while answer not in ['y', 'n']: answer = raw_input("Do you want me to create one for you? (Y/n)") if not answer: answer = 'y' answer = answer.lower()[0] if answer == 'n': print "Sorry, can't do much for you then." exit if answer == 'y': break Vapid().save_key('private_key.pem') vapid = Vapid('private_key.pem') if not os.path.exists('public_key.pem'): print "No public_key.pem file found. You'll need this to access " print "the developer dashboard." answer = None while answer not in ['y', 'n']: answer = raw_input("Do you want me to create one for you? (Y/n)") if not answer: answer = 'y' answer = answer.lower()[0] if answer == 'y': vapid.save_public_key('public_key.pem') claim_file = args.sign if claim_file: if not os.path.exists(claim_file): print "No %s file found." % claim_file print """ The claims file should be a JSON formatted file that holds the information that describes you. There are three elements in the claims file you'll need: "sub" This is your site's admin email address (e.g. "mailto:[email protected]") "exp" This is the expiration time for the claim in seconds. If you don't have one, I'll add one that expires in 24 hours. You're also welcome to add additional fields to the claims which could be helpful for the Push Service operations team to pass along to your operations team (e.g. "ami-id": "e-123456", "cust-id": "a3sfa10987"). Remember to keep these values short to prevent some servers from rejecting the transaction due to overly large headers. See https://jwt.io/introduction/ for details. For example, a claims.json file could contain: {"sub": "mailto:[email protected]"} """ exit try: claims = json.loads(open(claim_file).read()) result = vapid.sign(claims) except Exception, exc: print "Crap, something went wrong: %s", repr(exc) raise exc print "Include the following headers in your request:\n" for key, value in result.items(): print "%s: %s" % (key, value) print "\n"