Skip to content

Commit

Permalink
Fix JWK.from_json #130
Browse files Browse the repository at this point in the history
import_keyset no longer returns self.
  • Loading branch information
JonathanHuot authored and simo5 committed Oct 31, 2018
1 parent 3c9000f commit ea21d34
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
8 changes: 4 additions & 4 deletions jwcrypto/jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,8 @@ def from_json(cls, key):
jkey = json_decode(key)
except Exception as e: # pylint: disable=broad-except
raise InvalidJWKValue(e)
return obj.import_key(**jkey)
obj.import_key(**jkey)
return obj

def export(self, private_key=True):
"""Exports the key in the standard JSON format.
Expand Down Expand Up @@ -854,16 +855,15 @@ def import_keyset(self, keyset):
else:
self[k] = v

return self

@classmethod
def from_json(cls, keyset):
"""Creates a RFC 7517 keyset from the standard JSON format.
:param keyset: The RFC 7517 representation of a JOSE Keyset.
"""
obj = cls()
return obj.import_keyset(keyset)
obj.import_keyset(keyset)
return obj

def get_key(self, kid):
"""Gets a key from the set.
Expand Down
8 changes: 7 additions & 1 deletion jwcrypto/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,17 @@ def test_import_pyca_keys(self):
self.assertRaises(jwk.InvalidJWKValue,
jwk.JWK.from_pyca, dict())

def test_jwk_from_json(self):
k = jwk.JWK.generate(kty='oct', size=256)
y = jwk.JWK.from_json(k.export())
self.assertEqual(k.export(), y.export())

def test_jwkset(self):
k = jwk.JWK(**RSAPrivateKey)
ks = jwk.JWKSet()
ks.add(k)
ks2 = jwk.JWKSet().import_keyset(ks.export())
ks2 = jwk.JWKSet()
ks2.import_keyset(ks.export())
self.assertEqual(len(ks), len(ks2))
self.assertEqual(len(ks), 1)
k1 = ks.get_key(RSAPrivateKey['kid'])
Expand Down

0 comments on commit ea21d34

Please sign in to comment.