Authentication versus authorization
Local fixture: Node.js 22.16.0, ECMAScript modules, synthetic data only. Run node main.mjs in a disposable directory. This is not a production security implementation.
Evaluate a role policy
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'));Expected output:
401403200403Anonymous, forbidden, allowed and unknown-role requests produce distinct decisions.
How it works
A principal is an authenticated identity together with trusted attributes. A role groups permissions; a policy maps a principal, action and resource to a decision. Start with denial and grant only known permissions. In HTTP, 401 means valid authentication credentials are missing or unacceptable (include an appropriate WWW-Authenticate challenge); 403 means the server refuses the request. The following examples inject synthetic principals, not credentials. The policy does not authenticate anyone. Role checks alone do not ensure ownership or tenant isolation. Re-evaluate the relevant permission for each request, including internal application calls that can bypass an HTTP route.
Check permission on every action
import assert from 'node:assert/strict';const grants = new Set(['reader:read','editor:read','editor:update']);const allowed = (p, action) => p !== null && grants.has(`${p.role}:${action}`);// Role and action are trusted finite fixture values.assert.equal(allowed({role:'reader'}, 'read'), true);assert.equal(allowed({role:'reader'}, 'update'), false);assert.equal(allowed(null, 'read'), false);assert.equal(allowed({role:'editor'}, 'delete'), false);console.log('4 policy checks passed');Expected output:
4 policy checks passedThe policy grants only listed actions, including a negative test for an authenticated editor.
Common mistakes
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.
Practice and next steps
Add a librarian role allowed to read but not delete. Predict decisions for anonymous users, a known role and an unknown role. Verify the default denial before changing the policy.
Further reading: Primary documentation. Related staged reference: Authorization policy.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Authentication versus authorization — knowledge check
Apply the security boundary and distinguish the example from a production control.
A signed-in reader attempts an editor-only update. Which status fits?
Where should permissions be enforced?
What should an unknown role receive?