Skip to content

Commit d04af74

Browse files
committed
Use deprecated_alias decorator.
1 parent 2c4e16a commit d04af74

File tree

2 files changed

+184
-46
lines changed

2 files changed

+184
-46
lines changed

src/cryptojwt/key_jar.py

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -204,52 +204,6 @@ def get(self, key_use, key_type="", issuer_id="", kid=None, **kwargs):
204204

205205
return _issuer.get(key_use=key_use, key_type=key_type, kid=kid, **kwargs)
206206

207-
# lst = []
208-
# for bundle in _issuer:
209-
# if key_type:
210-
# if key_use in ['ver', 'dec']:
211-
# _bkeys = bundle.get(key_type, only_active=False)
212-
# else:
213-
# _bkeys = bundle.get(key_type)
214-
# else:
215-
# _bkeys = bundle.keys()
216-
# for key in _bkeys:
217-
# if key.inactive_since and key_use != "sig":
218-
# # Skip inactive keys unless for signature verification
219-
# continue
220-
# if not key.use or use == key.use:
221-
# if kid:
222-
# if key.kid == kid:
223-
# lst.append(key)
224-
# break
225-
# else:
226-
# continue
227-
# else:
228-
# lst.append(key)
229-
#
230-
# # if elliptic curve, have to check if I have a key of the right curve
231-
# if key_type == "EC" and "alg" in kwargs:
232-
# name = "P-{}".format(kwargs["alg"][2:]) # the type
233-
# _lst = []
234-
# for key in lst:
235-
# if name != key.crv:
236-
# continue
237-
# _lst.append(key)
238-
# lst = _lst
239-
#
240-
# if use == 'enc' and key_type == 'oct' and issuer_id != '':
241-
# # Add my symmetric keys
242-
# _issuer = self._get_issuer('')
243-
# if _issuer:
244-
# for kb in _issuer:
245-
# for key in kb.get(key_type):
246-
# if key.inactive_since:
247-
# continue
248-
# if not key.use or key.use == use:
249-
# lst.append(key)
250-
#
251-
# return lst
252-
253207
@deprecated_alias(issuer='issuer_id', owner='issuer_id')
254208
def get_signing_key(self, key_type="", issuer_id="", kid=None, **kwargs):
255209
"""
@@ -805,6 +759,7 @@ def build_keyjar(key_conf, kid_template="", keyjar=None, issuer_id='', storage_c
805759
return keyjar
806760

807761

762+
@deprecated_alias(issuer='issuer_id', owner='issuer_id')
808763
def init_key_jar(public_path='', private_path='', key_defs='', issuer_id='', read_only=True,
809764
storage_conf=None, abstract_storage_cls=None):
810765
"""

