99from py_vapid import Vapid01 , Vapid02 , b64urlencode
1010
1111
12+ def prompt (prompt ):
13+ # Not sure why, but python3 throws and exception if you try to
14+ # monkeypatch for this. It's ugly, but this seems to play nicer.
15+ try :
16+ return input (prompt )
17+ except NameError :
18+ return raw_input (prompt ) # noqa: F821
19+
20+
1221def main ():
1322 parser = argparse .ArgumentParser (description = "VAPID tool" )
1423 parser .add_argument ('--sign' , '-s' , help = 'claims file to sign' )
1524 parser .add_argument ('--gen' , '-g' , help = 'generate new key pairs' ,
1625 default = False , action = "store_true" )
17- parser .add_argument ('--validate' , '-v' , help = 'dashboard token to validate' )
1826 parser .add_argument ('--version2' , '-2' , help = "use VAPID spec Draft-02" ,
1927 default = False , action = "store_true" )
2028 parser .add_argument ('--version1' , '-1' , help = "use VAPID spec Draft-01" ,
@@ -27,10 +35,6 @@ def main():
2735 args = parser .parse_args ()
2836
2937 # Added to solve 2.7 => 3.* incompatibility
30- try :
31- input = raw_input
32- except NameError :
33- pass
3438 Vapid = Vapid01
3539 if args .version2 :
3640 Vapid = Vapid02
@@ -39,38 +43,26 @@ def main():
3943 print ("No private_key.pem file found." )
4044 answer = None
4145 while answer not in ['y' , 'n' ]:
42- answer = input ("Do you want me to create one for you? "
43- "(Y/n)" )
46+ answer = prompt ("Do you want me to create one for you? (Y/n)" )
4447 if not answer :
4548 answer = 'y'
4649 answer = answer .lower ()[0 ]
4750 if answer == 'n' :
4851 print ("Sorry, can't do much for you then." )
4952 exit (1 )
53+ vapid = Vapid ()
54+ vapid .generate_keys ()
5055 print ("Generating private_key.pem" )
51- Vapid ().save_key ('private_key.pem' )
52- vapid = Vapid .from_file ('private_key.pem' )
53- if args .gen or not os .path .exists ('public_key.pem' ):
54- if not args .gen :
55- print ("No public_key.pem file found. You'll need this to access "
56- "the developer dashboard." )
57- answer = None
58- while answer not in ['y' , 'n' ]:
59- answer = input ("Do you want me to create one for you? "
60- "(Y/n)" )
61- if not answer :
62- answer = 'y'
63- answer = answer .lower ()[0 ]
64- if answer == 'n' :
65- print ("Exiting..." )
66- exit (0 )
56+ vapid .save_key ('private_key.pem' )
6757 print ("Generating public_key.pem" )
6858 vapid .save_public_key ('public_key.pem' )
59+ vapid = Vapid .from_file ('private_key.pem' )
6960 claim_file = args .sign
7061 result = dict ()
7162 if args .applicationServerKey :
63+ raw_pub = vapid .public_key .public_numbers ().encode_point ()
7264 print ("Application Server Key = {}\n \n " .format (
73- b64urlencode (vapid . public_key . to_string () )))
65+ b64urlencode (raw_pub )))
7466 if claim_file :
7567 if not os .path .exists (claim_file ):
7668 print ("No {} file found." .format (claim_file ))
0 commit comments