Skip to content

Commit

Permalink
Merge pull request #71 from jschlyter/jwk_from_dict
Browse files Browse the repository at this point in the history
Implement key_from_jwk_dict()
  • Loading branch information
rohe authored Sep 24, 2016
2 parents 954362c + 6028e01 commit a5549af
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/jwkest/jwk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -189,6 +190,39 @@ def x509_rsa_load(txt):
return [("rsa", import_rsa_key(txt))]


def key_from_jwk_dict(jwk_dict, private=True):
"""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
Expand Down

0 comments on commit a5549af

Please sign in to comment.