Skip to content

Commit a25e168

Browse files
committed
Added some missing ensure_bytes in a couple of the tests.
Consolidated testing variables for has_rsa and has_ecdsa into has_crypto. Updated README and dependencies in tox.ini
1 parent a517b0c commit a25e168

File tree

4 files changed

+51
-45
lines changed

4 files changed

+51
-45
lines changed

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,11 @@ $ pip install PyJWT
1010

1111
**A Note on Dependencies**:
1212

13-
The RSASSA-PKCS1-v1_5 algorithms depend on PyCrypto. If you plan on
13+
RSA and ECDSA signatures depend on the cryptography package. If you plan on
1414
using any of those algorithms, you'll need to install it as well.
1515

1616
```
17-
$ pip install PyCrypto
18-
```
19-
20-
The Elliptic Curve Digital Signature algorithms depend on Python-ECDSA. If
21-
you plan on using any of those algorithms, you'll need to install it as well.
22-
23-
```
24-
$ pip install ecdsa
17+
$ pip install cryptography
2518
```
2619

2720
## Usage
@@ -73,11 +66,11 @@ jwt.encode({'some': 'payload'}, 'secret', 'HS512')
7366

7467
When using the RSASSA-PKCS1-v1_5 algorithms, the `key` argument in both
7568
`jwt.encode()` and `jwt.decode()` (`"secret"` in the examples) is expected to
76-
be an RSA public or private key as imported with `Crypto.PublicKey.RSA.importKey()`.
69+
be either an RSA public or private key in PEM format.
7770

7871
When using the ECDSA algorithms, the `key` argument is expected to
79-
be an Elliptic Curve private key as imported with `ecdsa.SigningKey.from_pem()`,
80-
or a public key as imported with `ecdsa.VerifyingKey.from_pem()`.
72+
be an Elliptic Curve private key or an Elliptic Curve public
73+
key in PEM foramt.
8174

8275
## Tests
8376

jwt/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ def prepare_HS_key(key):
7777

7878
try:
7979
from cryptography.hazmat.primitives import interfaces, hashes
80-
from cryptography.hazmat.primitives.serialization import load_pem_private_key, load_pem_public_key, load_ssh_public_key
80+
from cryptography.hazmat.primitives.serialization import (
81+
load_pem_private_key, load_pem_public_key, load_ssh_public_key
82+
)
8183
from cryptography.hazmat.primitives.asymmetric import ec, rsa, padding
8284
from cryptography.hazmat.backends import default_backend
8385
from cryptography.exceptions import InvalidSignature

tests/test_jwt.py

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
unicode = str
2020

2121
try:
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
2628
except ImportError:
27-
has_rsa = False
28-
has_ecdsa = False
29+
has_crypto = False
2930

3031

3132
def 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)

tox.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ envlist = py26, py27, py32, py33, py34
44
[testenv]
55
deps =
66
cryptography
7-
ecdsa
87
unittest2
98
commands = {envpython} setup.py test

0 commit comments

Comments
 (0)