Skip to content
This repository was archived by the owner on Apr 8, 2026. It is now read-only.

Commit b46ad81

Browse files
author
BuildTools
committed
refactor(WebAuthnController): improve code formatting and logging consistency
1 parent 2ed6d45 commit b46ad81

3 files changed

Lines changed: 142 additions & 56 deletions

File tree

dist/controllers/WebAuthnController.js

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,53 +31,56 @@ let WebAuthn = class WebAuthn {
3131
if (metadata)
3232
requestBody.metadata = metadata;
3333
await this.logService.createLog({
34-
ip_address: req.headers["x-real-ip"] || req.socket.remoteAddress,
34+
ip_address: req.headers["x-real-ip"] ||
35+
req.socket.remoteAddress,
3536
table_name: tableName,
3637
controller: `WebAuthnController.${action}`,
3738
original_path: req.originalUrl,
3839
http_method: req.method,
3940
request_body: requestBody,
4041
user_id: userId,
41-
status_code: statusCode
42+
status_code: statusCode,
4243
});
4344
}
4445
catch (error) {
4546
// Ne jamais bloquer la route sur une erreur de log
46-
console.error('Error creating log:', error);
47+
console.error("Error creating log:", error);
4748
}
4849
}
4950
async getRegistrationOptions(req, res) {
5051
const userId = req.body.userId;
5152
if (!userId) {
52-
await this.createLog(req, 'getRegistrationOptions', 'users', 400);
53+
await this.createLog(req, "getRegistrationOptions", "users", 400);
5354
return res.status(400).json({ message: "User ID is required" });
5455
}
5556
try {
5657
const options = await (0, webauthnService_1.getRegistrationOptions)(userId);
5758
// Encode challenge en base64 pour le front
58-
const challengeBase64 = Buffer.from(options.challenge).toString('base64');
59+
const challengeBase64 = Buffer.from(options.challenge).toString("base64");
5960
await this.userService.updateWebauthnChallenge(userId, challengeBase64); // <-- stocke en base64
6061
options.challenge = challengeBase64;
61-
options.user.id = Buffer.from(options.user.id).toString('base64');
62-
await this.createLog(req, 'getRegistrationOptions', 'users', 200, userId);
62+
options.user.id = Buffer.from(options.user.id).toString("base64");
63+
await this.createLog(req, "getRegistrationOptions", "users", 200, userId);
6364
res.status(200).json(options);
6465
}
6566
catch (e) {
66-
await this.createLog(req, 'getRegistrationOptions', 'users', 500, undefined, { error: e.message });
67-
res.status(500).json({ message: "Error generating registration options" });
67+
await this.createLog(req, "getRegistrationOptions", "users", 500, undefined, { error: e.message });
68+
res
69+
.status(500)
70+
.json({ message: "Error generating registration options" });
6871
}
6972
}
7073
async verifyRegistration(req, res) {
7174
const { credential, userId } = req.body;
7275
if (!credential) {
73-
await this.createLog(req, 'verifyRegistration', 'users', 400, userId);
76+
await this.createLog(req, "verifyRegistration", "users", 400, userId);
7477
return res.status(400).json({ message: "Credential is required" });
7578
}
7679
try {
7780
const user = await this.userService.getUser(userId);
7881
const expectedChallenge = user?.webauthn_challenge;
7982
if (!expectedChallenge) {
80-
await this.createLog(req, 'verifyRegistration', 'users', 400, userId);
83+
await this.createLog(req, "verifyRegistration", "users", 400, userId);
8184
return res.status(400).json({ message: "No challenge found" });
8285
}
8386
// Si tu stockes en base64, il faut le convertir en base64url si le front utilise base64url
@@ -93,16 +96,20 @@ let WebAuthn = class WebAuthn {
9396
name: credential.name || "Default Name",
9497
created_at: new Date(),
9598
});
96-
await this.createLog(req, 'verifyRegistration', 'users', 200, userId);
99+
await this.createLog(req, "verifyRegistration", "users", 200, userId);
97100
return res.status(200).json({ message: "Registration successful" });
98101
}
99102
else {
100-
await this.createLog(req, 'verifyRegistration', 'users', 400, userId);
101-
return res.status(400).json({ message: "Registration verification failed" });
103+
await this.createLog(req, "verifyRegistration", "users", 400, userId);
104+
return res
105+
.status(400)
106+
.json({ message: "Registration verification failed" });
102107
}
103108
}
104109
catch (error) {
105-
await this.createLog(req, 'verifyRegistration', 'users', 500, userId, { error: error.message });
110+
await this.createLog(req, "verifyRegistration", "users", 500, userId, {
111+
error: error.message,
112+
});
106113
res.status(500).json({ message: "Error verifying registration" });
107114
}
108115
}
@@ -119,25 +126,33 @@ let WebAuthn = class WebAuthn {
119126
credentials = [];
120127
}
121128
const options = await (0, webauthnService_1.getAuthenticationOptions)(credentials);
122-
const challengeBase64 = Buffer.from(options.challenge).toString('base64');
123-
await this.userService.updateWebauthnChallenge(userId, challengeBase64);
129+
const challengeBase64 = Buffer.from(options.challenge).toString("base64");
130+
if (userId) {
131+
await this.userService.updateWebauthnChallenge(userId, challengeBase64);
132+
}
124133
options.challenge = challengeBase64;
125-
await this.createLog(req, 'getAuthenticationOptionsHandler', 'users', 200, userId);
134+
await this.createLog(req, "getAuthenticationOptionsHandler", "users", 200, userId);
126135
res.status(200).json(options);
127136
}
128137
catch (error) {
129-
await this.createLog(req, 'getAuthenticationOptionsHandler', 'users', 500, userId, { error: error.message });
130-
res.status(500).json({ message: "Error generating authentication options" });
138+
console.error("Error generating authentication options:", error);
139+
await this.createLog(req, "getAuthenticationOptionsHandler", "users", 500, userId, { error: error.message });
140+
res
141+
.status(500)
142+
.json({ message: "Error generating authentication options" });
131143
}
132144
}
133145
async verifyAuthenticationHandler(req, res) {
134146
const { credential, userId } = req.body;
135147
if (!credential) {
136-
await this.createLog(req, 'verifyAuthenticationHandler', 'users', 400, userId);
148+
await this.createLog(req, "verifyAuthenticationHandler", "users", 400, userId);
137149
return res.status(400).json({ message: "Credential is required" });
138150
}
139151
try {
140-
credential.id = credential.id.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); // Assure que l'ID est en base64url
152+
credential.id = credential.id
153+
.replace(/\+/g, "-")
154+
.replace(/\//g, "_")
155+
.replace(/=+$/, ""); // Assure que l'ID est en base64url
141156
// Si pas d'userId, retrouve l'utilisateur par credential.id
142157
let user;
143158
if (userId) {
@@ -147,16 +162,18 @@ let WebAuthn = class WebAuthn {
147162
user = await this.userService.getUserByCredentialId(credential.id);
148163
}
149164
if (!user) {
150-
await this.createLog(req, 'verifyAuthenticationHandler', 'users', 404, userId);
165+
await this.createLog(req, "verifyAuthenticationHandler", "users", 404, userId);
151166
return res.status(404).json({ message: "User not found" });
152167
}
153168
const apiKey = (0, GenKey_1.genKey)(user.user_id);
154169
const jwtToken = (0, Jwt_1.generateUserJwt)(user, apiKey);
155-
await this.createLog(req, 'verifyAuthenticationHandler', 'users', 200, user.user_id);
156-
res.status(200).json({ message: "Authentication successful", token: jwtToken });
170+
await this.createLog(req, "verifyAuthenticationHandler", "users", 200, user.user_id);
171+
res
172+
.status(200)
173+
.json({ message: "Authentication successful", token: jwtToken });
157174
}
158175
catch (error) {
159-
await this.createLog(req, 'verifyAuthenticationHandler', 'users', 500, userId, { error: error.message });
176+
await this.createLog(req, "verifyAuthenticationHandler", "users", 500, userId, { error: error.message });
160177
res.status(500).json({ message: "Error verifying authentication" });
161178
}
162179
}

dist/services/UserService.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1717
var UserService_1;
1818
Object.defineProperty(exports, "__esModule", { value: true });
1919
exports.UserService = void 0;
20-
/* eslint-disable @typescript-eslint/no-explicit-any */
2120
const inversify_1 = require("inversify");
2221
const dotenv_1 = require("dotenv");
2322
const path_1 = __importDefault(require("path"));

0 commit comments

Comments
 (0)