Skip to content

Commit e8789c2

Browse files
authored
Merge pull request #51 from jschlyter/unknown_issuer
Better exception handling for missing keys and issuers
2 parents ec3febe + 72fe817 commit e8789c2

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

src/cryptojwt/exception.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,15 @@ class BadType(Invalid):
4040

4141

4242
class MissingKey(JWKESTException):
43-
""" No usable key """
43+
"""No usable key"""
44+
45+
46+
class KeyNotFound(KeyError):
47+
"""Key not found"""
48+
49+
50+
class IssuerNotFound(KeyError):
51+
"""Issuer not found"""
4452

4553

4654
class KeyIOError(Exception):

src/cryptojwt/key_jar.py

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from requests import request
77

8+
from .exception import UnknownKeyType, KeyIOError, UpdateFailed, IssuerNotFound
89
from .jwe.jwe import alg2keytype as jwe_alg2keytype
910
from .jws.utils import alg2keytype as jws_alg2keytype
1011
from .key_bundle import KeyBundle
@@ -20,18 +21,6 @@
2021
logger = logging.getLogger(__name__)
2122

2223

23-
class KeyIOError(Exception):
24-
pass
25-
26-
27-
class UnknownKeyType(KeyIOError):
28-
pass
29-
30-
31-
class UpdateFailed(KeyIOError):
32-
pass
33-
34-
3524
class KeyJar(object):
3625
""" A keyjar contains a number of KeyBundles sorted by owner/issuer """
3726

@@ -252,7 +241,7 @@ def get_issuer_keys(self, issuer_id):
252241
"""
253242
_issuer = self._get_issuer(issuer_id)
254243
if _issuer is None:
255-
raise KeyError(issuer_id)
244+
raise IssuerNotFound(issuer_id)
256245
return _issuer.all_keys()
257246

258247
@deprecated_alias(issuer='issuer_id', owner='issuer_id')
@@ -273,7 +262,7 @@ def __getitem__(self, issuer_id=''):
273262
"""
274263
_issuer = self._get_issuer(issuer_id)
275264
if _issuer is None:
276-
raise KeyError(issuer_id)
265+
raise IssuerNotFound(issuer_id)
277266
return _issuer
278267

279268
@deprecated_alias(issuer='issuer_id', owner='issuer_id')
@@ -478,7 +467,7 @@ def _add_key(self, keys, issuer_id, use, key_type='', kid='',
478467
_issuer = self._get_issuer(issuer_id)
479468
if _issuer is None:
480469
logger.error('Issuer "{}" not in keyjar'.format(issuer_id))
481-
return keys
470+
raise IssuerNotFound(issuer_id)
482471

483472
logger.debug('Key summary for {}: {}'.format(issuer_id, _issuer.key_summary()))
484473

@@ -678,7 +667,7 @@ def key_summary(self, issuer_id):
678667
if _issuer is not None:
679668
return _issuer.key_summary()
680669

681-
raise KeyError('Unknown Issuer ID: "{}"'.format(issuer_id))
670+
raise IssuerNotFound(issuer_id)
682671

683672
def update(self):
684673
"""

tests/test_04_key_jar.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import pytest
77

8-
from cryptojwt.exception import JWKESTException
8+
from cryptojwt.exception import JWKESTException, IssuerNotFound
99
from cryptojwt.jwe.jwenc import JWEnc
1010
from cryptojwt.jws.jws import JWS
1111
from cryptojwt.jws.jws import factory
@@ -799,8 +799,8 @@ def test_get_decrypt_keys():
799799
keys = kj.get_jwt_decrypt_keys(jwt)
800800
assert keys
801801

802-
keys = kj.get_jwt_decrypt_keys(jwt, aud='Bob')
803-
assert keys
802+
with pytest.raises(IssuerNotFound):
803+
keys = kj.get_jwt_decrypt_keys(jwt, aud='Bob')
804804

805805

806806
def test_update_keyjar():

tests/test_09_jwt.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import os
22

3+
import pytest
4+
5+
from cryptojwt.exception import JWKESTException, IssuerNotFound
6+
from cryptojwt.jws.exception import NoSuitableSigningKeys
37
from cryptojwt.jwt import JWT
48
from cryptojwt.jwt import pick_key
59
from cryptojwt.key_bundle import KeyBundle
@@ -64,6 +68,29 @@ def test_jwt_pack_and_unpack():
6468
assert set(info.keys()) == {'iat', 'iss', 'sub'}
6569

6670

71+
def test_jwt_pack_and_unpack_unknown_issuer():
72+
alice = JWT(key_jar=ALICE_KEY_JAR, iss=ALICE, sign_alg='RS256')
73+
payload = {'sub': 'sub'}
74+
_jwt = alice.pack(payload=payload)
75+
76+
kj = KeyJar()
77+
bob = JWT(key_jar=kj, iss=BOB, allowed_sign_algs=["RS256"])
78+
with pytest.raises(IssuerNotFound):
79+
info = bob.unpack(_jwt)
80+
81+
82+
def test_jwt_pack_and_unpack_unknown_key():
83+
alice = JWT(key_jar=ALICE_KEY_JAR, iss=ALICE, sign_alg='RS256')
84+
payload = {'sub': 'sub'}
85+
_jwt = alice.pack(payload=payload)
86+
87+
kj = KeyJar()
88+
kj.add_kb(ALICE, KeyBundle())
89+
bob = JWT(key_jar=kj, iss=BOB, allowed_sign_algs=["RS256"])
90+
with pytest.raises(NoSuitableSigningKeys):
91+
info = bob.unpack(_jwt)
92+
93+
6794
def test_jwt_pack_and_unpack_with_lifetime():
6895
alice = JWT(key_jar=ALICE_KEY_JAR, iss=ALICE, lifetime=600)
6996
payload = {'sub': 'sub'}

0 commit comments

Comments
 (0)