import json, hashlib, sys, io, subprocess sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8") cfg_path = r"G:\WEB-SERVER\htdocs\AttendanceHub\ZKTecoSyncService-V2\publish\config\phase1.device.json" with open(cfg_path, "r", encoding="utf-8") as f: cfg = json.load(f) token = cfg.get("upload", {}).get("apiToken", "") local_hash = hashlib.sha256(token.encode("utf-8")).hexdigest() print("LOCAL_TOKEN_SHA256=", local_hash, sep="") print("TOKEN_LENGTH=", len(token), sep="") # Compare against the database without printing any token. The hash column is # what tenant:rotate-token maintains (the plaintext column becomes NULL once a # token is rotated, so SHA2(api_token,256) alone would come back empty and this # script would report "no" for a perfectly healthy setup). Fall back to the old # expression only for installations where the migration has not run yet. q = "SELECT id, is_active, COALESCE(api_token_hash, SHA2(api_token,256)) FROM tenants;" r = subprocess.run( [r"G:\web-server\mysql\bin\mysql.exe", "-uroot", "-h127.0.0.1", "-P", "3306", "--protocol=TCP", "attendance_hub", "-N", "-B", "-e", q], capture_output=True, text=True ) if r.returncode != 0: q = "SELECT id, is_active, SHA2(api_token,256) FROM tenants;" r = subprocess.run( [r"G:\web-server\mysql\bin\mysql.exe", "-uroot", "-h127.0.0.1", "-P", "3306", "--protocol=TCP", "attendance_hub", "-N", "-B", "-e", q], capture_output=True, text=True ) if r.returncode == 0: print("NOTE: api_token_hash column not present; using SHA2(api_token,256) only.") if r.returncode != 0: print("MYSQL_ERROR:", r.stderr.strip()[:500]) sys.exit(1) for line in r.stdout.strip().splitlines(): parts = line.split("\t") tid, active, th = parts[0], parts[1], (parts[2] if len(parts) > 2 else "") match = "MATCH" if (th and th.lower() == local_hash) else "no" print(f"TENANT id={tid} active={active} token_match={match}")