import jwt from datetime import datetime payload_data = { 'sub': '4242', 'iss': 'issuer', # issuer of the token 'name': 'Jessica Temporal', 'nickname': 'Jess', 'exp': int(datetime.now().timestamp()) + 5 } key = 'secret' token = jwt.encode( payload=payload_data, key=key ) print(token) header = jwt.get_unverified_header(token) print(header) decoded_payload = jwt.decode( jwt=token, key=key, algorithms=[header['alg']] ) print(decoded_payload)
import asyncio, typing, types import os import random from contextlib import contextmanager, asynccontextmanager from urllib.request import urlopen import jwt from datetime import datetime from cryptography.hazmat.primitives import serialization from jwt.exceptions import ExpiredSignatureError payload_data = { 'sub': '4242', 'name': 'Jessica Temporal', 'nickname': 'Jess', 'exp': int(datetime.now().timestamp())+5 } private_key = open(os.path.expanduser('~/.ssh/id_rsa'), 'r').read() public_key = open(os.path.expanduser('~/.ssh/id_rsa.pub'), 'r').read() private_key_obj = serialization.load_ssh_private_key(private_key.encode(), password=b'') print(private_key_obj) public_key_obj = serialization.load_ssh_public_key(public_key.encode()) print(public_key_obj) secret = 'my_super_secret' token = jwt.encode( payload=payload_data, key=private_key_obj, # have to use private key sign algorithm='RS256' ) print('~' * 80) print(token) header = jwt.get_unverified_header(token) print(header) try: payload = jwt.decode( jwt=token, key=public_key_obj, # have to use public key verify algorithms=[header['alg']] ) print(payload) except ExpiredSignatureError as e: print(e, e.args)
标签:Python,jwt,token,key,print,import,payload From: https://www.cnblogs.com/dissipate/p/17464089.html