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A5RHVYWDFvUTRvaFVBQ3dBRkFBQUFCV1YxWDI1akFIakhLdlpTb3B4bGs2REJYb0xocVlKc1NoMVczQnJvVUZIL2kvd0hqUHpHalE1VjJRaVJlUXdxYzBHOE9wRTRjdXFkNk02QmluUUozdlpaK0JYdWo0SzlwOWdBMDRwZVF3TW5vSjlEY0l4SENlSGFCRmZSdmx5Wk5iZCtBY2RGZUxHRUdLZWNicTM4cUVGNE40bzlLYmRrWWZNUThFYU91bEhrcjh5WkFXWmdwczycllLVFBiNzFzTjVwS09mcU9aRmRvdz09IiwiZW5jX3ZlciI6InYxIn0.x-4ybLikzQ6WQEATSVbffwJGDKrTNYMydBp8RZtSAMuaCL1SUa8Wec7v6OBx0rLN3S4jEXMp9htJRsYZB09L5Q"

def decode_jwt(token):
    """解码 JWT 令牌（不验证签名）"""
    try:
        # 解码 header
        header = jwt.get_unverified_header(token)
        print("Header:")
        print(json.dumps(header, indent=2))
        
        # 解码 payload
        payload = jwt.decode(token, options={"verify_signature": False})
        print("\nPayload:")
        print(json.dumps(payload, indent=2))
        
        # 检查过期时间
        import time
        exp = payload.get('exp', 0)
        iat = payload.get('iat', 0)
        now = int(time.time())
        
        print(f"\n时间信息:")
        print(f"  签发时间 (iat): {iat} ({time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(iat))})")
        print(f"  过期时间 (exp): {exp} ({time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(exp))})")
        print(f"  当前时间：{now} ({time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now))})")
        print(f"  是否过期：{now > exp}")
        
        return payload
    except Exception as e:
        print(f"解码失败：{e}")
        return None

if __name__ == "__main__":
    decode_jwt(USER_ACCESS_TOKEN)
