pull/6/head
Bastien OLLIER 11 months ago committed by clfreville2
parent 689d1c8944
commit d1544f1773

@ -4,11 +4,13 @@
"type": "module", "type": "module",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc",
"start": "tsx src/server.ts" "start": "tsx src/server.ts",
"fmt": "dprint fmt"
}, },
"devDependencies": { "devDependencies": {
"@types/bcryptjs": "^2.4.6", "@types/bcryptjs": "^2.4.6",
"@types/bun": "^1.0.4", "@types/bun": "^1.0.4",
"dprint": "^0.46.2",
"tsx": "^4.7.0", "tsx": "^4.7.0",
"typescript": "^5.3.3" "typescript": "^5.3.3"
}, },
@ -20,6 +22,7 @@
"@fastify/websocket": "^10.0.1", "@fastify/websocket": "^10.0.1",
"@sinclair/typebox": "^0.32.9", "@sinclair/typebox": "^0.32.9",
"dprint": "^0.46.1", "dprint": "^0.46.1",
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"fastify": "^4.27.0", "fastify": "^4.27.0",
"nanoid": "^5.0.4", "nanoid": "^5.0.4",

@ -1,18 +0,0 @@
import * as bcrypt from "bcryptjs";
const saltRounds = 10; // Le nombre de tours de salage
/* Fonction pour hasher le mot de passe */
export async function hashPassword(password: string): Promise<string> {
const hashedPassword = await bcrypt.hash(password, saltRounds);
return hashedPassword;
}
/* Fonction pour vérifier le mot de passe */
export async function comparePassword(
plainPassword: string,
hashedPassword: string
): Promise<boolean> {
const isMatch = await bcrypt.compare(plainPassword, hashedPassword);
return isMatch;
}

@ -141,7 +141,7 @@ export async function insertUser(
/* Vérifier si un utilisateur existe dans la table registered_user */ /* Vérifier si un utilisateur existe dans la table registered_user */
export async function verifyUser( export async function verifyUser(
db: sqlite3.Database, db: sqlite3.Database,
login: string login: string,
): Promise<User | null> { ): Promise<User | null> {
const verifyUserQuery = `SELECT login, password FROM registered_user WHERE login = ?`; const verifyUserQuery = `SELECT login, password FROM registered_user WHERE login = ?`;

@ -3,12 +3,12 @@ import { ChangeSet, Text } from "@codemirror/state";
import cors from "@fastify/cors"; import cors from "@fastify/cors";
import { Type, TypeBoxTypeProvider } from "@fastify/type-provider-typebox"; import { Type, TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
import websocket, { WebSocket } from "@fastify/websocket"; import websocket, { WebSocket } from "@fastify/websocket";
import bcrypt from "bcrypt";
import Fastify, { FastifyReply } from "fastify"; import Fastify, { FastifyReply } from "fastify";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import { allocateBuffer, getRunner } from "runner"; import { allocateBuffer, getRunner } from "runner";
import { Pull, Push } from "zeromq"; import { Pull, Push } from "zeromq";
import * as db from "./database"; import * as db from "./database";
import { hashPassword } from "bcrypt";
const sender = new Push(); const sender = new Push();
await sender.bind(`tcp://127.0.0.1:5557`); await sender.bind(`tcp://127.0.0.1:5557`);
@ -166,6 +166,8 @@ const database = db.openDatabase();
/* Créer les tables si elles n'existent pas */ /* Créer les tables si elles n'existent pas */
db.createTables(database); db.createTables(database);
const salt = 10;
/* Route pour créer un utilisateur */ /* Route pour créer un utilisateur */
fastify.post( fastify.post(
"/users", "/users",
@ -180,14 +182,18 @@ fastify.post(
}, },
async (request, reply) => { async (request, reply) => {
const { login, password, permissions } = request.body; const { login, password, permissions } = request.body;
// Hasher le mot de passe avant de l'insérer dans la base de données (en type string)
const hashedPassword = (await hashPassword(password)) as string; bcrypt.hash(password, salt, async (err, hash) => {
if (!(await db.insertUser(database, login, hashedPassword, permissions))) { if (err) {
reply.send({ success: false }); reply.send({ success: false });
} else { }
reply.send({ success: true }); if (!(await db.insertUser(database, login, hash, permissions))) {
} reply.send({ success: false });
} } else {
reply.send({ success: true });
}
});
},
); );
/* Route pour vérifier si un utilisateur existe */ /* Route pour vérifier si un utilisateur existe */
@ -205,12 +211,10 @@ fastify.post(
const { login, password } = request.body; const { login, password } = request.body;
const user = await db.verifyUser(database, login); const user = await db.verifyUser(database, login);
if (user === null || user.password !== password) { bcrypt.compare(password, user!.password)
reply.send({ success: false }); .then(res => reply.send({ sucess: res }))
} else { .catch(err => reply.send({ sucess: false }));
reply.send({ success: true }); },
}
}
); );
/* Route pour mettre à jour le login d'un utilisateur */ /* Route pour mettre à jour le login d'un utilisateur */

Loading…
Cancel
Save