-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dev/sdagg9/rate-limiter
- Loading branch information
Showing
30 changed files
with
932 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,19 @@ | ||
name: Prettier on Push and PR | ||
name: Build | ||
|
||
on: | ||
push: | ||
branches: | ||
- "main" | ||
- "dev**" | ||
pull_request: | ||
branches: | ||
- "main" | ||
|
||
jobs: | ||
prettier: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: "18.x" | ||
cache: 'yarn' | ||
- name: Install dependencies | ||
run: yarn | ||
- name: Run Prettier | ||
run: npx prettier --write . | ||
- name: Build | ||
run: yarn build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: Prettier | ||
|
||
on: | ||
push: | ||
|
||
jobs: | ||
prettier: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/setup-node@v4 | ||
with: | ||
node-version: "18.x" | ||
cache: 'yarn' | ||
- name: Install dependencies | ||
run: yarn | ||
- name: Run Prettier | ||
run: npx prettier --write . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,10 @@ | ||
# rp-api | ||
# API for Reflections | Projections 2024 | ||
|
||
Contributors: | ||
|
||
- Aydan Pirani | ||
- Divya Koya | ||
- Riya Patel | ||
- Jacob Chang | ||
- Alex Yang | ||
- Shreenija Daggavolu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
version: 0.0 | ||
os: linux | ||
files: | ||
- source: / | ||
destination: /home/ubuntu/rp-api | ||
file_exists_behavior: OVERWRITE | ||
hooks: | ||
BeforeInstall: | ||
- location: scripts/install_dependencies.sh | ||
timeout: 300 | ||
runas: root | ||
AfterInstall: | ||
- location: scripts/build.sh | ||
timeout: 300 | ||
runas: root | ||
ApplicationStart: | ||
- location: scripts/install_dependencies.sh | ||
timeout: 300 | ||
runas: root | ||
- location: scripts/reload_server.sh | ||
timeout: 300 | ||
runas: root |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
cd /home/ubuntu/rp-api | ||
yarn build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
cd /home/ubuntu/rp-api | ||
sudo yarn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
cd /home/ubuntu/rp-api | ||
sudo pm2 reload RP_API |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// import cors from "cors"; | ||
|
||
// // Allow CORS for Netlify deploy previews | ||
// const allowedOrigins = ["https://reflectionsprojections.org"]; | ||
// // Function to check if the origin matches the deploy preview format | ||
// function isNetlifyDeployPreview(origin: string) { | ||
// const regex = new RegExp("deploy-preview-[0-9]*(--rp2024.netlify.app)(.*)"); | ||
// return regex.test(origin); | ||
// } | ||
// | ||
// const corsMiddleware = cors({ | ||
// origin: function (origin, callback) { | ||
// if ( | ||
// !origin || | ||
// allowedOrigins.includes(origin) || | ||
// isNetlifyDeployPreview(origin) | ||
// ) { | ||
// callback(null, true); | ||
// } else { | ||
// callback(new Error("Not allowed by CORS")); | ||
// } | ||
// }, | ||
// }); | ||
|
||
// export default cors(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import { JwtPayloadValidator, Role } from "../services/auth/auth-models"; | ||
import { z } from "zod"; | ||
import jsonwebtoken from "jsonwebtoken"; | ||
import { Config } from "../config"; | ||
import { StatusCodes } from "http-status-codes"; | ||
|
||
export default function RoleChecker( | ||
requiredRoles: z.infer<typeof Role>[], | ||
weakVerification: boolean = false | ||
) { | ||
return function (req: Request, res: Response, next: NextFunction) { | ||
const jwt = req.headers.authorization; | ||
|
||
if (jwt == undefined) { | ||
if (weakVerification) { | ||
next(); | ||
} | ||
|
||
return res.status(StatusCodes.BAD_REQUEST).json({ error: "NoJWT" }); | ||
} | ||
|
||
try { | ||
const payloadData = jsonwebtoken.verify( | ||
jwt, | ||
Config.JWT_SIGNING_SECRET | ||
); | ||
|
||
const payload = JwtPayloadValidator.parse(payloadData); | ||
res.locals.payload = payload; | ||
|
||
const error = new Error("InvalidRoles"); | ||
const userRoles = payload.roles; | ||
|
||
if (weakVerification) { | ||
next(); | ||
} | ||
|
||
if (requiredRoles.length == 0) { | ||
next(); | ||
} | ||
|
||
// Admins (staff) can access any endpoint | ||
if (userRoles.includes(Role.Enum.ADMIN)) { | ||
next(); | ||
} | ||
|
||
// Corporate role can access corporate only endpoints | ||
if (requiredRoles.includes(Role.Enum.CORPORATE)) { | ||
if (userRoles.includes(Role.Enum.CORPORATE)) { | ||
next(); | ||
} | ||
} | ||
|
||
// Need to be a user to access user endpoints (app users) | ||
if (requiredRoles.includes(Role.Enum.USER)) { | ||
if (userRoles.includes(Role.Enum.USER)) { | ||
next(); | ||
} | ||
} | ||
|
||
throw error; | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
} |
Oops, something went wrong.