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

Commit 4e26f72

Browse files
committed
fix: Implement verifyKey method for key validation in AuthenticatorController
1 parent e7424a7 commit 4e26f72

3 files changed

Lines changed: 75 additions & 73 deletions

File tree

dist/controllers/AuthenticatorController.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export declare class Authenticator {
77
private logService;
88
constructor(userService: IUserService, logService: ILogService);
99
private logAction;
10-
handleAuthenticatorActions(req: AuthenticatedRequest, res: Response): Promise<Response<any, Record<string, any>> | undefined>;
1110
verifyKey(req: Request, res: Response): Promise<Response<any, Record<string, any>> | undefined>;
11+
handleAuthenticatorActions(req: AuthenticatedRequest, res: Response): Promise<Response<any, Record<string, any>> | undefined>;
1212
}

dist/controllers/AuthenticatorController.js

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,40 @@ let Authenticator = class Authenticator {
7373
console.error("Error creating log:", error);
7474
}
7575
}
76+
async verifyKey(req, res) {
77+
const { code, userId } = req.body;
78+
if (!userId) {
79+
await this.logAction(req, "verifyKey", 400);
80+
return res.status(400).send({ message: "User ID is required" });
81+
}
82+
try {
83+
const user = await this.userService.getUser(userId);
84+
if (!user) {
85+
await this.logAction(req, "verifyKey", 404);
86+
return res.status(404).send({ message: "User not found" });
87+
}
88+
const key = user.authenticator_secret;
89+
if (!key || !code) {
90+
await this.logAction(req, "verifyKey", 400);
91+
return res.status(400).send({ message: "Key and code are required" });
92+
}
93+
const isValid = time2fa_1.Totp.validate({ secret: key, passcode: code });
94+
if (isValid) {
95+
await this.logAction(req, "verifyKey", 200);
96+
const apiKey = (0, GenKey_1.genKey)(user.user_id);
97+
const jwtToken = (0, Jwt_1.generateUserJwt)(user, apiKey);
98+
return res.status(200).send({ message: "Key verified successfully", token: jwtToken });
99+
}
100+
else {
101+
await this.logAction(req, "verifyKey", 400);
102+
return res.status(400).send({ message: "Invalid key or code" });
103+
}
104+
}
105+
catch (error) {
106+
await this.logAction(req, "verifyKey", 500, { error });
107+
handleError(res, error, "Error verifying key");
108+
}
109+
}
76110
async handleAuthenticatorActions(req, res) {
77111
const action = req.params.action;
78112
const user = req.user;
@@ -132,54 +166,20 @@ let Authenticator = class Authenticator {
132166
handleError(res, error, `Error in ${action}`);
133167
}
134168
}
135-
async verifyKey(req, res) {
136-
const { code, userId } = req.body;
137-
if (!userId) {
138-
await this.logAction(req, "verifyKey", 400);
139-
return res.status(400).send({ message: "User ID is required" });
140-
}
141-
try {
142-
const user = await this.userService.getUser(userId);
143-
if (!user) {
144-
await this.logAction(req, "verifyKey", 404);
145-
return res.status(404).send({ message: "User not found" });
146-
}
147-
const key = user.authenticator_secret;
148-
if (!key || !code) {
149-
await this.logAction(req, "verifyKey", 400);
150-
return res.status(400).send({ message: "Key and code are required" });
151-
}
152-
const isValid = time2fa_1.Totp.validate({ secret: key, passcode: code });
153-
if (isValid) {
154-
await this.logAction(req, "verifyKey", 200);
155-
const apiKey = (0, GenKey_1.genKey)(user.user_id);
156-
const jwtToken = (0, Jwt_1.generateUserJwt)(user, apiKey);
157-
return res.status(200).send({ message: "Key verified successfully", token: jwtToken });
158-
}
159-
else {
160-
await this.logAction(req, "verifyKey", 400);
161-
return res.status(400).send({ message: "Invalid key or code" });
162-
}
163-
}
164-
catch (error) {
165-
await this.logAction(req, "verifyKey", 500, { error });
166-
handleError(res, error, "Error verifying key");
167-
}
168-
}
169169
};
170170
exports.Authenticator = Authenticator;
171171
__decorate([
172-
(0, inversify_express_utils_1.httpPost)("/:action", LoggedCheck_1.LoggedCheck.middleware),
172+
(0, inversify_express_utils_1.httpPost)("/verifyKey"),
173173
__metadata("design:type", Function),
174174
__metadata("design:paramtypes", [Object, Object]),
175175
__metadata("design:returntype", Promise)
176-
], Authenticator.prototype, "handleAuthenticatorActions", null);
176+
], Authenticator.prototype, "verifyKey", null);
177177
__decorate([
178-
(0, inversify_express_utils_1.httpPost)("/verifyKey"),
178+
(0, inversify_express_utils_1.httpPost)("/:action", LoggedCheck_1.LoggedCheck.middleware),
179179
__metadata("design:type", Function),
180180
__metadata("design:paramtypes", [Object, Object]),
181181
__metadata("design:returntype", Promise)
182-
], Authenticator.prototype, "verifyKey", null);
182+
], Authenticator.prototype, "handleAuthenticatorActions", null);
183183
exports.Authenticator = Authenticator = __decorate([
184184
(0, inversify_express_utils_1.controller)("/authenticator"),
185185
__param(0, (0, inversify_1.inject)("UserService")),

src/controllers/AuthenticatorController.ts

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,42 @@ export class Authenticator {
4141
}
4242
}
4343

44+
45+
46+
@httpPost("/verifyKey")
47+
async verifyKey(req: Request, res: Response) {
48+
const { code, userId } = req.body;
49+
if (!userId) {
50+
await this.logAction(req, "verifyKey", 400);
51+
return res.status(400).send({ message: "User ID is required" });
52+
}
53+
try {
54+
const user = await this.userService.getUser(userId);
55+
if (!user) {
56+
await this.logAction(req, "verifyKey", 404);
57+
return res.status(404).send({ message: "User not found" });
58+
}
59+
const key = user.authenticator_secret;
60+
if (!key || !code) {
61+
await this.logAction(req, "verifyKey", 400);
62+
return res.status(400).send({ message: "Key and code are required" });
63+
}
64+
const isValid = Totp.validate({ secret: key, passcode: code });
65+
if (isValid) {
66+
await this.logAction(req, "verifyKey", 200);
67+
const apiKey = genKey(user.user_id);
68+
const jwtToken = generateUserJwt(user, apiKey);
69+
return res.status(200).send({ message: "Key verified successfully", token: jwtToken });
70+
} else {
71+
await this.logAction(req, "verifyKey", 400);
72+
return res.status(400).send({ message: "Invalid key or code" });
73+
}
74+
} catch (error) {
75+
await this.logAction(req, "verifyKey", 500, { error });
76+
handleError(res, error, "Error verifying key");
77+
}
78+
}
79+
4480
@httpPost("/:action", LoggedCheck.middleware)
4581
async handleAuthenticatorActions(req: AuthenticatedRequest, res: Response) {
4682
const action = req.params.action;
@@ -103,38 +139,4 @@ export class Authenticator {
103139
handleError(res, error, `Error in ${action}`);
104140
}
105141
}
106-
107-
@httpPost("/verifyKey")
108-
async verifyKey(req: Request, res: Response) {
109-
const { code, userId } = req.body;
110-
if (!userId) {
111-
await this.logAction(req, "verifyKey", 400);
112-
return res.status(400).send({ message: "User ID is required" });
113-
}
114-
try {
115-
const user = await this.userService.getUser(userId);
116-
if (!user) {
117-
await this.logAction(req, "verifyKey", 404);
118-
return res.status(404).send({ message: "User not found" });
119-
}
120-
const key = user.authenticator_secret;
121-
if (!key || !code) {
122-
await this.logAction(req, "verifyKey", 400);
123-
return res.status(400).send({ message: "Key and code are required" });
124-
}
125-
const isValid = Totp.validate({ secret: key, passcode: code });
126-
if (isValid) {
127-
await this.logAction(req, "verifyKey", 200);
128-
const apiKey = genKey(user.user_id);
129-
const jwtToken = generateUserJwt(user, apiKey);
130-
return res.status(200).send({ message: "Key verified successfully", token: jwtToken });
131-
} else {
132-
await this.logAction(req, "verifyKey", 400);
133-
return res.status(400).send({ message: "Invalid key or code" });
134-
}
135-
} catch (error) {
136-
await this.logAction(req, "verifyKey", 500, { error });
137-
handleError(res, error, "Error verifying key");
138-
}
139-
}
140142
}

0 commit comments

Comments
 (0)