Security

Authorization policy

Authentication establishes who is making a request. Authorization decides whether that identity may perform a particular action. A signed-in reader can be allowed to view the catalog while being forbidden to change its prices. Always make the permission decision on the server, using an identity established by trusted authentication middleware. Never accept a role or user ID from a request body as proof of identity. An authenticated user is not automatically an administrator. A hidden button is not a server-side permission check. Evidence: local teaching fixture, not a production security guarantee. Source: https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html

Formal Syntax & Structure

nodejs syntaxnodejs
decide(principal, action) → status (lesson-defined policy)

Working Example

Example Snippetnodejs
const permissions = new Map([  ['reader', new Set(['read'])],  ['editor', new Set(['read', 'update'])]]);function decide(principal, action) {  if (!principal) return 401;  return permissions.get(principal.role)?.has(action) ? 200 : 403;}console.log(decide(null, 'read'));console.log(decide({role:'reader'}, 'update'));console.log(decide({role:'editor'}, 'update'));console.log(decide({role:'unknown'}, 'read'));
Node.js Reference