diff --git a/control_mqtt.py b/control_mqtt.py index fd1da3a..8bcd0f3 100755 --- a/control_mqtt.py +++ b/control_mqtt.py @@ -1,4 +1,5 @@ import paho.mqtt.client as mqtt +import paho.mqtt.publish as publish import subprocess import argparse import requests @@ -68,7 +69,7 @@ class CurrentClients: def get_client_list(args, auth): try: - mqtt.publish.single(args.pub_topic, '{"commands":[{"command":"listClients"}]}', hostname=args.host, port=args.port, auth=auth) + publish.single(args.pub_topic, '{"commands":[{"command":"listClients"}]}', hostname=args.host, port=args.port, auth=auth) except Exception as e: logger.error(f"Error publishing client list request: {e}") diff --git a/password_wallet_api.py b/password_wallet_api.py index fbff8a0..f4bf1b0 100644 --- a/password_wallet_api.py +++ b/password_wallet_api.py @@ -46,7 +46,10 @@ def init_db(): id SERIAL PRIMARY KEY, site TEXT NOT NULL, username TEXT NOT NULL, - password TEXT NOT NULL + password TEXT NOT NULL, + client_id TEXT NULL, + created_at timestamptz DEFAULT CURRENT_TIMESTAMP, + CONSTRAINT site_user_clientid_unique UNIQUE(site, username, client_id) ) """) conn.commit() @@ -89,14 +92,14 @@ def authenticate(master_password): return auth_success # Aggiungi una password al database -def add_password(site, username, password, cipher): +def add_password(site, username, password, client_id, cipher): conn = get_db_connection() cursor = conn.cursor() encrypted_password = cipher.encrypt(password.encode()).decode() try: cursor.execute( - f"INSERT INTO {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} (site, username, password) VALUES (%s, %s, %s)", - (site, username, encrypted_password)) + f"INSERT INTO {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} (site, username, password, client_id) VALUES (%s, %s, %s, %s)", + (site, username, encrypted_password, client_id)) conn.commit() logging.info(f"Password aggiunta per il sito: {site}.") except psycopg2.Error as e: @@ -109,18 +112,18 @@ def get_password(site, cipher): conn = get_db_connection() cursor = conn.cursor() try: - cursor.execute(f"SELECT username, password FROM {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} WHERE site = %s", (site,)) + cursor.execute(f"SELECT username, password, client_id FROM {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} WHERE site = %s", (site,)) row = cursor.fetchone() if row: - username, encrypted_password = row + username, encrypted_password, client_id = row decrypted_password = cipher.decrypt(encrypted_password.encode()).decode() logging.info(f"Password recuperata per il sito: {site}.") - return username, decrypted_password + return username, decrypted_password, client_id logging.warning(f"Sito non trovato: {site}.") - return None, None + return None, None, None except psycopg2.Error as e: logging.error(f"Errore durante il recupero della password: {e}") - return None, None + return None, None, None finally: conn.close() @@ -162,6 +165,7 @@ def add_password_api(): site = request.json.get('site') username = request.json.get('username') password = request.json.get('password') + client_id = request.json.get('client_id') if not authenticate(master_password): logging.warning("Tentativo di aggiungere una password con master password errata.") @@ -169,7 +173,7 @@ def add_password_api(): key = derive_key(master_password) cipher = Fernet(key) - add_password(site, username, password, cipher) + add_password(site, username, password, client_id, cipher) return jsonify({"message": "Password aggiunta con successo"}) # Endpoint per recuperare una password @@ -184,12 +188,12 @@ def get_password_api(): key = derive_key(master_password) cipher = Fernet(key) - username, password = get_password(site, cipher) + username, password, client_id = get_password(site, cipher) if username is None: return jsonify({"error": "Sito non trovato"}), 404 - return jsonify({"site": site, "username": username, "password": password}) + return jsonify({"site": site, "username": username, "password": password, "client_id": client_id}) # Endpoint per cancellare una password @app.route('/delete', methods=['POST'])