2
2
# License, v. 2.0. If a copy of the MPL was not distributed with this
3
3
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
4
5
+ import os
6
+ import logging
5
7
import base64
6
8
import time
7
9
import hashlib
8
10
9
11
import ecdsa
10
- import logging
11
12
from jose import jws
12
13
13
14
14
15
class VapidException (Exception ):
16
+ """An exception wrapper for Vapid."""
15
17
pass
16
18
17
19
@@ -29,7 +31,10 @@ def __init__(self, private_key_file=None, private_key=None):
29
31
private key
30
32
"""
31
33
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 ()
33
38
if private_key :
34
39
try :
35
40
if "BEGIN EC" in private_key :
@@ -38,24 +43,27 @@ def __init__(self, private_key_file=None, private_key=None):
38
43
self ._private_key = \
39
44
ecdsa .SigningKey .from_der (
40
45
base64 .urlsafe_b64decode (private_key ))
41
- except Exception , exc :
46
+ except Exception as exc :
42
47
logging .error ("Could not open private key file: %s" , repr (exc ))
43
48
raise VapidException (exc )
44
- self ._pubilcKey = self ._private_key .get_verifying_key ()
49
+ self ._public_key = self ._private_key .get_verifying_key ()
45
50
46
51
@property
47
52
def private_key (self ):
53
+ """Return the private key."""
48
54
if not self ._private_key :
49
55
raise VapidException (
50
56
"No private key defined. Please import or generate a key." )
51
57
return self ._private_key
52
58
53
59
@private_key .setter
54
60
def private_key (self , value ):
61
+ """Set the private key."""
55
62
self ._private_key = value
56
63
57
64
@property
58
65
def public_key (self ):
66
+ """Return the public key."""
59
67
if not self ._public_key :
60
68
self ._public_key = self .private_key .get_verifying_key ()
61
69
return self ._public_key
@@ -67,18 +75,17 @@ def generate_keys(self):
67
75
68
76
def save_key (self , key_file ):
69
77
"""Save the private key to a PEM file."""
70
- file = open (key_file , "w " )
78
+ file = open (key_file , "wb " )
71
79
if not self ._private_key :
72
80
self .generate_keys ()
73
81
file .write (self ._private_key .to_pem ())
74
82
file .close ()
75
83
76
84
def save_public_key (self , key_file ):
77
85
"""Save the public key to a PEM file.
78
-
79
86
:param key_file: The name of the file to save the public key
80
87
"""
81
- with open (key_file , "w " ) as file :
88
+ with open (key_file , "wb " ) as file :
82
89
file .write (self .public_key .to_pem ())
83
90
file .close ()
84
91
@@ -88,14 +95,14 @@ def validate(self, token):
88
95
token = base64 .urlsafe_b64encode (sig )
89
96
return token
90
97
91
- def verifyToken (self , sig , token ):
98
+ def verify_token (self , sig , token ):
99
+ """Verify the signature against the token."""
92
100
hsig = base64 .urlsafe_b64decode (sig )
93
101
return self .public_key .verify (hsig , token ,
94
102
hashfunc = self ._hasher )
95
103
96
104
def sign (self , claims , crypto_key = None ):
97
105
"""Sign a set of claims.
98
-
99
106
:param claims: JSON object containing the JWT claims to use.
100
107
:param crypto_key: Optional existing crypto_key header content. The
101
108
vapid public key will be appended to this data.
@@ -116,5 +123,5 @@ def sign(self, claims, crypto_key=None):
116
123
else :
117
124
crypto_key = pkey
118
125
119
- return {"Authorization" : "Bearer " + sig .strip ('=' ),
126
+ return {"Authorization" : "WebPush " + sig .strip ('=' ),
120
127
"Crypto-Key" : crypto_key }
0 commit comments