1
1
import base64
2
2
3
- from jwt .algorithms import Algorithm , HMACAlgorithm
3
+ from jwt .algorithms import Algorithm , HMACAlgorithm , NoneAlgorithm
4
+ from jwt .exceptions import InvalidKeyError
4
5
5
6
from .compat import unittest
6
7
from .utils import ensure_bytes , ensure_unicode , key_path
@@ -35,6 +36,12 @@ def test_algorithm_should_throw_exception_if_verify_not_impl(self):
35
36
with self .assertRaises (NotImplementedError ):
36
37
algo .verify ('message' , 'key' , 'signature' )
37
38
39
+ def test_none_algorithm_should_throw_exception_if_key_is_not_none (self ):
40
+ algo = NoneAlgorithm ()
41
+
42
+ with self .assertRaises (InvalidKeyError ):
43
+ algo .prepare_key ('123' )
44
+
38
45
def test_hmac_should_reject_nonstring_key (self ):
39
46
algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
40
47
@@ -49,6 +56,38 @@ def test_hmac_should_accept_unicode_key(self):
49
56
50
57
algo .prepare_key (ensure_unicode ('awesome' ))
51
58
59
+ @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
60
+ def test_hmac_should_throw_exception_if_key_is_pem_public_key (self ):
61
+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
62
+
63
+ with self .assertRaises (InvalidKeyError ):
64
+ with open (key_path ('testkey2_rsa.pub.pem' ), 'r' ) as keyfile :
65
+ algo .prepare_key (keyfile .read ())
66
+
67
+ @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
68
+ def test_hmac_should_throw_exception_if_key_is_x509_certificate (self ):
69
+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
70
+
71
+ with self .assertRaises (InvalidKeyError ):
72
+ with open (key_path ('testkey_rsa.cer' ), 'r' ) as keyfile :
73
+ algo .prepare_key (keyfile .read ())
74
+
75
+ @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
76
+ def test_hmac_should_throw_exception_if_key_is_ssh_public_key (self ):
77
+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
78
+
79
+ with self .assertRaises (InvalidKeyError ):
80
+ with open (key_path ('testkey_rsa.pub' ), 'r' ) as keyfile :
81
+ algo .prepare_key (keyfile .read ())
82
+
83
+ @unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
84
+ def test_hmac_should_throw_exception_if_key_is_x509_cert (self ):
85
+ algo = HMACAlgorithm (HMACAlgorithm .SHA256 )
86
+
87
+ with self .assertRaises (InvalidKeyError ):
88
+ with open (key_path ('testkey2_rsa.pub.pem' ), 'r' ) as keyfile :
89
+ algo .prepare_key (keyfile .read ())
90
+
52
91
@unittest .skipIf (not has_crypto , 'Not supported without cryptography library' )
53
92
def test_rsa_should_parse_pem_public_key (self ):
54
93
algo = RSAAlgorithm (RSAAlgorithm .SHA256 )
0 commit comments