From e0c636bc4cce9a9a412f2d97335b9f67ac8eb71e Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Fri, 23 Sep 2016 10:54:44 +0200 Subject: [PATCH 1/2] add key_from_jwk_dict() --- src/jwkest/jwk.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/jwkest/jwk.py b/src/jwkest/jwk.py index 6c79b2a..de7be1e 100644 --- a/src/jwkest/jwk.py +++ b/src/jwkest/jwk.py @@ -22,6 +22,7 @@ from jwkest import JWKESTException from jwkest import b64d from jwkest import b64e +from jwkest import UnknownAlgorithm from jwkest.ecc import NISTEllipticCurve from jwkest.jwt import b2s_conv @@ -189,6 +190,39 @@ def x509_rsa_load(txt): return [("rsa", import_rsa_key(txt))] +def key_from_jwk_dict(jwk_dict: dict, private: bool=True) -> Key: + """Load JWK from dictionary""" + if jwk_dict['kty'] == 'EC': + if private: + return ECKey(kid=jwk_dict['kid'], + crv=jwk_dict['crv'], + x=jwk_dict['x'], + y=jwk_dict['y'], + d=jwk_dict['d']) + else: + return ECKey(kid=jwk_dict['kid'], + crv=jwk_dict['crv'], + x=jwk_dict['x'], + y=jwk_dict['y']) + elif jwk_dict['kty'] == 'RSA': + if private: + return RSAKey(kid=jwk_dict['kid'], + n=jwk_dict['n'], + e=jwk_dict['e'], + d=jwk_dict['d'], + p=jwk_dict['p'], + q=jwk_dict['q']) + else: + return RSAKey(kid=jwk_dict['kid'], + n=jwk_dict['n'], + e=jwk_dict['e']) + elif jwk_dict['kty'] == 'oct': + return SYMKey(kid=jwk_dict['kid'], + k=jwk_dict['k']) + else: + raise UnknownAlgorithm + + class Key(object): """ Basic JSON Web key class From 6028e0146e8cee6ef796d9f9bbffd08bef0a6e1c Mon Sep 17 00:00:00 2001 From: Jakob Schlyter Date: Fri, 23 Sep 2016 11:01:17 +0200 Subject: [PATCH 2/2] remove types :-( --- src/jwkest/jwk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jwkest/jwk.py b/src/jwkest/jwk.py index de7be1e..b708ebb 100644 --- a/src/jwkest/jwk.py +++ b/src/jwkest/jwk.py @@ -190,7 +190,7 @@ def x509_rsa_load(txt): return [("rsa", import_rsa_key(txt))] -def key_from_jwk_dict(jwk_dict: dict, private: bool=True) -> Key: +def key_from_jwk_dict(jwk_dict, private=True): """Load JWK from dictionary""" if jwk_dict['kty'] == 'EC': if private: