3
3
4
4
import pytest
5
5
6
- from jose import jwk , jws , jwt
6
+ from jose import jwk , jws
7
7
from jose .backends import RSAKey
8
8
from jose .constants import ALGORITHMS
9
- from jose .exceptions import JWSError
9
+ from jose .exceptions import JWSError , JWKAlgMismatchError
10
10
11
11
try :
12
12
from jose .backends .cryptography_backend import CryptographyRSAKey
@@ -26,14 +26,14 @@ def test_unicode_token(self):
26
26
jws .verify (token , "secret" , ["HS256" ])
27
27
28
28
def test_hetero_keys (self ):
29
- private_key = b"-----BEGIN PRIVATE KEY----- \n MIGEAgEAMBAGByqGSM49AgEGBS..."
30
- public_key = b"-----BEGIN PUBLIC KEY----- \n MHYwEAYHKoZIzj0CAQYFK4EEAC..."
31
- token = jwt . encode ({ "some" : "claims" }, private_key , algorithm = "RS256" )
32
-
33
- rsa_key = jwk . RSAKey ( public_key , "RS256" ). to_dict ()
34
- hmac_key = jwk . HMACKey ( "secret" , "HS256" ). to_dict ()
35
- # RSA key must come second to exercise "JWKAlgMismatchError"
36
- jws .verify (token , {"keys" : [hmac_key , rsa_key ]}, ["HS256 " , "RS256 " ])
29
+ class BadKey ( jwk . Key ):
30
+ def __init__ ( self , key , algorithm ):
31
+ if key != "xyzw" :
32
+ raise JWKAlgMismatchError ( "%s is not a valid XYZW algorithm" % algorithm )
33
+
34
+ token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhIjoiYiJ9.jiMyrsmD8AoHWeQgmxZ5yq8z0lXS67_QGs52AzC8Ru8"
35
+ jwk . register_key ( "XYZW" , BadKey )
36
+ jws .verify (token , {"keys" : [{ "alg" : "XYZW" }, "secret" ]}, ["XYZW " , "HS256 " ])
37
37
38
38
def test_multiple_keys (self ):
39
39
old_jwk_verify = jwk .HMACKey .verify
0 commit comments