Commit d4ddafcd authored by Patrick Uiterwijk's avatar Patrick Uiterwijk Committed by Simo Sorce

Implement reimport of previously exported JWKSet

Signed-off-by: default avatarPatrick Uiterwijk <puiterwijk@redhat.com>
Signed-off-by: default avatarSimo Sorce <simo@redhat.com>
Closes #24
parent 0e5766ec
......@@ -505,6 +505,20 @@ class JWKSet(set):
keys.append(json_decode(jwk.export(private_keys)))
return json_encode({'keys': keys})
def import_keyset(self, keyset):
try:
jwkset = json_decode(keyset)
except:
raise InvalidJWKValue()
if 'keys' not in jwkset:
raise InvalidJWKValue()
for jwk in jwkset['keys']:
self.add(JWK(**jwk))
return self
def get_key(self, kid):
"""Gets a key from the set.
:param kid: the 'kid' key identifier.
......
......@@ -216,6 +216,20 @@ class TestJWK(unittest.TestCase):
key = jwk.JWK(generate='EC', curve='P-256', crv='P-521')
key.get_curve('P-521')
def test_jwkset(self):
k = jwk.JWK(**RSAPrivateKey)
ks = jwk.JWKSet()
ks.add(k)
ks2 = jwk.JWKSet().import_keyset(ks.export())
self.assertEqual(len(ks), len(ks2))
self.assertEqual(len(ks), 1)
k1 = ks.get_key(RSAPrivateKey['kid'])
k2 = ks2.get_key(RSAPrivateKey['kid'])
# pylint: disable=protected-access
self.assertEqual(k1._key, k2._key)
# pylint: disable=protected-access
self.assertEqual(k1._key['d'], RSAPrivateKey['d'])
# RFC 7515 - A.1
A1_protected = \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment