Refonte authentification en crypté
This commit is contained in:
44
migre_bcrypt.py
Normal file
44
migre_bcrypt.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import os, sys, bcrypt, mysql.connector
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
conn = mysql.connector.connect(
|
||||
host=os.getenv("DB_HOST"),
|
||||
database=os.getenv("DB_NAME"),
|
||||
user=os.getenv("DB_USER"),
|
||||
password=os.getenv("DB_PASSWORD"),
|
||||
)
|
||||
try:
|
||||
cur = conn.cursor(dictionary=True)
|
||||
cur.execute("""
|
||||
SELECT NomUtilisateur, MotDePasse
|
||||
FROM Utilisateurs
|
||||
WHERE MotDePasseHash IS NULL OR MotDePasseHash = ''
|
||||
""")
|
||||
rows = cur.fetchall()
|
||||
print(f"{len(rows)} utilisateur(s) à migrer…")
|
||||
|
||||
for r in rows:
|
||||
login = r["NomUtilisateur"]
|
||||
plain = (r["MotDePasse"] or "").encode("utf-8")
|
||||
if not plain:
|
||||
print(f"- {login}: mot de passe vide — ignoré")
|
||||
continue
|
||||
hashed = bcrypt.hashpw(plain, bcrypt.gensalt(rounds=12)).decode("ascii")
|
||||
cur.execute("""
|
||||
UPDATE Utilisateurs
|
||||
SET MotDePasseHash = %s
|
||||
WHERE NomUtilisateur = %s
|
||||
""", (hashed, login))
|
||||
print(f"- {login}: OK")
|
||||
conn.commit()
|
||||
print("Migration terminée.")
|
||||
except Exception as e:
|
||||
conn.rollback()
|
||||
print("Erreur:", e)
|
||||
sys.exit(1)
|
||||
finally:
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
Reference in New Issue
Block a user