From 20773f41dec89e6740d4940bb6caf37a8897986c Mon Sep 17 00:00:00 2001 From: LucifersCircle Date: Sun, 8 Dec 2024 11:57:09 -0800 Subject: [PATCH] Cleaning cleaned up comments from old refresh portion of the script cleaned up phrasing on messages returned to users --- app.py | 40 ++++++++++++++++++++-------------------- task_runner.py | 14 ++++++++------ 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/app.py b/app.py index e162695..df6b40e 100644 --- a/app.py +++ b/app.py @@ -139,14 +139,14 @@ def initialize_db():

WebOS Token Refresher

- - - + + +
{% if message %}
{{ message }}
{% endif %} @@ -174,15 +174,15 @@ def manage_key(): if not key: if is_api_request: - return jsonify({'error': 'Key is required'}), 400 - message = "Key is required." + return jsonify({'error': 'token is required'}), 400 + message = "token is required" return render_template_string(HTML_TEMPLATE, message=message) # Validate the key format if not re.fullmatch(r'^[a-fA-F0-9]{64}$', key): if is_api_request: - return jsonify({'error': 'Invalid key format. Only 64-character alphanumeric keys are allowed.'}), 400 - message = "Invalid key format. Only 64-character alphanumeric keys are allowed." + return jsonify({'error': 'invalid token format - accepts 64-character alphanumeric tokens'}), 400 + message = "invalid token format - accepts 64-character alphanumeric tokens" return render_template_string(HTML_TEMPLATE, message=message) try: @@ -194,8 +194,8 @@ def manage_key(): if cursor.fetchone()[0] > 0: conn.close() if is_api_request: - return jsonify({'error': 'Duplicate key hash detected.'}), 409 - message = "Duplicate key hash detected." + return jsonify({'error': 'duplicate token hash detected - rejecting'}), 409 + message = "duplicate token hash detected - rejecting" return render_template_string(HTML_TEMPLATE, message=message) encrypted_key = cipher.encrypt(key.encode()) @@ -203,8 +203,8 @@ def manage_key(): conn.commit() conn.close() if is_api_request: - return jsonify({'message': 'Key added successfully.'}), 201 - message = "Key encrypted and added successfully." + return jsonify({'message': 'token added successfully'}), 201 + message = "token added successfully" return render_template_string(HTML_TEMPLATE, message=message) elif action == 'remove': @@ -212,29 +212,29 @@ def manage_key(): if cursor.fetchone()[0] == 0: conn.close() if is_api_request: - return jsonify({'error': 'Key hash not found in database.'}), 404 - message = "Key hash not found in database." + return jsonify({'error': 'token hash not found in database'}), 404 + message = "token hash not found in database" return render_template_string(HTML_TEMPLATE, message=message) conn.execute("DELETE FROM keys WHERE key_hash = ?", (key_hash,)) conn.commit() conn.close() if is_api_request: - return jsonify({'message': 'Key removed successfully.'}), 200 - message = "Key removed successfully." + return jsonify({'message': 'token removed successfully'}), 200 + message = "token removed successfully" return render_template_string(HTML_TEMPLATE, message=message) else: if is_api_request: - return jsonify({'error': 'Invalid action.'}), 400 - message = "Invalid action." + return jsonify({'error': 'invalid action'}), 400 + message = "invalid action" return render_template_string(HTML_TEMPLATE, message=message) except Exception as e: print(f"Error managing key: {e}", flush=True) if is_api_request: return jsonify({'error': str(e)}), 500 - message = f"An error occurred: {e}" + message = f"an error occurred: {e}" return render_template_string(HTML_TEMPLATE, message=message) # Render HTML for GET requests diff --git a/task_runner.py b/task_runner.py index 761e7ba..fb223b9 100644 --- a/task_runner.py +++ b/task_runner.py @@ -16,12 +16,8 @@ raise RuntimeError("ENCRYPTION_KEY environment variable is not set.") cipher = Fernet(encryption_key.encode()) -def mask_token(token): - """Masks the token, showing only the last 4 characters.""" - return f"***{token[-4:]}" - +# Fetch and decrypt all keys from the database def fetch_keys(): - """Fetch and decrypt all keys from the database.""" try: conn = sqlite3.connect(db_file) cursor = conn.execute("SELECT encrypted_key FROM keys") @@ -34,8 +30,12 @@ def fetch_keys(): print(f"Error fetching keys: {e}", flush=True) return [] +# Masks the token for logs, showing only the last 4 characters +def mask_token(token): + return f"***{token[-4:]}" + +# Send the request to the API using the token def send_request(token): - """Send the request to the API using the token.""" url = f"{base_url}{token}" try: response = requests.get(url) @@ -44,6 +44,8 @@ def send_request(token): except requests.RequestException as e: print(f"Failed to send request for token {mask_token(token)}: {e}", flush=True) + + if __name__ == "__main__": print("Starting task_runner...", flush=True) while True: