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

@@ -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'])