-
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.
Add yars service and hasPermission middleware
Middleware added to routes. Submission controller checking api scope
- Loading branch information
1 parent
0e4bd5b
commit b34d2a4
Showing
21 changed files
with
373 additions
and
114 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
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 |
---|---|---|
@@ -1,32 +1,69 @@ | ||
// @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 { userService, yarsService } from '../services'; | ||
|
||
import type { NextFunction, Request, Response } from '../interfaces/IExpress'; | ||
import { Scope } from '../utils/enums/application'; | ||
import { getCurrentIdentity } from '../utils/utils'; | ||
import { NIL } from 'uuid'; | ||
|
||
// Converts a primitive string to a Scope enum type | ||
function convertStringToScope(value: string): Scope | undefined { | ||
return (Object.values(Scope) as Array<string>).includes(value) ? (value as Scope) : undefined; | ||
} | ||
|
||
/** | ||
* @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 | ||
* Obtains the full permission mappings for the given resource/action pair for any of the users roles | ||
* 403 if none are found | ||
* Checks for highest priority scope and injects into the currentUser | ||
* Defaults scope to self if none were found | ||
* @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 { | ||
if (req.currentUser) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const roles = await yarsService.getIdentityRoles((req.currentUser?.tokenPayload as any).preferred_username); | ||
|
||
const permissions = await Promise.all( | ||
roles.map((x) => yarsService.getRolePermissionDetails(x.roleId, resource, action)) | ||
).then((x) => x.flat()); | ||
|
||
if (!permissions || permissions.length === 0) { | ||
throw new Error('Invalid role authorization'); | ||
} | ||
|
||
const userId = await userService.getCurrentUserId(getCurrentIdentity(req.currentUser, NIL), NIL); | ||
|
||
if (!userId) { | ||
throw new Error('Invalid role user'); | ||
} | ||
|
||
const scopes = permissions | ||
.filter((x) => !!x.scopeName) | ||
.map((x) => ({ scopeName: x.scopeName as string, scopePriority: x.scopePriority as number })) | ||
.sort((a, b) => (a.scopePriority > b.scopePriority ? 1 : -1)); | ||
|
||
req.currentUser.apiScope = { | ||
name: scopes.length ? convertStringToScope(scopes[0].scopeName) ?? Scope.SELF : Scope.SELF, | ||
userId: userId | ||
}; | ||
} else { | ||
throw new Error('No current user'); | ||
} | ||
// 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
Oops, something went wrong.