Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
801xAoGADQB84MJe/X8xSUZQzpn2KP/yZ7C517qDJjComGe3mjVxTIT5XAaa1tLy\n\
T4mvpSeYDJkBD8Hxr3fB1YNDWNbgwrNPGZnUTBNhxIsNLPnV8WySiW57LqVXlggH\n\
vjFmyDdU5Hh6ma4q+BeAqbXZSJz0cfkBcBLCSe2gIJ/QJ3YJVQI= \n\
-----END RSA PRIVATE KEY-----"
pub_pem = "-----BEGIN PUBLIC KEY----- \n\
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA4qiw8PWs7PpnnC2BUEoD\n\
RcwXF8pq8XT1/3Hc3cuUJwX/otNefr/Bomr3dtM0ERLN3DrepCXvuzEU5FcJVDUB\n\
3sI+pFtjjLBXD/zJmuL3Afg91J9p79+Dm+43cR6wuKywVJx5DJIdswF6oQDDzhwu\n\
89d2V5x02aXB9LqdXkPwiO0eR5s/xHXgASl+hqDdVL9hLod3iGa9nV7cElCbcl8U\n\
VXNPJnQAfaiKazF+hCdl/syrIh0KCZ5opggsTJibo8qFXBmG4PkT5YbhHE11wYKI\n\
LwZFSvZ9iddRPQK3CtgFiBnXbVwU5t67tn9pMizHgypgsfBoeoyBrpTuc4egSCpj\n\
sQIDAQAB \n\
-----END PUBLIC KEY-----"
priv_key = JWK.from_pem(to_bytes_2and3(priv_pem))
pub_key = JWK.from_pem(to_bytes_2and3(pub_pem))
priv_keys = {
'HS256': {'default': JWK(kty='oct', k=base64url_encode('some random key'))},
'HS384': {'default': JWK(kty='oct', k=base64url_encode('another one'))},
'HS512': {'default': JWK(kty='oct', k=base64url_encode('keys keys keys!'))},
'RS256': {'python-jwt': priv_key},
'RS384': {'python-jwt': priv_key},
'RS512': {'python-jwt': priv_key},
'PS256': {'python-jwt': priv_key},
'PS384': {'python-jwt': priv_key},
'PS512': {'python-jwt': priv_key}
}
pub_keys = {
'HS256': {'default': priv_keys['HS256']['default']},
def main(args):
if os.path.isfile(args.key):
with open(args.key) as f:
pem_data = f.read()
f.closed
key = jwk.JWK.from_pem(pem_data)
else:
key = jwk.JWK.from_uri(args.key)
with open(args.jwt) as f:
raw_jwt = f.read()
f.closed
token = jwt.JWT()
token.deserialize(raw_jwt, key)
def make_jwk_from_pem(private_pem: str) -> dict:
"""Convert a PEM into a JWK
:param private_pem:
:return jwk_dict:
"""
jwk_dict = dict()
try:
key_obj = jwk.JWK.from_pem(private_pem.encode('latin-1'))
except Exception as e:
app.logger.debug('{}'.format(e))
else:
jwk_dict = json.loads(key_obj.export())
jwk_dict['kid'] = key_obj.thumbprint(hashalg=cryptography.hazmat.primitives.hashes.SHA1())
jwk_dict['x5t'] = key_obj.thumbprint(hashalg=cryptography.hazmat.primitives.hashes.SHA1())
jwk_dict['x5t#256'] = key_obj.thumbprint(hashalg=cryptography.hazmat.primitives.hashes.SHA256())
return jwk_dict
def _load_private_key(pem_key_bytes):
return jwk.JWK.from_pem(pem_key_bytes)
def make_token(kid: str, software_statement_id: str, client_scopes: str, token_url: str) -> str:
jwt_iat = int(time.time())
jwt_exp = jwt_iat + 3600
header = dict(alg='RS256', kid=kid, typ='JWT')
claims = dict(
iss=software_statement_id,
sub=software_statement_id,
scopes=client_scopes,
aud=token_url,
jti=str(uuid.uuid4()),
iat=jwt_iat,
exp=jwt_exp
)
token = jwt.JWT(header=header, claims=claims)
key_obj = jwk.JWK.from_pem(cache.get('private_key_pem').encode('latin-1'))
token.make_signed_token(key_obj)
signed_token = token.serialize()
return signed_token