ajout de jwt fonctionnel
continuous-integration/drone/push Build is failing Details
continuous-integration/drone/pr Build is failing Details

Hugo PRADIER 10 months ago
parent f2ed1a86b9
commit 87190e9362

@ -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`);
@ -63,7 +62,7 @@ fastify.get("/live", { websocket: true }, (socket, req) => {
received.map((update: any) => ({
clientID: update.clientID,
changes: update.changes.toJSON(),
})),
}))
);
} else if (data.type == "getDocument") {
send(socket, requestId, { version: updates.length, doc: doc.toString() });
@ -104,7 +103,7 @@ fastify.post(
reply.raw.write("id: 0\n\n");
});
clients[jobId] = reply;
},
}
);
/* Création du répertoire de la base de données s'il n'existe pas */
@ -116,13 +115,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",
@ -138,17 +130,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))) {
reply.send({ success: false });
} else {
return reply.send({ success: true });
}
});
},
// 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 {
reply.send({ success: true });
}
}
);
/* Route pour vérifier si un utilisateur existe */
@ -165,19 +156,13 @@ 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 });
}
},
}
);
/* Route pour mettre à jour le login d'un utilisateur */
@ -200,7 +185,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 */
@ -221,9 +206,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 */
@ -246,7 +235,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 */
@ -265,7 +254,7 @@ fastify.delete(
const { id } = request.params;
await db.deleteUserById(database, id);
reply.send({ success: true });
},
}
);
/* Route pour supprimer un utilisateur par son login */
@ -282,7 +271,7 @@ fastify.delete(
const { login } = request.params;
await db.deleteUserByLogin(database, login);
reply.send({ success: true });
},
}
);
/* Route pour supprimer tous les utilisateurs */
@ -313,7 +302,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 */
@ -330,7 +319,7 @@ fastify.get(
const { login } = request.params;
const user = await db.selectUserByLogin(database, login);
reply.send(user);
},
}
);
/* Route pour créer un language */
@ -348,7 +337,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 */
@ -371,7 +360,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 */
@ -394,7 +383,7 @@ fastify.put(
const { newVersion } = request.body;
db.updateLanguageVersion(database, id, newVersion);
reply.send({ success: true });
},
}
);
/* Route pour supprimer un language */
@ -413,7 +402,7 @@ fastify.delete(
const { id } = request.params;
db.deleteLanguage(database, id);
reply.send({ success: true });
},
}
);
/* Route pour supprimer tous les languages */
@ -438,7 +427,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 */
@ -464,7 +453,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 */
@ -495,7 +484,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 */
@ -514,7 +503,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