authsquare/app/scripts/tokenguard.py

24 lines
736 B
Python
Raw Permalink Normal View History

2024-06-17 17:31:07 +07:00
from cryptography.hazmat.primitives import serialization
import jwt
def encode(payload, id_rsa, passphrase):
private_key = open(id_rsa, 'r').read()
key = serialization.load_ssh_private_key(private_key.encode(), password=passphrase)
token = jwt.encode(
payload = payload,
key = key,
algorithm = 'RS256'
)
return token
def decode(token, id_rsa):
public_key = open(id_rsa, 'r').read()
key = serialization.load_ssh_public_key(public_key.encode())
header = jwt.get_unverified_header(token)
payload = jwt.decode(
jwt = token,
key = key,
algorithms = [header['alg'], ]
)
return payload