22# License, v. 2.0. If a copy of the MPL was not distributed with this
33# file, You can obtain one at http://mozilla.org/MPL/2.0/.
44
5+ import os
6+ import logging
57import base64
68import time
79import hashlib
810
911import ecdsa
10- import logging
1112from jose import jws
1213
1314
1415class VapidException (Exception ):
16+ """An exception wrapper for Vapid."""
1517 pass
1618
1719
@@ -29,7 +31,10 @@ def __init__(self, private_key_file=None, private_key=None):
2931 private key
3032 """
3133 if private_key_file :
32- private_key = open (private_key_file ).read ()
34+ if not os .path .isfile (private_key_file ):
35+ self .save_key (private_key_file )
36+ return
37+ private_key = open (private_key_file , 'r' ).read ()
3338 if private_key :
3439 try :
3540 if "BEGIN EC" in private_key :
@@ -38,24 +43,27 @@ def __init__(self, private_key_file=None, private_key=None):
3843 self ._private_key = \
3944 ecdsa .SigningKey .from_der (
4045 base64 .urlsafe_b64decode (private_key ))
41- except Exception , exc :
46+ except Exception as exc :
4247 logging .error ("Could not open private key file: %s" , repr (exc ))
4348 raise VapidException (exc )
44- self ._pubilcKey = self ._private_key .get_verifying_key ()
49+ self ._public_key = self ._private_key .get_verifying_key ()
4550
4651 @property
4752 def private_key (self ):
53+ """Return the private key."""
4854 if not self ._private_key :
4955 raise VapidException (
5056 "No private key defined. Please import or generate a key." )
5157 return self ._private_key
5258
5359 @private_key .setter
5460 def private_key (self , value ):
61+ """Set the private key."""
5562 self ._private_key = value
5663
5764 @property
5865 def public_key (self ):
66+ """Return the public key."""
5967 if not self ._public_key :
6068 self ._public_key = self .private_key .get_verifying_key ()
6169 return self ._public_key
@@ -67,18 +75,17 @@ def generate_keys(self):
6775
6876 def save_key (self , key_file ):
6977 """Save the private key to a PEM file."""
70- file = open (key_file , "w " )
78+ file = open (key_file , "wb " )
7179 if not self ._private_key :
7280 self .generate_keys ()
7381 file .write (self ._private_key .to_pem ())
7482 file .close ()
7583
7684 def save_public_key (self , key_file ):
7785 """Save the public key to a PEM file.
78-
7986 :param key_file: The name of the file to save the public key
8087 """
81- with open (key_file , "w " ) as file :
88+ with open (key_file , "wb " ) as file :
8289 file .write (self .public_key .to_pem ())
8390 file .close ()
8491
@@ -88,14 +95,14 @@ def validate(self, token):
8895 token = base64 .urlsafe_b64encode (sig )
8996 return token
9097
91- def verifyToken (self , sig , token ):
98+ def verify_token (self , sig , token ):
99+ """Verify the signature against the token."""
92100 hsig = base64 .urlsafe_b64decode (sig )
93101 return self .public_key .verify (hsig , token ,
94102 hashfunc = self ._hasher )
95103
96104 def sign (self , claims , crypto_key = None ):
97105 """Sign a set of claims.
98-
99106 :param claims: JSON object containing the JWT claims to use.
100107 :param crypto_key: Optional existing crypto_key header content. The
101108 vapid public key will be appended to this data.
@@ -116,5 +123,5 @@ def sign(self, claims, crypto_key=None):
116123 else :
117124 crypto_key = pkey
118125
119- return {"Authorization" : "Bearer " + sig .strip ('=' ),
126+ return {"Authorization" : "WebPush " + sig .strip ('=' ),
120127 "Crypto-Key" : crypto_key }
0 commit comments