10
10
from jose import jws
11
11
from py_vapid import Vapid01 , Vapid02 , VapidException
12
12
13
+ # This is a private key in DER form.
13
14
T_DER = """
14
15
MHcCAQEEIPeN1iAipHbt8+/KZ2NIF8NeN24jqAmnMLFZEMocY8RboAoGCCqGSM49
15
16
AwEHoUQDQgAEEJwJZq/GN8jJbo1GGpyU70hmP2hbWAUpQFKDByKB81yldJ9GTklB
16
17
M5xqEwuPM7VuQcyiLDhvovthPIXx+gsQRQ==
17
18
"""
19
+
20
+ # This is the same private key, in PEM form.
18
21
T_PRIVATE = ("-----BEGIN EC PRIVATE KEY-----{}"
19
22
"-----END EC PRIVATE KEY-----\n " ).format (T_DER )
20
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
+
30
+ # This is a public key in PEM form.
21
31
T_PUBLIC = """-----BEGIN PUBLIC KEY-----
22
32
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEEJwJZq/GN8jJbo1GGpyU70hmP2hb
23
33
WAUpQFKDByKB81yldJ9GTklBM5xqEwuPM7VuQcyiLDhvovthPIXx+gsQRQ==
24
34
-----END PUBLIC KEY-----
25
35
"""
26
36
27
- # this is a DER RAW key ('\x04' + 2 32 octet digits )
37
+ # this is a public key in uncompressed form ('\x04' + 2 * 32 octets )
28
38
# Remember, this should have any padding stripped.
29
39
T_PUBLIC_RAW = (
30
40
"BBCcCWavxjfIyW6NRhqclO9IZj9oW1gFKUBSgwcigfNc"
33
43
34
44
35
45
def setUp (self ):
36
- ff = open ('/tmp/private' , 'w' )
37
- ff .write (T_PRIVATE )
38
- ff . close ()
39
- ff = open ( '/tmp/public' , 'w' )
40
- ff . write ( T_PUBLIC )
41
- ff .close ( )
46
+ with open ('/tmp/private' , 'w' ) as ff :
47
+ ff .write (T_PRIVATE )
48
+ with open ( '/tmp/public' , 'w' ) as ff :
49
+ ff . write ( T_PUBLIC )
50
+ with open ( '/tmp/private.der' , 'w' ) as ff :
51
+ ff .write ( T_DER )
42
52
43
53
44
54
def tearDown (self ):
@@ -48,17 +58,20 @@ def tearDown(self):
48
58
49
59
class VapidTestCase (unittest .TestCase ):
50
60
def test_init (self ):
51
- v1 = Vapid01 ( private_key_file = "/tmp/private" )
61
+ v1 = Vapid01 . from_file ( "/tmp/private" )
52
62
eq_ (v1 .private_key .to_pem (), T_PRIVATE .encode ('utf8' ))
53
63
eq_ (v1 .public_key .to_pem (), T_PUBLIC .encode ('utf8' ))
54
- v2 = Vapid01 ( private_key = T_PRIVATE )
64
+ v2 = Vapid01 . from_pem ( T_PRIVATE )
55
65
eq_ (v2 .private_key .to_pem (), T_PRIVATE .encode ('utf8' ))
56
66
eq_ (v2 .public_key .to_pem (), T_PUBLIC .encode ('utf8' ))
57
- v3 = Vapid01 ( private_key = T_DER )
67
+ v3 = Vapid01 . from_der ( T_DER )
58
68
eq_ (v3 .private_key .to_pem (), T_PRIVATE .encode ('utf8' ))
59
69
eq_ (v3 .public_key .to_pem (), T_PUBLIC .encode ('utf8' ))
70
+ v4 = Vapid01 .from_file ("/tmp/private.der" )
71
+ eq_ (v4 .private_key .to_pem (), T_PRIVATE .encode ('utf8' ))
72
+ eq_ (v4 .public_key .to_pem (), T_PUBLIC .encode ('utf8' ))
60
73
no_exist = '/tmp/not_exist'
61
- Vapid01 ( private_key_file = no_exist )
74
+ Vapid01 . from_file ( no_exist )
62
75
ok_ (os .path .isfile (no_exist ))
63
76
os .unlink (no_exist )
64
77
@@ -68,7 +81,7 @@ def repad(self, data):
68
81
@patch ("ecdsa.SigningKey.from_pem" , side_effect = Exception )
69
82
def test_init_bad_priv (self , mm ):
70
83
self .assertRaises (Exception ,
71
- Vapid01 ,
84
+ Vapid01 . from_file ,
72
85
private_key_file = "/tmp/private" )
73
86
74
87
def test_private (self ):
@@ -97,8 +110,13 @@ def test_same_public_key(self):
97
110
v .save_public_key ("/tmp/p2" )
98
111
os .unlink ("/tmp/p2" )
99
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
+
100
118
def test_validate (self ):
101
- v = Vapid01 ("/tmp/private" )
119
+ v = Vapid01 . from_file ("/tmp/private" )
102
120
msg = "foobar" .encode ('utf8' )
103
121
vtoken = v .validate (msg )
104
122
ok_ (v .public_key .verify (base64 .urlsafe_b64decode (vtoken ),
@@ -108,7 +126,7 @@ def test_validate(self):
108
126
ok_ (v .verify_token (msg , vtoken ))
109
127
110
128
def test_sign_01 (self ):
111
- v = Vapid01 ("/tmp/private" )
129
+ v = Vapid01 . from_file ("/tmp/private" )
112
130
claims = {
"aud" :
"example.com" ,
"sub" :
"[email protected] " }
113
131
result = v .sign (claims , "id=previous" )
114
132
eq_ (result ['Crypto-Key' ],
@@ -123,7 +141,7 @@ def test_sign_01(self):
123
141
'p256ecdsa=' + T_PUBLIC_RAW )
124
142
125
143
def test_sign_02 (self ):
126
- v = Vapid02 ("/tmp/private" )
144
+ v = Vapid02 . from_file ("/tmp/private" )
127
145
claims = {"aud" : "example.com" ,
128
146
129
147
"foo" : "extra value" }
@@ -144,7 +162,7 @@ def test_sign_02(self):
144
162
eq_ (t_val [k ], claims [k ])
145
163
146
164
def test_bad_sign (self ):
147
- v = Vapid01 ("/tmp/private" )
165
+ v = Vapid01 . from_file ("/tmp/private" )
148
166
self .assertRaises (VapidException ,
149
167
v .sign ,
150
168
{'aud' : "p.example.com" })
0 commit comments