Skip to content

Commit 6ed322d

Browse files
committed
More flexibility for key= argument to jws.verify()
1 parent a001a7e commit 6ed322d

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

jose/jws.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import json
44
import six
55

6-
from collections import Mapping
6+
from collections import Mapping, Iterable
77

88
from jose import jwk
99
from jose.constants import ALGORITHMS
@@ -213,6 +213,19 @@ def _sig_matches_keys(keys, signing_input, signature, alg):
213213
return False
214214

215215

216+
def _get_keys(key):
217+
if 'keys' in key: # JWK Set per RFC 7517
218+
if not isinstance(key, Mapping): # Caller didn't JSON-decode
219+
key = json.loads(key)
220+
return key['keys']
221+
# Iterable but not text or mapping => list- or tuple-like
222+
elif (isinstance(key, Iterable) and
223+
not (isinstance(key, six.string_types) or isinstance(key, Mapping))):
224+
return key
225+
else: # Scalar value, wrap in list.
226+
return [key]
227+
228+
216229
def _verify_signature(signing_input, header, signature, key='', algorithms=None):
217230

218231
alg = header.get('alg')
@@ -222,11 +235,7 @@ def _verify_signature(signing_input, header, signature, key='', algorithms=None)
222235
if algorithms is not None and alg not in algorithms:
223236
raise JWSError('The specified alg value is not allowed')
224237

225-
if 'keys' in key: # JWK Set per RFC 7517
226-
keys = key['keys']
227-
else:
228-
keys = [key]
229-
238+
keys = _get_keys(key)
230239
try:
231240
if not _sig_matches_keys(keys, signing_input, signature, alg):
232241
raise JWSSignatureError()

tests/test_jws.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,36 @@ def jwk_set():
197197
'WNz7vOIbvIlBR9Jrq5MIqbkkg'
198198
)
199199

200+
201+
class TestGetKeys(object):
202+
203+
def test_dict(self):
204+
assert [{}] == jws._get_keys({})
205+
206+
def test_custom_object(self):
207+
class MyDict(dict):
208+
pass
209+
mydict = MyDict()
210+
assert [mydict] == jws._get_keys(mydict)
211+
212+
def test_RFC7517_string(self):
213+
key = '{"keys": [{}, {}]}'
214+
assert [{}, {}] == jws._get_keys(key)
215+
216+
def test_RFC7517_mapping(self):
217+
key = {"keys": [{}, {}]}
218+
assert [{}, {}] == jws._get_keys(key)
219+
220+
def test_string(self):
221+
assert ['test'] == jws._get_keys('test')
222+
223+
def test_tuple(self):
224+
assert ('test', 'key') == jws._get_keys(('test', 'key'))
225+
226+
def test_list(self):
227+
assert ['test', 'key'] == jws._get_keys(['test', 'key'])
228+
229+
200230
class TestRSA(object):
201231

202232
def test_jwk_set(self, jwk_set):

0 commit comments

Comments
 (0)