πŸ› οΈToolsShed

Pemeriksa Kedaluwarsa JWT

Decode token JWT dan periksa kedaluwarsa serta semua klaim waktu.

Pertanyaan yang Sering Diajukan

Implementasi Kode

import base64
import json
import time

def decode_jwt_payload(token: str) -> dict:
    """Decode JWT payload without verifying signature."""
    parts = token.split(".")
    if len(parts) != 3:
        raise ValueError("Not a valid JWT (expected 3 parts)")
    # Base64url decode: add padding and replace URL-safe chars
    payload_b64 = parts[1].replace("-", "+").replace("_", "/")
    payload_b64 += "=" * (-len(payload_b64) % 4)
    return json.loads(base64.b64decode(payload_b64))

def check_jwt_expiry(token: str):
    payload = decode_jwt_payload(token)
    now = int(time.time())

    exp = payload.get("exp")
    if exp is None:
        print("No exp claim β€” token does not expire")
        return

    remaining = exp - now
    if remaining <= 0:
        print(f"EXPIRED {abs(remaining)} seconds ago")
    else:
        print(f"Valid for {remaining} seconds ({remaining // 60} minutes)")

    if "iat" in payload:
        print(f"Issued at: {time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(payload['iat']))}")
    if "nbf" in payload:
        print(f"Not before: {time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(payload['nbf']))}")

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.