2626def full_path (local_file ):
2727 return os .path .join (BASEDIR , local_file )
2828
29+
2930KEY = full_path ("server.key" )
3031
3132JWK = {"keys" : [{'alg' : 'RSA' ,
@@ -44,7 +45,6 @@ def full_path(local_file):
4445 119 , 98 , 61 , 34 , 61 , 46 , 33 , 114 , 5 , 46 , 79 , 8 , 192 , 205 , 154 , 245 ,
4546 103 , 208 , 128 , 163 ]
4647
47-
4848JWKS = {"keys" : [
4949 {
5050 "n" : b"zkpUgEgXICI54blf6iWiD2RbMDCOO1jV0VSff1MFFnujM4othfMsad7H1kRo50YM5S_X9TdvrpdOfpz5aBaKFhT6Ziv0nhtcekq1eRl8mjBlvGKCE5XGk-0LFSDwvqgkJoFYInq7bu0a4JEzKs5AyJY75YlGh879k1Uu2Sv3ZZOunfV1O1Orta-NvS-aG_jN5cstVbCGWE20H0vFVrJKNx0Zf-u-aA-syM4uX7wdWgQ-owoEMHge0GmGgzso2lwOYf_4znanLwEuO3p5aabEaFoKNR4K6GjQcjBcYmDEE4CtfRU9AEmhcD1kleiTB9TjPWkgDmT9MXsGxBHf3AKT5w" ,
@@ -132,6 +132,7 @@ def full_path(local_file):
132132SIGKEYS = KEYS ()
133133SIGKEYS .load_dict (JWKS )
134134
135+
135136def test_1 ():
136137 claimset = {"iss" : "joe" ,
137138 "exp" : 1300819380 ,
@@ -148,14 +149,14 @@ def test_1():
148149
149150
150151def test_hmac_256 ():
151- payload = b 'Please take a moment to register today'
152+ payload = 'Please take a moment to register today'
152153 keys = [SYMKey (key = jwkest .intarr2bin (HMAC_KEY ))]
153154 _jws = JWS (payload , alg = "HS256" )
154155 _jwt = _jws .sign_compact (keys )
155156
156157 info = JWS ().verify_compact (_jwt , keys )
157158
158- assert info == payload . decode ( "utf-8" )
159+ assert info == payload
159160
160161
161162def test_hmac_384 ():
@@ -205,7 +206,7 @@ def test_left_hash_hs512():
205206def test_rs256 ():
206207 payload = "Please take a moment to register today"
207208 keys = [RSAKey (key = import_rsa_key_from_file (KEY ))]
208- #keys[0]._keytype = "private"
209+ # keys[0]._keytype = "private"
209210 _jws = JWS (payload , alg = "RS256" )
210211 _jwt = _jws .sign_compact (keys )
211212
@@ -218,7 +219,7 @@ def test_rs256():
218219def test_rs384 ():
219220 payload = "Please take a moment to register today"
220221 keys = [RSAKey (key = import_rsa_key_from_file (KEY ))]
221- #keys[0]._keytype = "private"
222+ # keys[0]._keytype = "private"
222223 _jws = JWS (payload , alg = "RS384" )
223224 _jwt = _jws .sign_compact (keys )
224225
@@ -230,7 +231,7 @@ def test_rs384():
230231def test_rs512 ():
231232 payload = "Please take a moment to register today"
232233 keys = [RSAKey (key = import_rsa_key_from_file (KEY ))]
233- #keys[0]._keytype = "private"
234+ # keys[0]._keytype = "private"
234235 _jws = JWS (payload , alg = "RS512" )
235236 _jwt = _jws .sign_compact (keys )
236237
@@ -323,7 +324,7 @@ def test_signer_es512():
323324 payload = "Please take a moment to register today"
324325 _key = ECKey ().load_key (P521 )
325326 keys = [_key ]
326- #keys[0]._keytype = "private"
327+ # keys[0]._keytype = "private"
327328 _jws = JWS (payload , alg = "ES512" )
328329 _jwt = _jws .sign_compact (keys )
329330
@@ -335,7 +336,7 @@ def test_signer_es512():
335336def test_signer_ps256 ():
336337 payload = "Please take a moment to register today"
337338 keys = [RSAKey (key = import_rsa_key_from_file (KEY ))]
338- #keys[0]._keytype = "private"
339+ # keys[0]._keytype = "private"
339340 _jws = JWS (payload , alg = "PS256" )
340341 _jwt = _jws .sign_compact (keys )
341342
@@ -347,7 +348,7 @@ def test_signer_ps256():
347348def test_signer_ps256_fail ():
348349 payload = "Please take a moment to register today"
349350 keys = [RSAKey (key = import_rsa_key_from_file (KEY ))]
350- #keys[0]._keytype = "private"
351+ # keys[0]._keytype = "private"
351352 _jws = JWS (payload , alg = "PS256" )
352353 _jwt = _jws .sign_compact (keys )[:- 5 ] + 'abcde'
353354
@@ -363,7 +364,7 @@ def test_signer_ps256_fail():
363364def test_signer_ps384 ():
364365 payload = "Please take a moment to register today"
365366 keys = [RSAKey (key = import_rsa_key_from_file (KEY ))]
366- #keys[0]._keytype = "private"
367+ # keys[0]._keytype = "private"
367368 _jws = JWS (payload , alg = "PS384" )
368369 _jwt = _jws .sign_compact (keys )
369370
@@ -376,7 +377,7 @@ def test_signer_ps512():
376377 payload = "Please take a moment to register today"
377378 # Key has to be big enough > 512+512+2
378379 keys = [RSAKey (key = import_rsa_key_from_file (full_path ("./size2048.key" )))]
379- #keys[0]._keytype = "private"
380+ # keys[0]._keytype = "private"
380381 _jws = JWS (payload , alg = "PS512" )
381382 _jwt = _jws .sign_compact (keys )
382383
@@ -410,20 +411,21 @@ def test_sign_2():
410411 "use" : "sig" ,
411412 "kid" : "af22448d-4c7b-464d-b63a-f5bd90f6d7d1" ,
412413 "n" : "o9g8DpUwBW6B1qmcm-TfEh4rNX7n1t38jdo4Gkl_cI3q--7n0Blg0kN88LHZvyZjUB2NhBdFYNxMP8ucy0dOXvWGWzaPmGnq3DM__lN8P4WjD1cCTAVEYKawNBAmGKqrFj1SgpPNsSqiqK-ALM1w6mZ-QGimjOgwCyJy3l9lzZh5D8tKnS2t1pZgE0X5P7lZQWHYpHPqp4jKhETzrCpPGfv0Rl6nmmjp7NlRYBkWKf_HEKE333J6M039m2FbKgxrBg3zmYYpmHuMzVgxxb8LSiv5aqyeyJjxM-YDUAgNQBfKNhONqXyu9DqtSprNkw6sqmuxK0QUVrNYl3b03PgS5Q"
413- }]}
414+ }]}
414415
415416 keys = KEYS ()
416417 keys .load_dict (keyset )
417418 jws = JWS ("payload" , alg = "RS512" )
418419 jws .sign_compact (keys = keys )
419420
421+
420422def test_signer_protected_headers ():
421423 payload = "Please take a moment to register today"
422424 _key = ECKey ().load_key (P256 )
423425 keys = [_key ]
424426 _jws = JWS (payload , alg = "ES256" )
425427 protected = dict (header1 = u"header1 is protected" ,
426- header2 = "header2 is protected too" , a = 1 )
428+ header2 = "header2 is protected too" , a = 1 )
427429 _jwt = _jws .sign_compact (keys , protected = protected )
428430
429431 exp_protected = protected .copy ()
@@ -436,32 +438,26 @@ def test_signer_protected_headers():
436438 info = _rj .verify_compact (_jwt , keys )
437439 assert info == payload
438440
441+
439442def test_verify_protected_headers ():
440443 payload = "Please take a moment to register today"
441444 _key = ECKey ().load_key (P256 )
442445 keys = [_key ]
443446 _jws = JWS (payload , alg = "ES256" )
444447 protected = dict (header1 = u"header1 is protected" ,
445- header2 = "header2 is protected too" , a = 1 )
448+ header2 = "header2 is protected too" , a = 1 )
446449 _jwt = _jws .sign_compact (keys , protected = protected )
447450 protectedHeader , enc_payload , sig = _jwt .split ("." )
448451 data = dict (payload = enc_payload , signatures = [
449452 dict (
450453 header = dict (alg = u"ES256" , jwk = _key .serialize ()),
451454 protected = protectedHeader ,
452455 signature = sig ,
453- )
454- ])
455- fobj = io .BytesIO ( JSONEncoder (). encode (data ). encode ( "utf-8" ))
456+ )
457+ ])
458+ stream = io .StringIO ( json . dumps (data , ensure_ascii = False ))
456459 _jws = JWS ()
457- reader = codecs .getreader ("utf-8" )
458- assert _jws .verify_json (reader (fobj )) == payload
459-
460- class JSONEncoder (json .JSONEncoder ):
461- def default (self , o ):
462- if isinstance (o , bytes ):
463- return o .decode ('utf-8' )
464- return json .JSONEncoder .default (self , o )
460+ assert _jws .verify_json (stream ) == payload
465461
466462
467463def test_pick ():
0 commit comments