77from mock import patch
88
99from jose import jws
10- from py_vapid import Vapid , VapidException
10+ from py_vapid import Vapid01 , Vapid02 , VapidException
1111
1212T_DER = """
1313MHcCAQEEIPeN1iAipHbt8+/KZ2NIF8NeN24jqAmnMLFZEMocY8RboAoGCCqGSM49
@@ -43,54 +43,57 @@ def tearDown(self):
4343
4444class VapidTestCase (unittest .TestCase ):
4545 def test_init (self ):
46- v1 = Vapid (private_key_file = "/tmp/private" )
46+ v1 = Vapid01 (private_key_file = "/tmp/private" )
4747 eq_ (v1 .private_key .to_pem (), T_PRIVATE )
4848 eq_ (v1 .public_key .to_pem (), T_PUBLIC )
49- v2 = Vapid (private_key = T_PRIVATE )
49+ v2 = Vapid01 (private_key = T_PRIVATE )
5050 eq_ (v2 .private_key .to_pem (), T_PRIVATE )
5151 eq_ (v2 .public_key .to_pem (), T_PUBLIC )
52- v3 = Vapid (private_key = T_DER )
52+ v3 = Vapid01 (private_key = T_DER )
5353 eq_ (v3 .private_key .to_pem (), T_PRIVATE )
5454 eq_ (v3 .public_key .to_pem (), T_PUBLIC )
5555 no_exist = '/tmp/not_exist'
56- Vapid (private_key_file = no_exist )
56+ Vapid01 (private_key_file = no_exist )
5757 ok_ (os .path .isfile (no_exist ))
5858 os .unlink (no_exist )
5959
60+ def repad (self , data ):
61+ return data + b"====" [:len (data ) % 4 ]
62+
6063 @patch ("ecdsa.SigningKey.from_pem" , side_effect = Exception )
6164 def test_init_bad_priv (self , mm ):
6265 self .assertRaises (Exception ,
63- Vapid ,
66+ Vapid01 ,
6467 private_key_file = "/tmp/private" )
6568
6669 def test_private (self ):
67- v = Vapid ()
70+ v = Vapid01 ()
6871 self .assertRaises (VapidException , lambda x = None : v .private_key )
6972
7073 def test_public (self ):
71- v = Vapid ()
74+ v = Vapid01 ()
7275
7376 self .assertRaises (VapidException , lambda x = None : v .public_key )
7477
7578 def test_gen_key (self ):
76- v = Vapid ()
79+ v = Vapid01 ()
7780 v .generate_keys ()
7881 ok_ (v .public_key )
7982 ok_ (v .private_key )
8083
8184 def test_save_key (self ):
82- v = Vapid ()
85+ v = Vapid01 ()
8386 v .save_key ("/tmp/p2" )
8487 os .unlink ("/tmp/p2" )
8588
8689 def test_save_public_key (self ):
87- v = Vapid ()
90+ v = Vapid01 ()
8891 v .generate_keys ()
8992 v .save_public_key ("/tmp/p2" )
9093 os .unlink ("/tmp/p2" )
9194
9295 def test_validate (self ):
93- v = Vapid ("/tmp/private" )
96+ v = Vapid01 ("/tmp/private" )
9497 msg = "foobar"
9598 vtoken = v .validate (msg )
9699 ok_ (v .public_key .verify (base64 .urlsafe_b64decode (vtoken ),
@@ -99,8 +102,8 @@ def test_validate(self):
99102 # test verify
100103 ok_ (v .verify_token (msg , vtoken ))
101104
102- def test_sign (self ):
103- v = Vapid ("/tmp/private" )
105+ def test_sign_01 (self ):
106+ v = Vapid01 ("/tmp/private" )
104107 claims = {
"aud" :
"example.com" ,
"sub" :
"[email protected] " }
105108 result = v .sign (claims , "id=previous" )
106109 eq_ (result ['Crypto-Key' ],
@@ -114,8 +117,29 @@ def test_sign(self):
114117 eq_ (result ['Crypto-Key' ],
115118 'p256ecdsa=' + T_PUBLIC_RAW )
116119
120+ def test_sign_02 (self ):
121+ v = Vapid02 ("/tmp/private" )
122+ claims = {"aud" : "example.com" ,
123+ 124+ "foo" : "extra value" }
125+ result = v .sign (claims , "id=previous" )
126+ auth = result ['Authorization' ]
127+ eq_ (auth [:6 ], 'vapid ' )
128+ ok_ (' t=' in auth )
129+ ok_ (',k=' in auth )
130+ parts = auth [6 :].split (',' )
131+ eq_ (len (parts ), 2 )
132+ t_val = json .loads (base64 .urlsafe_b64decode (
133+ self .repad (parts [0 ][2 :].split ('.' )[1 ])
134+ ))
135+ k_val = base64 .urlsafe_b64decode (self .repad (parts [1 ][2 :]))
136+ eq_ (k_val [0 ], "\04 " )
137+ eq_ (len (k_val ), 65 )
138+ for k in claims :
139+ eq_ (t_val [k ], claims [k ])
140+
117141 def test_bad_sign (self ):
118- v = Vapid ("/tmp/private" )
142+ v = Vapid01 ("/tmp/private" )
119143 self .assertRaises (VapidException ,
120144 v .sign ,
121145 {'aud' : "p.example.com" })
0 commit comments