66from nose .tools import eq_ , ok_
77from mock import patch , Mock
88
9- from cryptography .hazmat .primitives .asymmetric import ec , utils
10- from cryptography .hazmat .primitives import hashes
11-
129from py_vapid import Vapid01 , Vapid02 , VapidException
1310from py_vapid .jwt import decode
14- from py_vapid .utils import b64urldecode
1511
1612# This is a private key in DER form.
1713T_DER = """
@@ -126,17 +122,6 @@ def test_from_raw(self):
126122 v = Vapid01 .from_raw (T_RAW )
127123 self .check_keys (v )
128124
129- def test_validate (self ):
130- v = Vapid01 .from_file ("/tmp/private" )
131- msg = "foobar" .encode ('utf8' )
132- vtoken = v .validate (msg )
133- ok_ (v .public_key .verify (
134- base64 .urlsafe_b64decode (self .repad (vtoken ).encode ()),
135- msg ,
136- ec .ECDSA (hashes .SHA256 ())))
137- # test verify
138- ok_ (v .verify_token (msg , vtoken ))
139-
140125 def test_sign_01 (self ):
141126 v = Vapid01 .from_file ("/tmp/private" )
142127 claims = {"aud" : "https://example.com" ,
@@ -152,6 +137,12 @@ def test_sign_01(self):
152137 result = v .sign (claims )
153138 eq_ (result ['Crypto-Key' ],
154139 'p256ecdsa=' + T_PUBLIC_RAW .decode ('utf8' ))
140+ # Verify using the same function as Integration
141+ # this should ensure that the r,s sign values are correctly formed
142+ ok_ (Vapid01 .verify (
143+ key = result ['Crypto-Key' ].split ('=' )[1 ],
144+ auth = result ['Authorization' ]
145+ ))
155146
156147 def test_sign_02 (self ):
157148 v = Vapid02 .from_file ("/tmp/private" )
@@ -174,26 +165,17 @@ def test_sign_02(self):
174165 for k in claims :
175166 eq_ (t_val [k ], claims [k ])
176167
177- def test_alt_sign (self ):
178- """ecdsa uses a raw key pair to sign, openssl uses a DER."""
179- v = Vapid01 .from_file ("/tmp/private" )
180- claims = {"aud" : "https://example.com" ,
181- "sub" :
"mailto:[email protected] " ,
182- "foo" : "extra value" }
183- # Get a signed token.
184- result = v .sign (claims )
185- # Convert the dss into raw.
186- auth , sig = result .get ('Authorization' ).split (' ' )[1 ].rsplit ('.' , 1 )
187- ss = utils .decode_dss_signature (b64urldecode (sig .encode ('utf8' )))
188- new_sig = binascii .b2a_base64 (
189- binascii .unhexlify ("%064x%064x" % ss )
190- ).strip ().strip (b'=' ).decode ()
191- new_auth = auth + '.' + new_sig
192- # phew, all that done, now check
193- pkey = result .get ("Crypto-Key" ).split ('=' )[1 ]
194- items = decode (new_auth , pkey )
195-
196- eq_ (items , claims )
168+ def test_integration (self ):
169+ # These values were taken from a test page. DO NOT ALTER!
170+ key = ("BDd3_hVL9fZi9Ybo2UUzA284WG5FZR30_95YeZJsiApwXKpNcF1rRPF3foI"
171+ "iBHXRdJI2Qhumhf6_LFTeZaNndIo" )
172+
173+ auth = ("WebPush eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NiJ9.eyJhdWQiOiJod"
174+ "HRwczovL3VwZGF0ZXMucHVzaC5zZXJ2aWNlcy5tb3ppbGxhLmNvbSIsImV"
175+ "4cCI6MTQ5NDY3MTQ3MCwic3ViIjoibWFpbHRvOnNpbXBsZS1wdXNoLWRlb"
176+ "W9AZ2F1bnRmYWNlLmNvLnVrIn0.LqPi86T-HJ71TXHAYFptZEHD7Wlfjcc"
177+ "4u5jYZ17WpqOlqDcW-5Wtx3x1OgYX19alhJ9oLumlS2VzEvNioZolQA" )
178+ ok_ (Vapid01 .verify (key = key , auth = auth ))
197179
198180 def test_bad_sign (self ):
199181 v = Vapid01 .from_file ("/tmp/private" )
0 commit comments