Skip to content

Commit

Permalink
Cleaning
Browse files Browse the repository at this point in the history
cleaned up comments from old refresh portion of the script

cleaned up phrasing on messages returned to users
  • Loading branch information
LucifersCircle committed Dec 8, 2024
1 parent 8a5efb8 commit 20773f4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 26 deletions.
40 changes: 20 additions & 20 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ def initialize_db():
<div class="container">
<h1>WebOS Token Refresher</h1>
<form action="/" method="post">
<input type="text" name="key" placeholder="Enter your key" required>
<button type="submit" name="action" value="add">Add Key</button>
<button type="submit" name="action" value="remove" class="remove">Remove Key</button>
<input type="text" name="key" placeholder="Enter your token" required>
<button type="submit" name="action" value="add">Add Token</button>
<button type="submit" name="action" value="remove" class="remove">Remove Token</button>
</form>
{% if message %}
<div class="message
{% if 'Duplicate' in message %}duplicate{% endif %}
{% if 'Invalid key format' in message %}invalid{% endif %}
{% if 'duplicate' in message %}duplicate{% endif %}
{% if 'invalid token format' in message %}invalid{% endif %}
{% if 'not found in database' in message %}not-found{% endif %}
">{{ message }}</div>
{% endif %}
Expand Down Expand Up @@ -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:
Expand All @@ -194,47 +194,47 @@ 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())
conn.execute('INSERT INTO keys (encrypted_key, key_hash) VALUES (?, ?)', (encrypted_key, key_hash))
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':
cursor = conn.execute("SELECT COUNT(*) FROM keys WHERE key_hash = ?", (key_hash,))
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
Expand Down
14 changes: 8 additions & 6 deletions task_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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:
Expand Down

0 comments on commit 20773f4

Please sign in to comment.