Fix .gitignore, add secure pairing key route, and fix template errors

This commit is contained in:
2025-10-01 20:46:46 +03:00
parent ea6364eb7b
commit b2b225049d
3 changed files with 114 additions and 37 deletions

View File

@@ -1,3 +1,17 @@
import json
from flask import Blueprint
bp = Blueprint('main', __name__)
@bp.route('/get_pairing_keys')
def get_pairing_keys():
"""Return all pairing keys as JSON for client selection."""
keys_path = os.path.join(current_app.instance_path, 'pairing_keys.json')
try:
with open(keys_path, 'r') as f:
keys = json.load(f)
except Exception as e:
print(f"Error loading pairing keys: {e}")
return jsonify({'success': False, 'error': str(e), 'pairing_keys': []}), 500
return jsonify({'success': True, 'pairing_keys': keys})
import os
import mariadb
from datetime import datetime, timedelta
@@ -932,10 +946,65 @@ def upload_data():
def print_module():
return render_template('print_module.html')
import secrets
from datetime import datetime, timedelta
@bp.route('/generate_pairing_key', methods=['POST'])
def generate_pairing_key():
"""Generate a secure pairing key for a printer and store it."""
printer_name = request.form.get('printer_name', '').strip()
if not printer_name:
flash('Printer name is required.', 'danger')
return redirect(url_for('main.download_extension'))
# Generate a secure random key
pairing_key = secrets.token_urlsafe(32)
warranty_until = (datetime.utcnow() + timedelta(days=365)).strftime('%Y-%m-%d')
# Load existing keys
keys_path = os.path.join(current_app.instance_path, 'pairing_keys.json')
try:
if os.path.exists(keys_path):
with open(keys_path, 'r') as f:
keys = json.load(f)
else:
keys = []
except Exception as e:
keys = []
# Add new key
keys.append({
'printer_name': printer_name,
'pairing_key': pairing_key,
'warranty_until': warranty_until
})
# Save updated keys
with open(keys_path, 'w') as f:
json.dump(keys, f, indent=2)
# Pass new key and all keys to template
return render_template('download_extension.html',
pairing_key=pairing_key,
printer_name=printer_name,
warranty_until=warranty_until,
pairing_keys=keys)
@bp.route('/download_extension')
def download_extension():
"""Route for downloading the Chrome extension"""
return render_template('download_extension.html')
# Load all pairing keys for display
keys_path = os.path.join(current_app.instance_path, 'pairing_keys.json')
try:
if os.path.exists(keys_path):
with open(keys_path, 'r') as f:
keys = json.load(f)
else:
keys = []
except Exception as e:
keys = []
return render_template('download_extension.html', pairing_keys=keys)
@bp.route('/extension_files/<path:filename>')
def extension_files(filename):