Created
September 1, 2019 08:49
-
-
Save kalda341/cfbf8aad65459afd29c37065572c411c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def import_jwk(jwk): | |
""" | |
Used to import a JWK key from the JSON client. | |
""" | |
if (jwk.get('kty') != 'RSA'): | |
raise ValueError('Expected kty to be RSA') | |
def decode_base64(data): | |
# Python is very particular about padding of base64 strings. We'll | |
# just pad the end with zero length data | |
missing_padding = len(data) % 4 | |
data += '=' * (4 - missing_padding) | |
# Isn't it amazing that we can read a BigInt this easy with Python! | |
return int.from_bytes( | |
base64.urlsafe_b64decode(data), | |
byteorder='big', | |
signed=False | |
) | |
def get_component(component): | |
if component in jwk: | |
return decode_base64(jwk[component]) | |
return None | |
# In the order that they are expected by construct | |
components = map(get_component, ['n', 'e', 'd', 'p', 'q', 'u']) | |
# Only include components up until we have seen one which is None | |
return RSA.construct(list(takewhile(lambda x: x is not None, components))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment