Download src/middlewares/authContainerMiddleware.ts from lynfera/api: direct link, hf CLI and curl.
- Browser
- Download file 1.23 kB
-
https://huggingface.co/spaces/lynfera/api/resolve/main/src/middlewares/authContainerMiddleware.ts
- Command line
-
hf download hf://spaces/lynfera/api/src/middlewares/authContainerMiddleware.ts
-
curl -L -o authContainerMiddleware.ts https://huggingface.co/spaces/lynfera/api/resolve/main/src/middlewares/authContainerMiddleware.ts
1.23 kB
| import { NextFunction, Request, Response } from "express"; | |
| import jwt from "jsonwebtoken"; | |
| import { ENVS } from "@/config/env.config.js"; | |
| import { USER_ERRORS } from "@/constants/errors.js"; | |
| import { STATUS_CODES } from "@/utils/statusCodes.js"; | |
| import AppError from "@/utils/AppError.js"; | |
| export const authenticaContainerToken = (req: Request, res: Response, next: NextFunction) => { | |
| try { | |
| const serviceToken = req.headers["authorization"]?.split(" ")[1]; | |
| const containerToken = req.headers["x-static-token"]; | |
| if (!containerToken || !serviceToken) { | |
| console.log("No token"); | |
| return res.status(STATUS_CODES.UNAUTHORIZED).json({ message: USER_ERRORS.NO_TOKEN }); | |
| } | |
| const decoded = jwt.verify(serviceToken, ENVS.SERVICE_JWT_SECRET, { | |
| algorithms: ["HS256"], | |
| }) as { pId: string; dId: string }; | |
| // console.log(decoded); | |
| const server_token = ENVS.CONTAINER_API_TOKEN; | |
| if (containerToken !== server_token) { | |
| console.log("Invalid container token"); | |
| return res.status(STATUS_CODES.UNAUTHORIZED).json({ message: USER_ERRORS.INVALID_TOKEN, resource: "Container token" }); | |
| } | |
| } catch (error: any) { | |
| console.log("JWT decode error"); | |
| next(new AppError(error.message, 401)); | |
| return; | |
| } | |
| next(); | |
| }; | |