Skip to content

Commit 89c3d41

Browse files
committed
Support loading a JWK object directly
1 parent eb7c5fe commit 89c3d41

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

jose/jws.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from collections import Mapping, Iterable # Python 2, will be deprecated in Python 3.8
1010

1111
from jose import jwk
12+
from jose.backends.base import Key
1213
from jose.constants import ALGORITHMS
1314
from jose.exceptions import JWSError
1415
from jose.exceptions import JWSSignatureError
@@ -163,10 +164,11 @@ def _encode_payload(payload):
163164
return base64url_encode(payload)
164165

165166

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):
167168
signing_input = b'.'.join([encoded_header, encoded_claims])
168169
try:
169-
key = jwk.construct(key_data, algorithm)
170+
if not isinstance(key, Key):
171+
key = jwk.construct(key, algorithm)
170172
signature = key.sign(signing_input)
171173
except Exception as e:
172174
raise JWSError(e)
@@ -213,7 +215,8 @@ def _load(jwt):
213215

214216
def _sig_matches_keys(keys, signing_input, signature, alg):
215217
for key in keys:
216-
key = jwk.construct(key, alg)
218+
if not isinstance(key, Key):
219+
key = jwk.construct(key, alg)
217220
try:
218221
if key.verify(signing_input, signature):
219222
return True
@@ -224,6 +227,9 @@ def _sig_matches_keys(keys, signing_input, signature, alg):
224227

225228
def _get_keys(key):
226229

230+
if isinstance(key, Key):
231+
return (key,)
232+
227233
try:
228234
key = json.loads(key, parse_int=str, parse_float=str)
229235
except Exception:

0 commit comments

Comments
 (0)