1212def main ():
1313 parser = argparse .ArgumentParser (description = "VAPID tool" )
1414 parser .add_argument ('--sign' , '-s' , help = 'claims file to sign' )
15+ parser .add_argument ('--gen' , '-g' , help = 'generate new key pairs' ,
16+ default = False , action = "store_true" )
1517 parser .add_argument ('--validate' , '-v' , help = 'dashboard token to validate' )
1618 parser .add_argument ('--version2' , '-2' , help = "use VAPID spec Draft-02" ,
1719 default = False , action = "store_true" )
1820 parser .add_argument ('--version1' , '-1' , help = "use VAPID spec Draft-01" ,
1921 default = True , action = "store_true" )
22+ parser .add_argument ('--json' , help = "dump as json" ,
23+ default = False , action = "store_true" )
2024 args = parser .parse_args ()
2125 Vapid = Vapid01
2226 if args .version2 :
2327 Vapid = Vapid02
24- if not os .path .exists ('private_key.pem' ):
25- print "No private_key.pem file found."
26- answer = None
27- while answer not in [ 'y' , 'n' ]:
28- answer = raw_input ( "Do you want me to create one for you? (Y/n)" )
29- if not answer :
30- answer = 'y'
31- answer = answer . lower ()[ 0 ]
32- if answer == 'n' :
33- print "Sorry, can't do much for you then."
34- exit
35- if answer == 'y' :
36- break
28+ if args . gen or not os .path .exists ('private_key.pem' ):
29+ if not args . gen :
30+ print ( "No private_key.pem file found." )
31+ answer = None
32+ while answer not in [ 'y' , 'n' ]:
33+ answer = input ( "Do you want me to create one for you? (Y/n)" )
34+ if not answer :
35+ answer = 'y'
36+ answer = answer . lower ()[ 0 ]
37+ if answer == 'n' :
38+ print ( "Sorry, can't do much for you then." )
39+ exit
40+ print ( "Generating private_key.pem" )
3741 Vapid ().save_key ('private_key.pem' )
3842 vapid = Vapid ('private_key.pem' )
39- if not os .path .exists ('public_key.pem' ):
40- print "No public_key.pem file found. You'll need this to access "
41- print "the developer dashboard."
42- answer = None
43- while answer not in ['y' , 'n' ]:
44- answer = raw_input ("Do you want me to create one for you? (Y/n)" )
45- if not answer :
46- answer = 'y'
47- answer = answer .lower ()[0 ]
48- if answer == 'y' :
49- vapid .save_public_key ('public_key.pem' )
43+ if args .gen or not os .path .exists ('public_key.pem' ):
44+ if not args .gen :
45+ print ("No public_key.pem file found. You'll need this to access "
46+ "the developer dashboard." )
47+ answer = None
48+ while answer not in ['y' , 'n' ]:
49+ answer = input ("Do you want me to create one for you? (Y/n)" )
50+ if not answer :
51+ answer = 'y'
52+ answer = answer .lower ()[0 ]
53+ if answer == 'n' :
54+ print ("Exiting..." )
55+ exit
56+ print ("Generating public_key.pem" )
57+ vapid .save_public_key ('public_key.pem' )
5058 claim_file = args .sign
5159 if claim_file :
5260 if not os .path .exists (claim_file ):
53- print "No %s file found." % claim_file
54- print """
61+ print ( "No {} file found." . format ( claim_file ))
62+ print ( """
5563The claims file should be a JSON formatted file that holds the
5664information that describes you. There are three elements in the claims
5765file you'll need:
@@ -70,25 +78,27 @@ def main():
7078For example, a claims.json file could contain:
7179
7280{"sub": "mailto:[email protected] "} 73- """
81+ """ )
7482 exit
7583 try :
7684 claims = json .loads (open (claim_file ).read ())
7785 result = vapid .sign (claims )
78- except Exception , exc :
79- print "Crap, something went wrong: %s" , repr (exc )
86+ except Exception as exc :
87+ print ( "Crap, something went wrong: {}" . format ( repr (exc )) )
8088 raise exc
81-
82- print "Include the following headers in your request:\n "
89+ if args .json :
90+ print (json .dumps (result ))
91+ return
92+ print ("Include the following headers in your request:\n " )
8393 for key , value in result .items ():
84- print "%s: %s" % (key , value )
85- print "\n "
94+ print ( "{}: {} \n " . format (key , value ) )
95+ print ( "\n " )
8696
8797 token = args .validate
8898 if token :
89- print "signed token for dashboard validation:\n "
90- print vapid .validate (token )
91- print "\n "
99+ print ( "signed token for dashboard validation:\n " )
100+ print ( vapid .validate (token ) )
101+ print ( "\n " )
92102
93103
94104if __name__ == '__main__' :
0 commit comments