ajout de jwt fonctionnel

pull/6/head
Hugo PRADIER 10 months ago committed by clfreville2
parent 4ab0407bb8
commit d7fc1e8062

@ -18,7 +18,7 @@
"@codemirror/collab": "^6.1.1",
"@codemirror/state": "^6.4.1",
"@fastify/cors": "^9.0.0",
"@fastify/jwt": "^5.0.0",
"@fastify/jwt": "^5.0.1",
"@fastify/type-provider-typebox": "^4.0.0",
"@fastify/websocket": "^10.0.1",
"@sinclair/typebox": "^0.32.9",

@ -1,15 +1,14 @@
import { rebaseUpdates, Update } from "@codemirror/collab";
import { ChangeSet, Text } from "@codemirror/state";
import cors from "@fastify/cors";
import { Type, TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
import websocket, { WebSocket } from "@fastify/websocket";
import bcrypt from "bcrypt";
import { Type, TypeBoxTypeProvider } from "@fastify/type-provider-typebox";
import Fastify, { FastifyReply } from "fastify";
import fastifyJwt from "@fastify/jwt";
import { nanoid } from "nanoid";
import { allocateBuffer, getRunner } from "runner";
import { Pull, Push } from "zeromq";
import { ChangeSet, Text } from "@codemirror/state";
import { Update, rebaseUpdates } from "@codemirror/collab";
import * as db from "./database";
import bcrypt from "bcrypt";
const sender = new Push();
await sender.bind(`tcp://127.0.0.1:5557`);
@ -167,13 +166,6 @@ const database = db.openDatabase();
/* Créer les tables si elles n'existent pas */
db.createTables(database);
fastify.register(fastifyJwt, {
secret: 'supersecret' // Utilisez une clé secrète sécurisée en production
});
const salt = 10;
/* Route pour créer un utilisateur */
fastify.post(
"/users",
@ -189,17 +181,16 @@ fastify.post(
async (request, reply) => {
const { login, password, permissions } = request.body;
bcrypt.hash(password, salt, async (err, hash) => {
if (err) {
reply.send({ success: false });
}
if (!(await db.insertUser(database, login, hash, permissions))) {
// Hashage du mot de passe
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds);
if (!(await db.insertUser(database, login, hashedPassword, permissions))) {
reply.send({ success: false });
} else {
return reply.send({ success: true });
reply.send({ success: true });
}
}
});
},
);
/* Route pour vérifier si un utilisateur existe */
@ -216,16 +207,11 @@ fastify.post(
async (request, reply) => {
const { login, password } = request.body;
const user = await db.verifyUser(database, login);
if (!user) {
return reply.code(401).send({ error: 'Invalid username or password' });
}
const isPasswordValid = await bcrypt.compare(password, user.password);
if(isPasswordValid){
const token = fastify.jwt.sign({ login });
reply.send({ token: token });
if (user === null || !(await bcrypt.compare(password, user.password))) {
reply.send({ success: false });
} else {
reply.code(401).send({ error: 'Invalid username or password' });
reply.send({ success: true });
}
bcrypt.compare(password, user!.password)
@ -254,7 +240,7 @@ fastify.put(
const { newLogin } = request.body;
db.updateUserLogin(database, id, newLogin);
reply.send({ success: true });
},
}
);
/* Route pour mettre à jour le mot de passe d'un utilisateur */
@ -275,9 +261,13 @@ fastify.put(
async (request, reply) => {
const { id } = request.params;
const { newPassword } = request.body;
db.updateUserPassword(database, id, newPassword);
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(newPassword, saltRounds);
await db.updateUserPassword(database, id, hashedPassword);
reply.send({ success: true });
},
}
);
/* Route pour mettre à jour les permissions d'un utilisateur */
@ -300,7 +290,7 @@ fastify.put(
const { newPermissions } = request.body;
await db.updateUserPermissions(database, id, newPermissions);
reply.send({ success: true });
},
}
);
/* Route pour supprimer un utilisateur par son ID */
@ -319,7 +309,7 @@ fastify.delete(
const { id } = request.params;
await db.deleteUserById(database, id);
reply.send({ success: true });
},
}
);
/* Route pour supprimer un utilisateur par son login */
@ -336,7 +326,7 @@ fastify.delete(
const { login } = request.params;
await db.deleteUserByLogin(database, login);
reply.send({ success: true });
},
}
);
/* Route pour supprimer tous les utilisateurs */
@ -367,7 +357,7 @@ fastify.get(
const { id } = request.params;
const user = await db.selectUserById(database, id);
reply.send(user);
},
}
);
/* Route pour récupérer un utilisateur par son login */
@ -384,7 +374,7 @@ fastify.get(
const { login } = request.params;
const user = await db.selectUserByLogin(database, login);
reply.send(user);
},
}
);
/* Route pour créer un language */
@ -402,7 +392,7 @@ fastify.post(
const { designation, version } = request.body;
db.insertLanguage(database, designation, version);
reply.send({ success: true });
},
}
);
/* Route pour mettre à jour la désignation d'un language */
@ -425,7 +415,7 @@ fastify.put(
const { newDesignation } = request.body;
db.updateLanguageDesignation(database, id, newDesignation);
reply.send({ success: true });
},
}
);
/* Route pour mettre à jour la version d'un language */
@ -448,7 +438,7 @@ fastify.put(
const { newVersion } = request.body;
db.updateLanguageVersion(database, id, newVersion);
reply.send({ success: true });
},
}
);
/* Route pour supprimer un language */
@ -467,7 +457,7 @@ fastify.delete(
const { id } = request.params;
db.deleteLanguage(database, id);
reply.send({ success: true });
},
}
);
/* Route pour supprimer tous les languages */
@ -492,7 +482,7 @@ fastify.get(
const { id } = request.params;
const language = await db.selectLanguageById(database, id);
reply.send(language);
},
}
);
/* Route pour récupérer tous les languages */
@ -518,7 +508,7 @@ fastify.post(
const { id_user, link, id_language, code } = request.body;
db.insertWork(database, link, id_user, id_language, code);
reply.send({ success: true });
},
}
);
/* Route pour récupérer tous les works */
@ -549,7 +539,7 @@ fastify.delete(
const { id } = request.params;
db.deleteWork(database, id);
reply.send({ success: true });
},
}
);
/* Route pour récupérer un work par son ID */
@ -568,7 +558,7 @@ fastify.get(
const { id } = request.params;
const work = await db.selectWorkById(database, id);
reply.send(work);
},
}
);
/* Forward output est une fonction asynchrone qui permet de récupérer les messages envoyés par le container et de les renvoyer au client */

Loading…
Cancel
Save