From 8651cfb25c6b56a1a792ebed116e90cccc64b1e3 Mon Sep 17 00:00:00 2001 From: Timothy Gonzalez <105177619+Timothy-Gonzalez@users.noreply.github.com> Date: Thu, 18 Jan 2024 01:06:39 -0600 Subject: [PATCH] Move s3 to middleware (#153) --- src/middleware/s3.ts | 17 +++++++++++ src/services/s3/s3-router.ts | 59 ++++++++++++++++++------------------ 2 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 src/middleware/s3.ts diff --git a/src/middleware/s3.ts b/src/middleware/s3.ts new file mode 100644 index 00000000..34c6dee4 --- /dev/null +++ b/src/middleware/s3.ts @@ -0,0 +1,17 @@ +import { NextFunction, Request, Response } from "express"; + +import S3 from "aws-sdk/clients/s3.js"; +import Config from "../config.js"; + +export function s3ClientMiddleware(_: Request, res: Response, next: NextFunction): void { + console.log("im middleware s3"); + res.locals.s3 = new S3({ + apiVersion: "2006-03-01", + accessKeyId: Config.S3_ACCESS_KEY, + secretAccessKey: Config.S3_SECRET_KEY, + region: Config.S3_REGION, + signatureVersion: "v4", + }); + + next(); +} diff --git a/src/services/s3/s3-router.ts b/src/services/s3/s3-router.ts index 70b19e2f..e12e1d93 100644 --- a/src/services/s3/s3-router.ts +++ b/src/services/s3/s3-router.ts @@ -5,19 +5,12 @@ import { JwtPayload } from "../auth/auth-models.js"; import { StatusCode } from "status-code-enum"; import { hasElevatedPerms } from "../auth/auth-lib.js"; -import S3 from "aws-sdk/clients/s3.js"; import Config from "../../config.js"; +import S3 from "aws-sdk/clients/s3.js"; +import { s3ClientMiddleware } from "../../middleware/s3.js"; const s3Router: Router = Router(); -const s3 = new S3({ - apiVersion: "2006-03-01", - accessKeyId: Config.S3_ACCESS_KEY, - secretAccessKey: Config.S3_SECRET_KEY, - region: Config.S3_REGION, - signatureVersion: "v4", -}); - /** * @api {get} /s3/upload GET /s3/upload * @apiGroup s3 @@ -31,8 +24,9 @@ const s3 = new S3({ "url": "https://resume-bucket-dev.s3.us-east-2.amazonaws.com/randomuser?randomstuffs", } */ -s3Router.get("/upload", strongJwtVerification, async (_1: Request, res: Response, _2: NextFunction) => { +s3Router.get("/upload", strongJwtVerification, s3ClientMiddleware, async (_1: Request, res: Response, _2: NextFunction) => { const payload: JwtPayload = res.locals.payload as JwtPayload; + const s3 = res.locals.s3 as S3; const userId: string = payload.id; const s3Params = { @@ -60,8 +54,9 @@ s3Router.get("/upload", strongJwtVerification, async (_1: Request, res: Response "url": "https://resume-bucket-dev.s3.us-east-2.amazonaws.com/randomuser?randomstuffs", } */ -s3Router.get("/download", strongJwtVerification, async (_1: Request, res: Response, _2: NextFunction) => { +s3Router.get("/download", strongJwtVerification, s3ClientMiddleware, async (_1: Request, res: Response, _2: NextFunction) => { const payload: JwtPayload = res.locals.payload as JwtPayload; + const s3 = res.locals.s3 as S3; const userId: string = payload.id; const s3Params = { @@ -92,23 +87,29 @@ s3Router.get("/download", strongJwtVerification, async (_1: Request, res: Respon * HTTP/1.1 403 Forbidden * {"error": "Forbidden"} */ -s3Router.get("/download/:USERID", strongJwtVerification, async (req: Request, res: Response, _2: NextFunction) => { - const userId: string | undefined = req.params.USERID; - const payload: JwtPayload = res.locals.payload as JwtPayload; - - if (!hasElevatedPerms(payload)) { - return res.status(StatusCode.ClientErrorForbidden).send({ error: "Forbidden" }); - } - - const s3Params = { - Bucket: Config.S3_BUCKET_NAME, - Key: `${userId}.pdf`, - Expires: 60, - }; - - const downloadUrl = await s3.getSignedUrl("getObject", s3Params); - - return res.status(StatusCode.SuccessOK).send({ url: downloadUrl }); -}); +s3Router.get( + "/download/:USERID", + strongJwtVerification, + s3ClientMiddleware, + async (req: Request, res: Response, _2: NextFunction) => { + const userId: string | undefined = req.params.USERID; + const payload: JwtPayload = res.locals.payload as JwtPayload; + const s3 = res.locals.s3 as S3; + + if (!hasElevatedPerms(payload)) { + return res.status(StatusCode.ClientErrorForbidden).send({ error: "Forbidden" }); + } + + const s3Params = { + Bucket: Config.S3_BUCKET_NAME, + Key: `${userId}.pdf`, + Expires: 60, + }; + + const downloadUrl = await s3.getSignedUrl("getObject", s3Params); + + return res.status(StatusCode.SuccessOK).send({ url: downloadUrl }); + }, +); export default s3Router;