Use alphanumeric API tokens instead of ints

This commit is contained in:
Brett Williams
2023-12-21 20:46:55 -06:00
parent d55f6a5187
commit 685297e2f2
2 changed files with 20 additions and 3 deletions

View File

@@ -25,7 +25,7 @@ from src.engines.engine_manager import EngineManager
from src.render_queue import RenderQueue, JobNotFoundError
from src.utilities.config import Config
from src.utilities.misc_helper import system_safe_path, current_system_os, current_system_cpu, \
current_system_os_version
current_system_os_version, num_to_alphanumeric
from src.utilities.server_helper import generate_thumbnail_for_job
from src.utilities.zeroconf_server import ZeroconfServer
@@ -56,8 +56,8 @@ def jobs_json():
try:
hash_token = request.args.get('token', None)
all_jobs = [x.json() for x in RenderQueue.all_jobs()]
job_cache_token = str(json.dumps(all_jobs).__hash__())
job_cache_int = int(json.dumps(all_jobs).__hash__())
job_cache_token = num_to_alphanumeric(job_cache_int)
if hash_token and hash_token == job_cache_token:
return [], 204 # no need to update
else:

View File

@@ -3,6 +3,7 @@ import os
import platform
import shutil
import socket
import string
import subprocess
from datetime import datetime
@@ -156,3 +157,19 @@ def is_localhost(comparison_hostname):
return comparison_hostname == local_hostname
except AttributeError:
return False
def num_to_alphanumeric(num):
# List of possible alphanumeric characters
characters = string.ascii_letters + string.digits
# Make sure number is positive
num = abs(num)
# Convert number to alphanumeric
result = ""
while num > 0:
num, remainder = divmod(num, len(characters))
result += characters[remainder]
return result[::-1] # Reverse the result to get the correct alphanumeric string