2026-01-16 22:41:12 +00:00
|
|
|
import { NextFunction, Request, Response } from "express";
|
|
|
|
|
|
2025-04-20 11:29:53 -07:00
|
|
|
import jwt from "jsonwebtoken";
|
2026-01-20 22:40:06 +00:00
|
|
|
import { AppError } from "@/utils/AppError.js";
|
2026-03-20 12:40:27 -07:00
|
|
|
import type { ISettingsService } from "@/service/system/settingsService.js";
|
2026-03-12 11:25:32 -07:00
|
|
|
import type { User } from "@/types/user.js";
|
2026-01-20 22:40:06 +00:00
|
|
|
|
2025-04-20 11:29:53 -07:00
|
|
|
const SERVICE_NAME = "verifyJWT";
|
|
|
|
|
const TOKEN_PREFIX = "Bearer ";
|
|
|
|
|
|
2026-03-12 11:25:32 -07:00
|
|
|
const isUser = (payload: unknown): payload is User => {
|
|
|
|
|
return typeof payload === "object" && payload !== null && "id" in payload && "teamId" in payload && "role" in payload;
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-20 12:40:27 -07:00
|
|
|
export const createVerifyJWT = (settingsService: ISettingsService) => {
|
2026-01-20 22:40:06 +00:00
|
|
|
return (req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
const token = req.headers["authorization"];
|
|
|
|
|
// Make sure a token is provided
|
|
|
|
|
if (!token) {
|
|
|
|
|
const error = new AppError({ message: "No token provided", status: 401, service: SERVICE_NAME });
|
|
|
|
|
next(error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Make sure it is properly formatted
|
|
|
|
|
if (!token.startsWith(TOKEN_PREFIX)) {
|
|
|
|
|
const error = new AppError({ message: "Invalid token format", status: 401, service: SERVICE_NAME, method: "verifyJWT" });
|
|
|
|
|
next(error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-04-20 11:29:53 -07:00
|
|
|
|
2026-01-20 22:40:06 +00:00
|
|
|
const parsedToken = token.slice(TOKEN_PREFIX.length, token.length);
|
|
|
|
|
// Verify the token's authenticity
|
|
|
|
|
const { jwtSecret } = settingsService.getSettings();
|
|
|
|
|
if (!jwtSecret) {
|
|
|
|
|
const error = new AppError({ message: "JWT secret not configured", status: 500, service: SERVICE_NAME });
|
|
|
|
|
next(error);
|
2025-07-21 09:55:49 -07:00
|
|
|
return;
|
2025-04-20 11:29:53 -07:00
|
|
|
}
|
2026-03-12 11:25:32 -07:00
|
|
|
jwt.verify(parsedToken, jwtSecret, (err: jwt.VerifyErrors | null, decoded: jwt.JwtPayload | string | undefined) => {
|
2026-01-20 22:40:06 +00:00
|
|
|
if (err) {
|
|
|
|
|
const error = new AppError({
|
2026-03-12 11:25:32 -07:00
|
|
|
message: err instanceof Error ? err.message : "Failed to authenticate token",
|
2026-01-20 22:40:06 +00:00
|
|
|
status: 401,
|
|
|
|
|
service: SERVICE_NAME,
|
2026-03-12 11:25:32 -07:00
|
|
|
details: err instanceof Error ? { error: err } : undefined,
|
2026-01-20 22:40:06 +00:00
|
|
|
method: "verifyJWT",
|
|
|
|
|
});
|
|
|
|
|
next(error);
|
|
|
|
|
return;
|
2026-03-12 11:25:32 -07:00
|
|
|
} else if (isUser(decoded)) {
|
2026-01-20 22:40:06 +00:00
|
|
|
req.user = decoded;
|
|
|
|
|
next();
|
2026-03-12 11:25:32 -07:00
|
|
|
} else {
|
|
|
|
|
next(new AppError({ message: "Invalid token payload", status: 401, service: SERVICE_NAME, method: "verifyJWT" }));
|
2026-01-20 22:40:06 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
2025-04-20 11:29:53 -07:00
|
|
|
};
|