-
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.
WIP: yars service and hasPermission middleware
- Loading branch information
1 parent
effb7d6
commit 59024f7
Showing
13 changed files
with
281 additions
and
103 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 |
---|---|---|
@@ -1,32 +1,36 @@ | ||
// @ts-expect-error api-problem lacks a defined interface; code still works fine | ||
import Problem from 'api-problem'; | ||
|
||
import { ACCESS_ROLES_LIST } from '../utils/constants/application'; | ||
import { yarsService } from '../services'; | ||
|
||
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 | ||
* @function hasPermission | ||
* Obtains the roles for the current users identity | ||
* Checks if the permission mappings contain the given resource/action pair for any of the users roles | ||
* @param {string} resource a resource name | ||
* @param {string} action an action name | ||
* @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 || ACCESS_ROLES_LIST.some((r) => roles.includes(r))) { | ||
throw new Error('Invalid role authorization'); | ||
export const hasPermission = (resource: string, action: string) => { | ||
return async (req: Request, res: Response, next: NextFunction) => { | ||
try { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const roles = await yarsService.getIdentityRoles((req.currentUser?.tokenPayload as any).preferred_username); | ||
|
||
const results = await Promise.all(roles.map((x) => yarsService.roleHasPermission(x.roleId, resource, action))); | ||
|
||
if (!results.includes(true)) { | ||
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 })); | ||
} | ||
// 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(); | ||
// 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
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
Oops, something went wrong.