Skip to content

Commit

Permalink
Move s3 to middleware (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timothy-Gonzalez authored Jan 18, 2024
1 parent bb3ef83 commit 8651cfb
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
17 changes: 17 additions & 0 deletions src/middleware/s3.ts
Original file line number Diff line number Diff line change
@@ -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();
}
59 changes: 30 additions & 29 deletions src/services/s3/s3-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;

0 comments on commit 8651cfb

Please sign in to comment.