import jwt
import base64
import json
from jwt.algorithms import RSAAlgorithm
token = "your_access_token_here"
header_b64, payload_b64, signature_b64 = token.split('.')
header = base64.urlsafe_b64decode(header_b64 + '==').decode('utf-8')
payload = base64.urlsafe_b64decode(payload_b64 + '==').decode('utf-8')
header_json = json.loads(header)
payload_json = json.loads(payload)
print("Header:", header_json)
print("Payload:", payload_json)
public_key_pem = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtd....
-----END PUBLIC KEY-----"""
algorithm = header_json['alg']
try:
decoded = jwt.decode(token, public_key_pem, algorithms=[algorithm])
print("Decoded JWT:", decoded)
except jwt.ExpiredSignatureError:
print("The token has expired")
except jwt.InvalidTokenError:
print("Invalid token")
import jwt
from jwt import ExpiredSignatureError, InvalidSignatureError, DecodeError, ImmatureSignatureError, InvalidTokenError
try:
decoded = jwt.decode(token, public_key_pem, algorithms=["RS256"])
print("Decoded JWT:", decoded)
except ExpiredSignatureError:
print("Error: The token has expired. Please request a new one.")
except InvalidSignatureError:
print("Error: The token's signature is invalid. Check if the correct public key is being used.")
except ImmatureSignatureError:
print("Error: The token is not yet valid (check 'nbf' claim).")
except DecodeError:
print("Error: The token could not be decoded. It might be malformed.")
except InvalidTokenError as e:
print(f"General Invalid Token Error: {e}. The token is invalid for some reason.")