passaggio su postgres del wallet
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from flask import Flask, request, jsonify
|
||||
import os
|
||||
import sqlite3
|
||||
import psycopg2
|
||||
import bcrypt
|
||||
import base64
|
||||
import hashlib
|
||||
@@ -9,20 +9,40 @@ from cryptography.fernet import Fernet
|
||||
from waitress import serve
|
||||
|
||||
# Configurazione
|
||||
db_file = "data/passwords.db"
|
||||
hash_file = "data/master_hash.txt"
|
||||
|
||||
# Configurazione connessione PostgreSQL
|
||||
DB_CONFIG = {
|
||||
"dbname": os.getenv("DB_NAME"),
|
||||
"dbschema": os.getenv("DB_SCHEMA"),
|
||||
"dbtable": os.getenv("DB_TABLE"),
|
||||
"user": os.getenv("DB_USER"),
|
||||
"password": os.getenv("DB_PASSWORD"),
|
||||
"host": os.getenv("DB_HOST"),
|
||||
"port": os.getenv("DB_PORT")
|
||||
}
|
||||
app = Flask(__name__)
|
||||
|
||||
# Configura il logging
|
||||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - PID: %(process)d %(levelname)s - %(message)s')
|
||||
|
||||
def get_db_connection():
|
||||
return psycopg2.connect(
|
||||
dbname=DB_CONFIG["dbname"],
|
||||
user=DB_CONFIG["user"],
|
||||
password=DB_CONFIG["password"],
|
||||
host=DB_CONFIG["host"],
|
||||
port=DB_CONFIG["port"]
|
||||
)
|
||||
|
||||
|
||||
# Inizializza il database
|
||||
def init_db():
|
||||
conn = sqlite3.connect(db_file)
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS passwords (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cursor.execute(f"""
|
||||
CREATE TABLE IF NOT EXISTS {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} (
|
||||
id SERIAL PRIMARY KEY,
|
||||
site TEXT NOT NULL,
|
||||
username TEXT NOT NULL,
|
||||
password TEXT NOT NULL
|
||||
@@ -69,25 +89,26 @@ def authenticate(master_password):
|
||||
|
||||
# Aggiungi una password al database
|
||||
def add_password(site, username, password, cipher):
|
||||
conn = sqlite3.connect(db_file)
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
encrypted_password = cipher.encrypt(password.encode()).decode()
|
||||
try:
|
||||
cursor.execute("INSERT INTO passwords (site, username, password) VALUES (?, ?, ?)",
|
||||
(site, username, encrypted_password))
|
||||
cursor.execute(
|
||||
f"INSERT INTO {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} (site, username, password) VALUES (%s, %s, %s)",
|
||||
(site, username, encrypted_password))
|
||||
conn.commit()
|
||||
logging.info(f"Password aggiunta per il sito: {site}.")
|
||||
except sqlite3.Error as e:
|
||||
except psycopg2.Error as e:
|
||||
logging.error(f"Errore durante l'aggiunta della password: {e}")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
# Recupera una password dal database
|
||||
def get_password(site, cipher):
|
||||
conn = sqlite3.connect(db_file)
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT username, password FROM passwords WHERE site = ?", (site,))
|
||||
cursor.execute(f"SELECT username, password FROM {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} WHERE site = %s", (site,))
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
username, encrypted_password = row
|
||||
@@ -96,7 +117,7 @@ def get_password(site, cipher):
|
||||
return username, decrypted_password
|
||||
logging.warning(f"Sito non trovato: {site}.")
|
||||
return None, None
|
||||
except sqlite3.Error as e:
|
||||
except psycopg2.Error as e:
|
||||
logging.error(f"Errore durante il recupero della password: {e}")
|
||||
return None, None
|
||||
finally:
|
||||
@@ -104,30 +125,30 @@ def get_password(site, cipher):
|
||||
|
||||
# Cancella una password dal database
|
||||
def delete_password(site):
|
||||
conn = sqlite3.connect(db_file)
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("DELETE FROM passwords WHERE site = ?", (site,))
|
||||
cursor.execute(f"DELETE FROM {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']} WHERE site = %s", (site,))
|
||||
if cursor.rowcount > 0:
|
||||
logging.info(f"Password cancellata per il sito: {site}.")
|
||||
else:
|
||||
logging.warning(f"Nessuna password trovata per il sito: {site}.")
|
||||
conn.commit()
|
||||
except sqlite3.Error as e:
|
||||
except psycopg2.Error as e:
|
||||
logging.error(f"Errore durante la cancellazione della password: {e}")
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
# Ottieni la lista di tutti i siti
|
||||
def list_sites():
|
||||
conn = sqlite3.connect(db_file)
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("SELECT site FROM passwords")
|
||||
cursor.execute(f"SELECT site FROM {DB_CONFIG['dbschema']}.{DB_CONFIG['dbtable']}")
|
||||
sites = [row[0] for row in cursor.fetchall()]
|
||||
logging.info("Elenco dei siti recuperato.")
|
||||
return sites
|
||||
except sqlite3.Error as e:
|
||||
except psycopg2.Error as e:
|
||||
logging.error(f"Errore durante il recupero dell'elenco dei siti: {e}")
|
||||
return []
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user