add clientid manag

This commit is contained in:
2024-12-30 11:30:30 +01:00
parent 927578ecd3
commit 4619255a32
2 changed files with 18 additions and 13 deletions

View File

@@ -1,4 +1,5 @@
import paho.mqtt.client as mqtt import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish
import subprocess import subprocess
import argparse import argparse
import requests import requests
@@ -68,7 +69,7 @@ class CurrentClients:
def get_client_list(args, auth): def get_client_list(args, auth):
try: 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: except Exception as e:
logger.error(f"Error publishing client list request: {e}") logger.error(f"Error publishing client list request: {e}")

View File

@@ -46,7 +46,10 @@ def init_db():
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
site TEXT NOT NULL, site TEXT NOT NULL,
username 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() conn.commit()
@@ -89,14 +92,14 @@ def authenticate(master_password):
return auth_success return auth_success
# Aggiungi una password al database # 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() conn = get_db_connection()
cursor = conn.cursor() cursor = conn.cursor()
encrypted_password = cipher.encrypt(password.encode()).decode() encrypted_password = cipher.encrypt(password.encode()).decode()
try: try:
cursor.execute( cursor.execute(
f"INSERT INTO {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} (site, username, password) VALUES (%s, %s, %s)", f"INSERT INTO {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} (site, username, password, client_id) VALUES (%s, %s, %s, %s)",
(site, username, encrypted_password)) (site, username, encrypted_password, client_id))
conn.commit() conn.commit()
logging.info(f"Password aggiunta per il sito: {site}.") logging.info(f"Password aggiunta per il sito: {site}.")
except psycopg2.Error as e: except psycopg2.Error as e:
@@ -109,18 +112,18 @@ def get_password(site, cipher):
conn = get_db_connection() conn = get_db_connection()
cursor = conn.cursor() cursor = conn.cursor()
try: 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() row = cursor.fetchone()
if row: if row:
username, encrypted_password = row username, encrypted_password, client_id = row
decrypted_password = cipher.decrypt(encrypted_password.encode()).decode() decrypted_password = cipher.decrypt(encrypted_password.encode()).decode()
logging.info(f"Password recuperata per il sito: {site}.") 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}.") logging.warning(f"Sito non trovato: {site}.")
return None, None return None, None, None
except psycopg2.Error as e: except psycopg2.Error as e:
logging.error(f"Errore durante il recupero della password: {e}") logging.error(f"Errore durante il recupero della password: {e}")
return None, None return None, None, None
finally: finally:
conn.close() conn.close()
@@ -162,6 +165,7 @@ def add_password_api():
site = request.json.get('site') site = request.json.get('site')
username = request.json.get('username') username = request.json.get('username')
password = request.json.get('password') password = request.json.get('password')
client_id = request.json.get('client_id')
if not authenticate(master_password): if not authenticate(master_password):
logging.warning("Tentativo di aggiungere una password con master password errata.") logging.warning("Tentativo di aggiungere una password con master password errata.")
@@ -169,7 +173,7 @@ def add_password_api():
key = derive_key(master_password) key = derive_key(master_password)
cipher = Fernet(key) cipher = Fernet(key)
add_password(site, username, password, cipher) add_password(site, username, password, client_id, cipher)
return jsonify({"message": "Password aggiunta con successo"}) return jsonify({"message": "Password aggiunta con successo"})
# Endpoint per recuperare una password # Endpoint per recuperare una password
@@ -184,12 +188,12 @@ def get_password_api():
key = derive_key(master_password) key = derive_key(master_password)
cipher = Fernet(key) cipher = Fernet(key)
username, password = get_password(site, cipher) username, password, client_id = get_password(site, cipher)
if username is None: if username is None:
return jsonify({"error": "Sito non trovato"}), 404 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 # Endpoint per cancellare una password
@app.route('/delete', methods=['POST']) @app.route('/delete', methods=['POST'])