<s> import json <EOL> import os <EOL> from jwt . utils import base64url_decode <EOL> from tests . utils import ensure_bytes , int_from_bytes <EOL> BASE_PATH = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> def decode_value ( val ) : <EOL> decoded = base64url_decode ( ensure_bytes ( val ) ) <EOL> return int_from_bytes ( decoded , '<STR_LIT>' ) <EOL> def load_hmac_key ( ) : <EOL> with open ( os . path . join ( BASE_PATH , '<STR_LIT>' ) , '<STR_LIT:r>' ) as infile : <EOL> keyobj = json . load ( infile ) <EOL> return base64url_decode ( ensure_bytes ( keyobj [ '<STR_LIT:k>' ] ) ) <EOL> try : <EOL> from cryptography . hazmat . primitives . asymmetric import rsa <EOL> from cryptography . hazmat . primitives . asymmetric import ec <EOL> from cryptography . hazmat . backends import default_backend <EOL> has_crypto = True <EOL> except ImportError : <EOL> has_crypto = False <EOL> if has_crypto : <EOL> def load_rsa_key ( ) : <EOL> with open ( os . path . join ( BASE_PATH , '<STR_LIT>' ) , '<STR_LIT:r>' ) as infile : <EOL> keyobj = json . load ( infile ) <EOL> return rsa . RSAPrivateNumbers ( <EOL> p = decode_value ( keyobj [ '<STR_LIT:p>' ] ) , <EOL> q = decode_value ( keyobj [ '<STR_LIT:q>' ] ) , <EOL> d = decode_value ( keyobj [ '<STR_LIT:d>' ] ) , <EOL> dmp1 = decode_value ( keyobj [ '<STR_LIT>' ] ) , <EOL> dmq1 = decode_value ( keyobj [ '<STR_LIT>' ] ) , <EOL> iqmp = decode_value ( keyobj [ '<STR_LIT>' ] ) , <EOL> public_numbers = load_rsa_pub_key ( ) . public_numbers ( ) <EOL> ) . private_key ( default_backend ( ) ) <EOL> def load_rsa_pub_key ( ) : <EOL> with open ( os . path . join ( BASE_PATH , '<STR_LIT>' ) , '<STR_LIT:r>' ) as infile : <EOL> keyobj = json . load ( infile ) <EOL> return rsa . RSAPublicNumbers ( <EOL> n = decode_value ( keyobj [ '<STR_LIT:n>' ] ) , <EOL> e = decode_value ( keyobj [ '<STR_LIT:e>' ] ) <EOL> ) . public_key ( default_backend ( ) ) <EOL> def load_ec_key ( ) : <EOL> with open ( os . path . join ( BASE_PATH , '<STR_LIT>' ) , '<STR_LIT:r>' ) as infile : <EOL> keyobj = json . load ( infile ) <EOL> return ec . EllipticCurvePrivateNumbers ( <EOL> private_value = decode_value ( keyobj [ '<STR_LIT:d>' ] ) , <EOL> public_numbers = load_ec_pub_key ( ) . public_numbers ( ) <EOL> ) <EOL> def load_ec_pub_key ( ) : <EOL> with open ( os . path . join ( BASE_PATH , '<STR_LIT>' ) , '<STR_LIT:r>' ) as infile : <EOL> keyobj = json . load ( infile ) <EOL> return ec . EllipticCurvePublicNumbers ( <EOL> x = decode_value ( keyobj [ '<STR_LIT:x>' ] ) , <EOL> y = decode_value ( keyobj [ '<STR_LIT:y>' ] ) , <EOL> curve = ec . SECP521R1 ( ) <EOL> ) . public_key ( default_backend ( ) ) </s>
