8
8
import time
9
9
import re
10
10
import copy
11
- import typing
11
+
12
+ from typing import cast
12
13
13
14
from argparse import Namespace
14
15
from cryptography .hazmat .backends import default_backend
@@ -55,11 +56,11 @@ def __init__(
55
56
56
57
"""
57
58
if conf is None :
58
- conf = Namespace ()
59
+ conf = Namespace (no_strict = False )
59
60
self .conf = conf
60
61
self .private_key = private_key
61
- if private_key :
62
- self ._public_key = self .private_key .public_key ()
62
+ if self . _private_key :
63
+ self ._public_key = self ._private_key .public_key ()
63
64
64
65
@classmethod
65
66
def from_raw (cls , private_raw , conf : None | Namespace = None ):
@@ -109,10 +110,15 @@ def from_der(cls, private_key, conf: None | Namespace = None):
109
110
key = serialization .load_der_private_key (
110
111
b64urldecode (private_key ), password = None , backend = default_backend ()
111
112
)
112
- return cls (key , conf = conf )
113
+ if key is None :
114
+ raise VapidException ("Could not load private key" )
115
+ else :
116
+ return cls (cast (ec .EllipticCurvePrivateKey , key ), conf = conf )
113
117
114
118
@classmethod
115
- def from_file (cls , private_key_file = None , conf : None | Namespace = None ):
119
+ def from_file (
120
+ cls , private_key_file : str = "private_key.pem" , conf : None | Namespace = None
121
+ ):
116
122
"""Initialize VAPID using a file containing a private key in PEM or
117
123
DER format.
118
124
@@ -190,7 +196,7 @@ def private_key(self, value):
190
196
self ._public_key = self .private_key .public_key ()
191
197
192
198
@property
193
- def public_key (self ):
199
+ def public_key (self ) -> ec . EllipticCurvePublicKey :
194
200
"""The VAPID public ECDSA key
195
201
196
202
The public key is currently read only. Set it via the `.private_key`
@@ -200,11 +206,13 @@ def public_key(self):
200
206
:returns ec.EllipticCurvePublicKey
201
207
202
208
"""
209
+ if not self ._public_key :
210
+ raise VapidException ("Public key is undefined." )
203
211
return self ._public_key
204
212
205
213
def generate_keys (self ):
206
214
"""Generate a valid ECDSA Key Pair."""
207
- self .private_key = ec .generate_private_key (ec .SECP256R1 , default_backend ())
215
+ self .private_key = ec .generate_private_key (ec .SECP256R1 () , default_backend ())
208
216
209
217
def private_pem (self ):
210
218
return self .private_key .private_bytes (
0 commit comments