1919 unicode = str
2020
2121try :
22- from cryptography .hazmat .primitives .serialization import load_pem_private_key , load_pem_public_key , load_ssh_public_key
2322 from cryptography .hazmat .backends import default_backend
24- has_rsa = True
25- has_ecdsa = True
23+ from cryptography .hazmat .primitives .serialization import (
24+ load_pem_private_key , load_pem_public_key , load_ssh_public_key
25+ )
26+
27+ has_crypto = True
2628except ImportError :
27- has_rsa = False
28- has_ecdsa = False
29+ has_crypto = False
2930
3031
3132def utc_timestamp ():
@@ -104,7 +105,7 @@ def test_decodes_valid_jwt(self):
104105 # Used to test for regressions that could affect both
105106 # encoding / decoding operations equally (causing tests
106107 # to still pass).
107- @unittest .skipIf (not has_ecdsa , "Can't run without ecdsa " )
108+ @unittest .skipIf (not has_crypto , "Can't run without cryptography library " )
108109 def test_decodes_valid_es384_jwt (self ):
109110 example_payload = {'hello' : 'world' }
110111 example_pubkey = open ('tests/testkey_ec.pub' , 'r' ).read ()
@@ -124,7 +125,7 @@ def test_decodes_valid_es384_jwt(self):
124125 # Used to test for regressions that could affect both
125126 # encoding / decoding operations equally (causing tests
126127 # to still pass).
127- @unittest .skipIf (not has_rsa , "Can't run without crypto " )
128+ @unittest .skipIf (not has_crypto , "Can't run without cryptography library " )
128129 def test_decodes_valid_rs384_jwt (self ):
129130 example_payload = {'hello' : 'world' }
130131 example_pubkey = open ('tests/testkey_rsa.pub' , 'r' ).read ()
@@ -457,16 +458,18 @@ def test_encode_decode_with_algo_none(self):
457458
458459 jwt .decode (jwt_message , verify = False )
459460
460- @unittest .skipIf (not has_rsa , 'Not supported without crypto library' )
461+ @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
461462 def test_encode_decode_with_rsa_sha256 (self ):
462463 # PEM-formatted RSA key
463464 with open ('tests/testkey_rsa' , 'r' ) as rsa_priv_file :
464- priv_rsakey = load_pem_private_key (ensure_bytes (rsa_priv_file .read ()), password = None , backend = default_backend ())
465+ priv_rsakey = load_pem_private_key (ensure_bytes (rsa_priv_file .read ()),
466+ password = None , backend = default_backend ())
465467 jwt_message = jwt .encode (self .payload , priv_rsakey ,
466468 algorithm = 'RS256' )
467469
468470 with open ('tests/testkey_rsa.pub' , 'r' ) as rsa_pub_file :
469- pub_rsakey = load_ssh_public_key (ensure_bytes (rsa_pub_file .read ()), backend = default_backend ())
471+ pub_rsakey = load_ssh_public_key (ensure_bytes (rsa_pub_file .read ()),
472+ backend = default_backend ())
470473 assert jwt .decode (jwt_message , pub_rsakey )
471474
472475 load_output = jwt .load (jwt_message )
@@ -485,16 +488,18 @@ def test_encode_decode_with_rsa_sha256(self):
485488 load_output = jwt .load (jwt_message )
486489 jwt .verify_signature (key = pub_rsakey , * load_output )
487490
488- @unittest .skipIf (not has_rsa , 'Not supported without crypto library' )
491+ @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
489492 def test_encode_decode_with_rsa_sha384 (self ):
490493 # PEM-formatted RSA key
491494 with open ('tests/testkey_rsa' , 'r' ) as rsa_priv_file :
492- priv_rsakey = load_pem_private_key (ensure_bytes (rsa_priv_file .read ()), password = None , backend = default_backend ())
495+ priv_rsakey = load_pem_private_key (ensure_bytes (rsa_priv_file .read ()),
496+ password = None , backend = default_backend ())
493497 jwt_message = jwt .encode (self .payload , priv_rsakey ,
494498 algorithm = 'RS384' )
495499
496500 with open ('tests/testkey_rsa.pub' , 'r' ) as rsa_pub_file :
497- pub_rsakey = load_ssh_public_key (ensure_bytes (rsa_pub_file .read ()), backend = default_backend ())
501+ pub_rsakey = load_ssh_public_key (ensure_bytes (rsa_pub_file .read ()),
502+ backend = default_backend ())
498503 assert jwt .decode (jwt_message , pub_rsakey )
499504
500505 # string-formatted key
@@ -510,16 +515,18 @@ def test_encode_decode_with_rsa_sha384(self):
510515 load_output = jwt .load (jwt_message )
511516 jwt .verify_signature (key = pub_rsakey , * load_output )
512517
513- @unittest .skipIf (not has_rsa , 'Not supported without crypto library' )
518+ @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
514519 def test_encode_decode_with_rsa_sha512 (self ):
515520 # PEM-formatted RSA key
516521 with open ('tests/testkey_rsa' , 'r' ) as rsa_priv_file :
517- priv_rsakey = load_pem_private_key (ensure_bytes (rsa_priv_file .read ()), password = None , backend = default_backend ())
522+ priv_rsakey = load_pem_private_key (ensure_bytes (rsa_priv_file .read ()),
523+ password = None , backend = default_backend ())
518524 jwt_message = jwt .encode (self .payload , priv_rsakey ,
519525 algorithm = 'RS512' )
520526
521527 with open ('tests/testkey_rsa.pub' , 'r' ) as rsa_pub_file :
522- pub_rsakey = load_ssh_public_key (ensure_bytes (rsa_pub_file .read ()), backend = default_backend ())
528+ pub_rsakey = load_ssh_public_key (ensure_bytes (rsa_pub_file .read ()),
529+ backend = default_backend ())
523530 assert jwt .decode (jwt_message , pub_rsakey )
524531
525532 load_output = jwt .load (jwt_message )
@@ -539,7 +546,7 @@ def test_encode_decode_with_rsa_sha512(self):
539546 jwt .verify_signature (key = pub_rsakey , * load_output )
540547
541548 def test_rsa_related_signing_methods (self ):
542- if has_rsa :
549+ if has_crypto :
543550 self .assertTrue ('RS256' in jwt .signing_methods )
544551 self .assertTrue ('RS384' in jwt .signing_methods )
545552 self .assertTrue ('RS512' in jwt .signing_methods )
@@ -549,7 +556,7 @@ def test_rsa_related_signing_methods(self):
549556 self .assertFalse ('RS512' in jwt .signing_methods )
550557
551558 def test_rsa_related_verify_methods (self ):
552- if has_rsa :
559+ if has_crypto :
553560 self .assertTrue ('RS256' in jwt .verify_methods )
554561 self .assertTrue ('RS384' in jwt .verify_methods )
555562 self .assertTrue ('RS512' in jwt .verify_methods )
@@ -559,7 +566,7 @@ def test_rsa_related_verify_methods(self):
559566 self .assertFalse ('RS512' in jwt .verify_methods )
560567
561568 def test_rsa_related_key_preparation_methods (self ):
562- if has_rsa :
569+ if has_crypto :
563570 self .assertTrue ('RS256' in jwt .prepare_key_methods )
564571 self .assertTrue ('RS384' in jwt .prepare_key_methods )
565572 self .assertTrue ('RS512' in jwt .prepare_key_methods )
@@ -568,16 +575,18 @@ def test_rsa_related_key_preparation_methods(self):
568575 self .assertFalse ('RS384' in jwt .prepare_key_methods )
569576 self .assertFalse ('RS512' in jwt .prepare_key_methods )
570577
571- @unittest .skipIf (not has_ecdsa , "Can't run without ecdsa " )
578+ @unittest .skipIf (not has_crypto , "Can't run without cryptography library " )
572579 def test_encode_decode_with_ecdsa_sha256 (self ):
573580 # PEM-formatted EC key
574581 with open ('tests/testkey_ec' , 'r' ) as ec_priv_file :
575- priv_eckey = load_pem_private_key (ec_priv_file .read (), password = None , backend = default_backend ())
582+ priv_eckey = load_pem_private_key (ensure_bytes (ec_priv_file .read ()),
583+ password = None , backend = default_backend ())
576584 jwt_message = jwt .encode (self .payload , priv_eckey ,
577585 algorithm = 'ES256' )
578586
579587 with open ('tests/testkey_ec.pub' , 'r' ) as ec_pub_file :
580- pub_eckey = load_pem_public_key (ec_pub_file .read (), backend = default_backend ())
588+ pub_eckey = load_pem_public_key (ensure_bytes (ec_pub_file .read ()),
589+ backend = default_backend ())
581590 assert jwt .decode (jwt_message , pub_eckey )
582591
583592 load_output = jwt .load (jwt_message )
@@ -596,17 +605,19 @@ def test_encode_decode_with_ecdsa_sha256(self):
596605 load_output = jwt .load (jwt_message )
597606 jwt .verify_signature (key = pub_eckey , * load_output )
598607
599- @unittest .skipIf (not has_ecdsa , "Can't run without ecdsa " )
608+ @unittest .skipIf (not has_crypto , "Can't run without cryptography library " )
600609 def test_encode_decode_with_ecdsa_sha384 (self ):
601610
602611 # PEM-formatted EC key
603612 with open ('tests/testkey_ec' , 'r' ) as ec_priv_file :
604- priv_eckey = load_pem_private_key (ec_priv_file .read (), password = None , backend = default_backend ())
613+ priv_eckey = load_pem_private_key (ensure_bytes (ec_priv_file .read ()),
614+ password = None , backend = default_backend ())
605615 jwt_message = jwt .encode (self .payload , priv_eckey ,
606616 algorithm = 'ES384' )
607617
608618 with open ('tests/testkey_ec.pub' , 'r' ) as ec_pub_file :
609- pub_eckey = load_pem_public_key (ec_pub_file .read (), backend = default_backend ())
619+ pub_eckey = load_pem_public_key (ensure_bytes (ec_pub_file .read ()),
620+ backend = default_backend ())
610621 assert jwt .decode (jwt_message , pub_eckey )
611622
612623 load_output = jwt .load (jwt_message )
@@ -625,16 +636,17 @@ def test_encode_decode_with_ecdsa_sha384(self):
625636 load_output = jwt .load (jwt_message )
626637 jwt .verify_signature (key = pub_eckey , * load_output )
627638
628- @unittest .skipIf (not has_ecdsa , "Can't run without ecdsa " )
639+ @unittest .skipIf (not has_crypto , "Can't run without cryptography library " )
629640 def test_encode_decode_with_ecdsa_sha512 (self ):
630641 # PEM-formatted EC key
631642 with open ('tests/testkey_ec' , 'r' ) as ec_priv_file :
632- priv_eckey = load_pem_private_key (ec_priv_file .read (), password = None , backend = default_backend ())
643+ priv_eckey = load_pem_private_key (ensure_bytes (ec_priv_file .read ()),
644+ password = None , backend = default_backend ())
633645 jwt_message = jwt .encode (self .payload , priv_eckey ,
634646 algorithm = 'ES512' )
635647
636648 with open ('tests/testkey_ec.pub' , 'r' ) as ec_pub_file :
637- pub_eckey = load_pem_public_key (ec_pub_file .read (), backend = default_backend ())
649+ pub_eckey = load_pem_public_key (ensure_bytes ( ec_pub_file .read () ), backend = default_backend ())
638650 assert jwt .decode (jwt_message , pub_eckey )
639651
640652 load_output = jwt .load (jwt_message )
@@ -654,7 +666,7 @@ def test_encode_decode_with_ecdsa_sha512(self):
654666 jwt .verify_signature (key = pub_eckey , * load_output )
655667
656668 def test_ecdsa_related_signing_methods (self ):
657- if has_ecdsa :
669+ if has_crypto :
658670 self .assertTrue ('ES256' in jwt .signing_methods )
659671 self .assertTrue ('ES384' in jwt .signing_methods )
660672 self .assertTrue ('ES512' in jwt .signing_methods )
@@ -664,7 +676,7 @@ def test_ecdsa_related_signing_methods(self):
664676 self .assertFalse ('ES512' in jwt .signing_methods )
665677
666678 def test_ecdsa_related_verify_methods (self ):
667- if has_ecdsa :
679+ if has_crypto :
668680 self .assertTrue ('ES256' in jwt .verify_methods )
669681 self .assertTrue ('ES384' in jwt .verify_methods )
670682 self .assertTrue ('ES512' in jwt .verify_methods )
@@ -674,7 +686,7 @@ def test_ecdsa_related_verify_methods(self):
674686 self .assertFalse ('ES512' in jwt .verify_methods )
675687
676688 def test_ecdsa_related_key_preparation_methods (self ):
677- if has_ecdsa :
689+ if has_crypto :
678690 self .assertTrue ('ES256' in jwt .prepare_key_methods )
679691 self .assertTrue ('ES384' in jwt .prepare_key_methods )
680692 self .assertTrue ('ES512' in jwt .prepare_key_methods )
0 commit comments