Skip to content

Commit 33df4c8

Browse files
committed
working on tests
1 parent 8ce7278 commit 33df4c8

File tree

4 files changed

+47
-16
lines changed

4 files changed

+47
-16
lines changed

LICENSE.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright (C) 2017 Roland Hedberg, Sweden
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

src/cryptojwt/exception.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ class HeaderError(JWKESTException):
6969

7070
class UnSupported(JWKESTException):
7171
pass
72+
73+
74+
class MissingValue(JWKESTException):
75+
pass

src/cryptojwt/jwt.py

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

66
from cryptojwt import jwe
77
from cryptojwt import jws
8+
from cryptojwt.exception import MissingValue
89
from cryptojwt.jwe import JWE
910
from cryptojwt.jws import JWS
1011
from cryptojwt.jws import NoSuitableSigningKeys
@@ -57,11 +58,24 @@ def get_jwt_keys(jwt, keys, use):
5758
try:
5859
_kid = jwt.headers['kid']
5960
except KeyError:
60-
_kid = ''
61+
_kid = '' # Unknown
6162

62-
# Pick issuers keys
63+
# pick issuer keys
64+
if use == 'sig':
65+
payload = json.loads(jwt.part[1])
66+
try:
67+
_keys = keys[payload['iss']]
68+
except KeyError: # No issuer, not kosher
69+
raise MissingValue('iss')
70+
if not _kid:
71+
try:
72+
_kid = payload['kid']
73+
except KeyError:
74+
_kid = '' # Unknown
75+
else:
76+
_keys = keys
6377

64-
return pick_key(keys, use, key_type=_key_type, kid=_kid)
78+
return pick_key(_keys, use, key_type=_key_type, kid=_kid)
6579

6680

6781
class JWT(object):
@@ -150,7 +164,7 @@ def pack(self, payload=None, kid='', owner='', recv='', **kwargs):
150164
if payload is not None:
151165
_args.update(payload)
152166

153-
_jws = JWS(json.dumps(payload), alg=self.sign_alg)
167+
_jws = JWS(json.dumps(_args), alg=self.sign_alg)
154168
_sjwt = _jws.sign_compact([_key])
155169
#_jws = _jwt.to_jwt([_key], self.sign_alg)
156170
if _encrypt:

tests/test_5_jwt.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ def full_path(local_file):
1818
k1 = import_private_rsa_key_from_file(full_path('rsa.key'))
1919
k2 = import_private_rsa_key_from_file(full_path('size2048.key'))
2020

21-
ALICE_KEYS = [RSAKey(use='sig').load_key(k1),
22-
RSAKey(use='enc').load_key(k2)]
23-
ALICE_PUB_KEYS = [RSAKey(use='sig').load_key(k1.public_key()),
24-
RSAKey(use='enc').load_key(k2.public_key())]
21+
ALICE_KEYS = [RSAKey(use='sig', kid='1').load_key(k1),
22+
RSAKey(use='enc', kid='2').load_key(k2)]
23+
ALICE_PUB_KEYS = [RSAKey(use='sig', kid='1').load_key(k1.public_key()),
24+
RSAKey(use='enc', kid='2').load_key(k2.public_key())]
2525

2626
k3 = import_private_rsa_key_from_file(full_path('server.key'))
2727

28-
BOB_KEYS = [RSAKey(use='enc').load_key(k3)]
29-
BOB_PUB_KEYS = [RSAKey(use='enc').load_key(k3.public_key())]
28+
BOB_KEYS = [RSAKey(use='enc', kid='3').load_key(k3)]
29+
BOB_PUB_KEYS = [RSAKey(use='enc', kid='3').load_key(k3.public_key())]
3030

3131

3232
def _eq(l1, l2):
@@ -45,32 +45,32 @@ def test_jwt_pack_and_unpack():
4545
payload = {'sub': 'sub'}
4646
_jwt = alice.pack(payload=payload)
4747

48-
bob = JWT(own_keys=BOB_KEYS, iss=BOB, rec_keys={ALICE: ALICE_KEYS})
48+
bob = JWT(own_keys=BOB_KEYS, iss=BOB, rec_keys={ALICE: ALICE_PUB_KEYS})
4949
info = bob.unpack(_jwt)
5050

51-
assert set(info.keys()) == {'jti', 'iat', 'iss', 'sub', 'kid'}
51+
assert set(info.keys()) == {'iat', 'iss', 'sub', 'kid'}
5252

5353

5454
def test_jwt_pack_and_unpack_with_lifetime():
5555
alice = JWT(own_keys=ALICE_KEYS, iss=ALICE, lifetime=600)
5656
payload = {'sub': 'sub'}
5757
_jwt = alice.pack(payload=payload)
5858

59-
bob = JWT(own_keys=BOB_KEYS, iss=BOB, rec_keys={ALICE: ALICE_KEYS})
59+
bob = JWT(own_keys=BOB_KEYS, iss=BOB, rec_keys={ALICE: ALICE_PUB_KEYS})
6060
info = bob.unpack(_jwt)
6161

62-
assert set(info.keys()) == {'jti', 'iat', 'iss', 'sub', 'kid', 'exp'}
62+
assert set(info.keys()) == {'iat', 'iss', 'sub', 'kid', 'exp'}
6363

6464

6565
def test_jwt_pack_encrypt():
6666
alice = JWT(own_keys=ALICE_KEYS, iss=ALICE, rec_keys={BOB: BOB_PUB_KEYS})
6767
payload = {'sub': 'sub', 'aud': BOB}
6868
_jwt = alice.pack(payload=payload, encrypt=True, recv=BOB)
6969

70-
bob = JWT(own_keys=BOB_KEYS, iss=BOB, rec_keys={ALICE: ALICE_KEYS})
70+
bob = JWT(own_keys=BOB_KEYS, iss=BOB, rec_keys={ALICE: ALICE_PUB_KEYS})
7171
info = bob.unpack(_jwt)
7272

73-
assert set(info.keys()) == {'jti', 'iat', 'iss', 'sub', 'kid', 'aud'}
73+
assert set(info.keys()) == {'iat', 'iss', 'sub', 'kid', 'aud'}
7474

7575

7676
def test_jwt_pack_unpack_sym():

0 commit comments

Comments
 (0)