In JavaScript, The Bitwise Operators perform operations on the integer values at the binary level (32-bit binary digitals zeros and ones).
*** Note: The number stored is a 64-bit floating point number. So, to perform a bit-operation JavaScript converts the number into a 32-bit binary number (signed) performs the operation, and converts back the result to a 64-bit number.
Below is the list of Bitwise Operators in JavaScript:
RBAC stands for Role-Base Access Control.
It is a security model that restricts access to resources and actions based on a user's role. It separates the management of user permissions from individual users, making it easier to maintain and scale your application. By assigning roles to users, you can control who can access specific resources and perform certain actions in your application.
The RBAC involves components :
Firstly, the RBAC components should be defined:
Secondly, we will create a function to check the permission of the roles.
export const hasPermission = ( rbac: Rbac[], resource: Resource, permission: Permission, ) => { return !!(permission & (rbac.find((rbacResource) => rbacResource === resource)?.value ?? 0)); };
Continually, create a middleware in Node.js
const verifyPermission = (resource: Resource, permission: Permission) => { return async (_: Request, res: Response, next: NextFunction) => { try { try { const allow = !!hasPermission(rbac, resource, permission); if (!allow) { return new Forbidden(message).send(res); } return next(); } catch (error) { return new Forbidden(message).send(res); } } catch (error: any) { return next(new Forbidden(error.message)); } }; };
Finally, adding middleware to a router
router.post('/comments', [verifyPermission(1, 1)], (req: Request, res: Response, next: NextFunction) => {});
In this article, I showed you how to build an RBAC in Node.js using Bitwise Operators. This ensures users can or cannot allow access to resources, enhancing the security of your application.
I hope this article helps secure your application.
Thank you for your following.
❤️ Code for fun!!! ❤️