9
9
from collections import Mapping , Iterable # Python 2, will be deprecated in Python 3.8
10
10
11
11
from jose import jwk
12
+ from jose .backends .base import Key
12
13
from jose .constants import ALGORITHMS
13
14
from jose .exceptions import JWSError
14
15
from jose .exceptions import JWSSignatureError
@@ -163,10 +164,11 @@ def _encode_payload(payload):
163
164
return base64url_encode (payload )
164
165
165
166
166
- def _sign_header_and_claims (encoded_header , encoded_claims , algorithm , key_data ):
167
+ def _sign_header_and_claims (encoded_header , encoded_claims , algorithm , key ):
167
168
signing_input = b'.' .join ([encoded_header , encoded_claims ])
168
169
try :
169
- key = jwk .construct (key_data , algorithm )
170
+ if not isinstance (key , Key ):
171
+ key = jwk .construct (key , algorithm )
170
172
signature = key .sign (signing_input )
171
173
except Exception as e :
172
174
raise JWSError (e )
@@ -213,7 +215,8 @@ def _load(jwt):
213
215
214
216
def _sig_matches_keys (keys , signing_input , signature , alg ):
215
217
for key in keys :
216
- key = jwk .construct (key , alg )
218
+ if not isinstance (key , Key ):
219
+ key = jwk .construct (key , alg )
217
220
try :
218
221
if key .verify (signing_input , signature ):
219
222
return True
@@ -224,6 +227,9 @@ def _sig_matches_keys(keys, signing_input, signature, alg):
224
227
225
228
def _get_keys (key ):
226
229
230
+ if isinstance (key , Key ):
231
+ return (key ,)
232
+
227
233
try :
228
234
key = json .loads (key , parse_int = str , parse_float = str )
229
235
except Exception :
0 commit comments