File tree Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Expand file tree Collapse file tree 2 files changed +22
-0
lines changed Original file line number Diff line number Diff line change 16
16
VERSION = "VAPID-DRAFT-02/ECE-DRAFT-07"
17
17
18
18
19
+ def b64urldecode (data ):
20
+ return base64 .urlsafe_b64decode (data + "====" [:len (data ) % 4 ])
21
+
22
+
19
23
class VapidException (Exception ):
20
24
"""An exception wrapper for Vapid."""
21
25
pass
@@ -42,6 +46,13 @@ def __init__(self, private_key=None):
42
46
"""
43
47
self .private_key = private_key
44
48
49
+ @classmethod
50
+ def from_raw (cls , private_key ):
51
+ key = ecdsa .SigningKey .from_string (b64urldecode (private_key ),
52
+ curve = cls ._curve ,
53
+ hashfunc = cls ._hasher )
54
+ return cls (key )
55
+
45
56
@classmethod
46
57
def from_pem (cls , private_key ):
47
58
"""Initialize VAPID using a private key in PEM format.
Original file line number Diff line number Diff line change 21
21
T_PRIVATE = ("-----BEGIN EC PRIVATE KEY-----{}"
22
22
"-----END EC PRIVATE KEY-----\n " ).format (T_DER )
23
23
24
+ # This is the same private key, as a point in uncompressed form. This should
25
+ # be Base64url-encoded without padding.
26
+ T_RAW = """
27
+ 943WICKkdu3z78pnY0gXw143biOoCacwsVkQyhxjxFs
28
+ """
29
+
24
30
# This is a public key in PEM form.
25
31
T_PUBLIC = """-----BEGIN PUBLIC KEY-----
26
32
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEJwJZq/GN8jJbo1GGpyU70hmP2hb
@@ -104,6 +110,11 @@ def test_same_public_key(self):
104
110
v .save_public_key ("/tmp/p2" )
105
111
os .unlink ("/tmp/p2" )
106
112
113
+ def test_from_raw (self ):
114
+ v = Vapid01 .from_raw (T_RAW )
115
+ eq_ (v .private_key .to_pem (), T_PRIVATE .encode ('utf8' ))
116
+ eq_ (v .public_key .to_pem (), T_PUBLIC .encode ('utf8' ))
117
+
107
118
def test_validate (self ):
108
119
v = Vapid01 .from_file ("/tmp/private" )
109
120
msg = "foobar" .encode ('utf8' )
You can’t perform that action at this time.
0 commit comments