diff --git a/actions/account.json b/actions/account.json index 8916b5d..b260253 100644 --- a/actions/account.json +++ b/actions/account.json @@ -1,7 +1,7 @@ [ { "id": "pierre", - "password": "1234", + "password": "$2b$10$I1FEg.qONWiSG8P8AbjM9eplDluAgyIkkxq/dkvkRd.146Zq60dom", "role": "enseignant", "prenom": "Pierre", "nom": "Dupont", @@ -11,12 +11,12 @@ "rue": "12 rue Marie Curie", "ville": "Troyes", "region": "Grand-Est", - "code_postal": "10000" + "codePostal": "10000" } }, { "id": "jean", - "password": "2345", + "password": "$2b$10$uMzzmySq4xaGExeQSV4.CuJBlmOb3n.WoxdyeVsA77n8J07jbPXIO", "role": "etudiant", "prenom": "Jean", "nom": "Dupont", @@ -26,7 +26,7 @@ "rue": "13 rue Marie Curie", "ville": "Troyes", "region": "Grand-Est", - "code_postal": "10000" + "codePostal": "10000" } } ] \ No newline at end of file diff --git a/actions/ajoutAccount.js b/actions/ajoutAccount.js index e44fdab..1f14734 100644 --- a/actions/ajoutAccount.js +++ b/actions/ajoutAccount.js @@ -3,6 +3,7 @@ import { stdin as input, stdout as output } from "node:process"; import path from "node:path"; import fs from "node:fs/promises"; import { colors } from '../utils/colors.js'; +import {hashPassword} from './passwordManager.js'; export default async function ajoutAccount(rl = null) { console.log("\n Ajout d'un compte : \n"); @@ -19,7 +20,7 @@ export default async function ajoutAccount(rl = null) { const accounts = JSON.parse(data); const id = await rl.question("Identifiant : "); - const password = await rl.question("Mot de passe : "); + const password = await hashPassword(await rl.question("Mot de passe : ")); const role = await demanderRole(rl); const prenom = await rl.question("Prenom : "); const nom = await rl.question("Nom : "); diff --git a/actions/passwordManager.js b/actions/passwordManager.js new file mode 100644 index 0000000..6aa3a11 --- /dev/null +++ b/actions/passwordManager.js @@ -0,0 +1,13 @@ +import bcrypt from 'bcryptjs'; + +export async function hashPassword(password) { + const salt = await bcrypt.genSalt(10); + + const hashedPassword = await bcrypt.hash(password, salt); + return hashedPassword; +} + +export async function checkPassword(plainPassword, hashedPassword) { + const isMatch = await bcrypt.compare(plainPassword, hashedPassword); + return isMatch; +} diff --git a/menuLogin.js b/menuLogin.js index eae18f5..4335d94 100644 --- a/menuLogin.js +++ b/menuLogin.js @@ -5,6 +5,7 @@ import path from "node:path"; import inquirer from "inquirer"; import ajoutAccount from "./actions/ajoutAccount.js"; import { colors } from "./utils/colors.js"; +import {checkPassword} from "./actions/passwordManager.js"; export default async function menuLogin(rl = null) { @@ -40,8 +41,12 @@ export default async function menuLogin(rl = null) { const data = await fs.readFile(path.join(process.cwd(), "actions", "account.json"), "utf-8"); const enseignants = JSON.parse(data); - const user = enseignants.find(e => e.id === id && e.password === password.password); - + let user; + for (const e of enseignants) { + if (e.id === id && await checkPassword(password.password, e.password)) { + user = e; + } + } if (user) { console.log(`${colors.green}\nConnexion réussie ! Bienvenue ${user.prenom} ${user.nom}.${colors.reset}`); diff --git a/package-lock.json b/package-lock.json index 8acb842..2a0cbff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "dependencies": { "@caporal/core": "^2.0.2", + "bcryptjs": "^3.0.3", "colors": "^1.4.0", "inquirer": "^12.11.1", "prompt": "^1.2.0", @@ -563,6 +564,15 @@ "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", "license": "MIT" }, + "node_modules/bcryptjs": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-3.0.3.tgz", + "integrity": "sha512-GlF5wPWnSa/X5LKM1o0wz0suXIINz1iHRLvTS+sLyi7XPbe5ycmYI3DlZqVGZZtDgl4DmasFg7gOB3JYbphV5g==", + "license": "BSD-3-Clause", + "bin": { + "bcrypt": "bin/bcrypt" + } + }, "node_modules/brace-expansion": { "version": "1.1.12", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.12.tgz", diff --git a/package.json b/package.json index 8406e8a..273d01f 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ }, "dependencies": { "@caporal/core": "^2.0.2", + "bcryptjs": "^3.0.3", "colors": "^1.4.0", "inquirer": "^12.11.1", "prompt": "^1.2.0",