@@ -25,10 +25,13 @@ class Vapid(object):
25
25
26
26
def __init__ (self , private_key_file = None , private_key = None ):
27
27
"""Initialize VAPID using an optional file containing a private key
28
- in PEM format.
28
+ in PEM format, or a string containing the PEM formatted private key.
29
+
30
+ :param private_key_file: Name of the file containing the private key
31
+ :type private_key_file: str
32
+ :param private_key: A private key in PEM format
33
+ :type private_key: str
29
34
30
- :param private_key_file: The name of the file containing the
31
- private key
32
35
"""
33
36
if private_key_file :
34
37
if not os .path .isfile (private_key_file ):
@@ -50,31 +53,46 @@ def __init__(self, private_key_file=None, private_key=None):
50
53
51
54
@property
52
55
def private_key (self ):
53
- """Return the private key. """
56
+ """The VAPID private ECDSA key"""
54
57
if not self ._private_key :
55
58
raise VapidException (
56
59
"No private key defined. Please import or generate a key." )
57
60
return self ._private_key
58
61
59
62
@private_key .setter
60
63
def private_key (self , value ):
61
- """Set the private key."""
64
+ """Set the VAPID private ECDSA key
65
+
66
+ :param value: the byte array containing the private ECDSA key data
67
+ :type value: bytes
68
+
69
+ """
62
70
self ._private_key = value
63
71
64
72
@property
65
73
def public_key (self ):
66
- """Return the public key."""
74
+ """The VAPID public ECDSA key
75
+
76
+ The public key is currently read only. Set it via the `.private_key`
77
+ method.
78
+
79
+ """
67
80
if not self ._public_key :
68
81
self ._public_key = self .private_key .get_verifying_key ()
69
82
return self ._public_key
70
83
71
84
def generate_keys (self ):
72
85
"""Generate a valid ECDSA Key Pair."""
73
86
self .private_key = ecdsa .SigningKey .generate (curve = ecdsa .NIST256p )
74
- self .public_key
87
+ self ._public_key = self . private_key . get_verifying_key ()
75
88
76
89
def save_key (self , key_file ):
77
- """Save the private key to a PEM file."""
90
+ """Save the private key to a PEM file.
91
+
92
+ :param key_file: The file path to save the private key data
93
+ :type key_file: str
94
+
95
+ """
78
96
file = open (key_file , "wb" )
79
97
if not self ._private_key :
80
98
self .generate_keys ()
@@ -84,30 +102,52 @@ def save_key(self, key_file):
84
102
def save_public_key (self , key_file ):
85
103
"""Save the public key to a PEM file.
86
104
:param key_file: The name of the file to save the public key
105
+ :type key_file: str
106
+
87
107
"""
88
108
with open (key_file , "wb" ) as file :
89
109
file .write (self .public_key .to_pem ())
90
110
file .close ()
91
111
92
- def validate (self , token ):
93
- """Sign a Valdiation token from the dashboard"""
94
- sig = self .private_key .sign (token , hashfunc = self ._hasher )
95
- token = base64 .urlsafe_b64encode (sig )
96
- return token
112
+ def validate (self , validation_token ):
113
+ """Sign a Valdiation token from the dashboard
114
+
115
+ :param validation_token: Short validation token from the dev dashboard
116
+ :type validation_token: str
117
+ :returns: corresponding token for key verification
118
+ :rtype: str
119
+
120
+ """
121
+ sig = self .private_key .sign (validation_token , hashfunc = self ._hasher )
122
+ verification_token = base64 .urlsafe_b64encode (sig )
123
+ return verification_token
97
124
98
- def verify_token (self , sig , token ):
99
- """Verify the signature against the token."""
100
- hsig = base64 .urlsafe_b64decode (sig )
101
- return self .public_key .verify (hsig , token ,
125
+ def verify_token (self , validation_token , verification_token ):
126
+ """Internally used to verify the verification token is correct.
127
+
128
+ :param validation_token: Provided validation token string
129
+ :type validation_token: str
130
+ :param verification_token: Generated verification token
131
+ :type verification_token: str
132
+ :returns: Boolean indicating if verifictation token is valid.
133
+ :rtype: boolean
134
+
135
+ """
136
+ hsig = base64 .urlsafe_b64decode (verification_token )
137
+ return self .public_key .verify (hsig , validation_token ,
102
138
hashfunc = self ._hasher )
103
139
104
140
def sign (self , claims , crypto_key = None ):
105
141
"""Sign a set of claims.
106
142
:param claims: JSON object containing the JWT claims to use.
143
+ :type claims: dict
107
144
:param crypto_key: Optional existing crypto_key header content. The
108
145
vapid public key will be appended to this data.
109
- :returns result: a hash containing the header fields to use in
146
+ :type crypto_key: str
147
+ :returns: a hash containing the header fields to use in
110
148
the subscription update.
149
+ :rtype: dict
150
+
111
151
"""
112
152
if not claims .get ('exp' ):
113
153
claims ['exp' ] = int (time .time ()) + 86400
0 commit comments