tests/test_50_argument_alias.py

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import os
2+
3+
import pytest
4+
5+
from cryptojwt.jws.jws import JWS
6+
from cryptojwt.jws.jws import factory
7+
from cryptojwt.key_jar import build_keyjar
8+
from cryptojwt.key_jar import init_key_jar
9+
10+
__author__ = 'Roland Hedberg'
11+
12+
BASE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
13+
"test_keys"))
14+
RSAKEY = os.path.join(BASE_PATH, "cert.key")
15+
RSA0 = os.path.join(BASE_PATH, "rsa.key")
16+
EC0 = os.path.join(BASE_PATH, "ec.key")
17+
BASEDIR = os.path.abspath(os.path.dirname(__file__))
18+
19+
20+
def full_path(local_file):
21+
return os.path.join(BASEDIR, local_file)
22+
23+
24+
JWK1 = {
25+
"keys": [
26+
{
27+
"n":
28+
"zkpUgEgXICI54blf6iWiD2RbMDCOO1jV0VSff1MFFnujM4othfMsad7H1kRo50YM5S_X9TdvrpdOfpz5aBaKFhT6Ziv0nhtcekq1eRl8"
29+
"mjBlvGKCE5XGk-0LFSDwvqgkJoFYInq7bu0a4JEzKs5AyJY75YlGh879k1Uu2Sv3ZZOunfV1O1Orta"
30+
"-NvS-aG_jN5cstVbCGWE20H0vF"
31+
"VrJKNx0Zf-u-aA-syM4uX7wdWgQ"
32+
"-owoEMHge0GmGgzso2lwOYf_4znanLwEuO3p5aabEaFoKNR4K6GjQcjBcYmDEE4CtfRU9AEmhcD1k"
33+
"leiTB9TjPWkgDmT9MXsGxBHf3AKT5w",
34+
"e": "AQAB", "kty": "RSA", "kid": "rsa1"
35+
},
36+
{
37+
"k":
38+
"YTEyZjBlMDgxMGI4YWU4Y2JjZDFiYTFlZTBjYzljNDU3YWM0ZWNiNzhmNmFlYTNkNTY0NzMzYjE",
39+
"kty": "oct"
40+
},
41+
]
42+
}
43+
44+
KEYDEFS = [
45+
{"type": "RSA", "key": '', "use": ["sig"]},
46+
{"type": "EC", "crv": "P-256", "use": ["sig"]}
47+
]
48+
49+
50+
class TestVerifyJWTKeys(object):
51+
@pytest.fixture(autouse=True)
52+
def setup(self):
53+
mkey = [
54+
{"type": "RSA", "use": ["sig"]},
55+
{"type": "RSA", "use": ["sig"]},
56+
{"type": "RSA", "use": ["sig"]},
57+
]
58+
59+
skey = [
60+
{"type": "RSA", "use": ["sig"]},
61+
]
62+
63+
# Alice has multiple keys
64+
self.alice_keyjar = build_keyjar(mkey)
65+
# Bob has one single keys
66+
self.bob_keyjar = build_keyjar(skey)
67+
self.alice_keyjar['Alice'] = self.alice_keyjar['']
68+
self.bob_keyjar['Bob'] = self.bob_keyjar['']
69+
70+
# To Alice's keyjar add Bob's public keys
71+
self.alice_keyjar.import_jwks(
72+
self.bob_keyjar.export_jwks(issuer='Bob'), 'Bob')
73+
74+
# To Bob's keyjar add Alice's public keys
75+
self.bob_keyjar.import_jwks(
76+
self.alice_keyjar.export_jwks(issuer='Alice'), 'Alice')
77+
78+
_jws = JWS('{"aud": "Bob", "iss": "Alice"}', alg='RS256')
79+
sig_key = self.alice_keyjar.get_signing_key('rsa', owner='Alice')[0]
80+
self.sjwt_a = _jws.sign_compact([sig_key])
81+
82+
_jws = JWS('{"aud": "Alice", "iss": "Bob"}', alg='RS256')
83+
sig_key = self.bob_keyjar.get_signing_key('rsa', owner='Bob')[0]
84+
self.sjwt_b = _jws.sign_compact([sig_key])
85+
86+
def test_no_kid_multiple_keys_no_kid_issuer(self):
87+
a_kids = [k.kid for k in
88+
self.alice_keyjar.get_verify_key(owner='Alice',
89+
key_type='RSA')]
90+
no_kid_issuer = {'Alice': a_kids}
91+
_jwt = factory(self.sjwt_a)
92+
_jwt.jwt.headers['kid'] = ''
93+
keys = self.bob_keyjar.get_jwt_verify_keys(_jwt.jwt,
94+
no_kid_issuer=no_kid_issuer)
95+
assert len(keys) == 3
96+
97+
def test_aud(self):
98+
self.alice_keyjar.import_jwks(JWK1, issuer='D')
99+
self.bob_keyjar.import_jwks(JWK1, issuer='D')
100+
101+
_jws = JWS('{"iss": "D", "aud": "A"}', alg='HS256')
102+
sig_key = self.alice_keyjar.get_signing_key('oct', owner='D')[0]
103+
_sjwt = _jws.sign_compact([sig_key])
104+
105+
no_kid_issuer = {'D': []}
106+
107+
_jwt = factory(_sjwt)
108+
109+
keys = self.bob_keyjar.get_jwt_verify_keys(_jwt.jwt,
110+
no_kid_issuer=no_kid_issuer)
111+
assert len(keys) == 1
112+
113+
114+
PUBLIC_FILE = '{}/public_jwks.json'.format(BASEDIR)
115+
PRIVATE_FILE = '{}/private_jwks.json'.format(BASEDIR)
116+
KEYSPEC = [
117+
{"type": "RSA", "use": ["sig"]},
118+
{"type": "EC", "crv": "P-256", "use": ["sig"]}
119+
]
120+
KEYSPEC_2 = [
121+
{"type": "RSA", "use": ["sig"]},
122+
{"type": "EC", "crv": "P-256", "use": ["sig"]},
123+
{"type": "EC", "crv": "P-384", "use": ["sig"]}
124+
]
125+
126+
127+
def test_init_key_jar_dump_private():
128+
for _file in [PRIVATE_FILE, PUBLIC_FILE]:
129+
if os.path.isfile(_file):
130+
os.unlink(_file)
131+
132+
# New set of keys, JWKSs with keys and public written to file
133+
_keyjar = init_key_jar(private_path=PRIVATE_FILE, key_defs=KEYSPEC, owner='https://example.com')
134+
assert list(_keyjar.owners()) == ['https://example.com']
135+
136+
# JWKS will be read from disc, not created new
137+
_keyjar2 = init_key_jar(private_path=PRIVATE_FILE, key_defs=KEYSPEC)
138+
assert list(_keyjar2.owners()) == ['']
139+
140+
141+
def test_init_key_jar_update():
142+
for _file in [PRIVATE_FILE, PUBLIC_FILE]:
143+
if os.path.isfile(_file):
144+
os.unlink(_file)
145+
146+
# New set of keys, JWKSs with keys and public written to file
147+
_keyjar_1 = init_key_jar(private_path=PRIVATE_FILE, key_defs=KEYSPEC,
148+
owner='https://example.com',
149+
public_path=PUBLIC_FILE, read_only=False)
150+
assert list(_keyjar_1.owners()) == ['https://example.com']
151+
152+
_keyjar_2 = init_key_jar(private_path=PRIVATE_FILE, key_defs=KEYSPEC_2,
153+
public_path=PUBLIC_FILE)
154+
155+
# Both should contain the same RSA key
156+
rsa1 = _keyjar_1.get_signing_key('RSA', 'https://example.com')
157+
rsa2 = _keyjar_2.get_signing_key('RSA', '')
158+
159+
assert len(rsa1) == 1
160+
assert len(rsa2) == 1
161+
assert rsa1[0] == rsa2[0]
162+
163+
# keyjar1 should only contain one EC key while keyjar2 should contain 2.
164+
165+
ec1 = _keyjar_1.get_signing_key('EC', 'https://example.com')
166+
ec2 = _keyjar_2.get_signing_key('EC', '')
167+
assert len(ec1) == 1
168+
assert len(ec2) == 2
169+
170+
# The file on disc should not have changed
171+
_keyjar_3 = init_key_jar(private_path=PRIVATE_FILE)
172+
173+
assert len(_keyjar_3.get_signing_key('RSA')) == 1
174+
assert len(_keyjar_3.get_signing_key('EC')) == 1
175+
176+
_keyjar_4 = init_key_jar(private_path=PRIVATE_FILE, key_defs=KEYSPEC_2,
177+
public_path=PUBLIC_FILE, read_only=False)
178+
179+
# Now it should
180+
_keyjar_5 = init_key_jar(private_path=PRIVATE_FILE)
181+
182+
assert len(_keyjar_5.get_signing_key('RSA')) == 1
183+
assert len(_keyjar_5.get_signing_key('EC')) == 2

0 commit comments

Comments
 (0)