-
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.
- Loading branch information
ahmedmohmd
committed
Aug 10, 2024
1 parent
00e3006
commit 1ff2217
Showing
8 changed files
with
150 additions
and
34 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
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,6 @@ | ||
enum Role { | ||
ADMIN = "admin", | ||
MODERATOR = "moderator", | ||
} | ||
|
||
export { Role }; |
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,66 @@ | ||
import { NextFunction, Request, Response } from "express"; | ||
import createHttpError from "http-errors"; | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { Role } from "../enums/user-role.enum"; | ||
import { authUserRoles } from "./auth-user-roles.middleware"; | ||
|
||
vi.mock("../../logging", () => ({ | ||
default: { | ||
errors: { | ||
error: vi.fn(), | ||
}, | ||
}, | ||
})); | ||
|
||
describe("authUserRoles()", () => { | ||
let fakeRequest: Request = {} as Request; | ||
const fakeResponse: Response = {} as Response; | ||
const fakeNextFunction: NextFunction = vi.fn(); | ||
|
||
beforeEach(() => { | ||
Object.assign(fakeRequest, { user: { id: 1 } }); | ||
}); | ||
|
||
it("Should Throw if User not found", async () => { | ||
try { | ||
fakeRequest = {} as Request; | ||
|
||
await authUserRoles(Role.ADMIN)( | ||
fakeRequest, | ||
fakeResponse, | ||
fakeNextFunction | ||
); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(createHttpError.Unauthorized); | ||
} | ||
}); | ||
|
||
it("Should Throw if User not authorized", async () => { | ||
try { | ||
await authUserRoles(Role.ADMIN)( | ||
fakeRequest, | ||
fakeResponse, | ||
fakeNextFunction | ||
); | ||
} catch (error) { | ||
expect(error).toBeInstanceOf(createHttpError.Forbidden); | ||
} | ||
}); | ||
|
||
it("Should call next function if all is well", async () => { | ||
Object.assign(fakeRequest, { | ||
user: { | ||
id: 1, | ||
role: "admin", | ||
}, | ||
}); | ||
|
||
await authUserRoles(Role.ADMIN)( | ||
fakeRequest, | ||
fakeResponse, | ||
fakeNextFunction | ||
); | ||
|
||
expect(fakeNextFunction).toHaveBeenCalledOnce(); | ||
}); | ||
}); |
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,26 @@ | ||
import { RequestHandler } from "express"; | ||
import createHttpError from "http-errors"; | ||
import logger from "../../logging"; | ||
import { Role } from "../enums/user-role.enum"; | ||
|
||
const authUserRoles = | ||
(...roles: Role[]): RequestHandler => | ||
async (req, res, next) => { | ||
// eslint-disable-next-line | ||
const { user } = req as any; | ||
|
||
if (!user) { | ||
logger.errors.error(`Unauthorized Access Try.`); | ||
throw new createHttpError.Unauthorized("Unauthorized User."); | ||
} | ||
|
||
const isUserAllowed = roles.includes(user?.role); | ||
|
||
if (!isUserAllowed) { | ||
logger.errors.error(`Unauthorized Access Try.`); | ||
throw new createHttpError.Forbidden("Forbidden Resource."); | ||
} | ||
|
||
next(); | ||
}; | ||
export { authUserRoles }; |
This file was deleted.
Oops, something went wrong.
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 { RequestHandler } from "express"; | ||
import createHttpError from "http-errors"; | ||
import { decodeJwtToken } from "../../auth/utils/jwt.util"; | ||
import logger from "../../logging"; | ||
|
||
const authenticate: RequestHandler = async (req, res, next) => { | ||
const authorizationHeader = req.headers?.authorization; | ||
|
||
if (!authorizationHeader) { | ||
logger.errors.error(`Failed authentication try.`); | ||
|
||
throw new createHttpError.Unauthorized(`Your are not Authorized.`); | ||
} | ||
|
||
const [, jwtToken] = authorizationHeader.split(" "); | ||
const payload = await decodeJwtToken(jwtToken); | ||
|
||
Object.assign(req, { | ||
user: payload, | ||
}); | ||
|
||
next(); | ||
}; | ||
|
||
export { authenticate }; |
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