-
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.
Forbid front end navigation and API access if at least one role isn't present
- Loading branch information
1 parent
1bc96a5
commit e7c5c91
Showing
6 changed files
with
72 additions
and
2 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,32 @@ | ||
// @ts-expect-error api-problem lacks a defined interface; code still works fine | ||
import Problem from 'api-problem'; | ||
|
||
import { AccessRoles } from '../components/constants'; | ||
|
||
import type { NextFunction, Request, Response } from '../interfaces/IExpress'; | ||
|
||
/** | ||
* @function hasAccess | ||
* Check if the currentUser has at least one assigned role | ||
* @param {Request} req Express request object | ||
* @param {Response} res Express response object | ||
* @param {NextFunction} next The next callback function | ||
* @returns {function} Express middleware function | ||
* @throws The error encountered upon failure | ||
*/ | ||
export const hasAccess = async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
// TODO: Can we expand tokenPayload to include client_roles? | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const roles = (req.currentUser?.tokenPayload as any)?.client_roles; | ||
if (!roles || !Object.values(AccessRoles).some((r) => roles.includes(r))) { | ||
throw new Error('Invalid role authorization'); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (err: any) { | ||
return next(new Problem(403, { detail: err.message, instance: req.originalUrl })); | ||
} | ||
|
||
// Continue middleware | ||
next(); | ||
}; |
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