JavaScriptEasy
Difference: AuthN vs AuthZ?
Authentication (AuthN) verifies who a user is; authorization (AuthZ) decides what that authenticated user is allowed to do; authentication always comes first.
Authentication: "Who are you?"
- Passwords (hashed with bcrypt, scrypt or Argon2 on the server).
- Passkeys / WebAuthn: phishing-resistant, now widely supported and recommended.
- OAuth/OpenID Connect ("Sign in with Google/GitHub").
- Magic links, one-time codes, multi-factor authentication (MFA).
- Result: a session or token proving identity.
- Failure status: 401 Unauthorized (the name is historical; it really means "unauthenticated").
Authorization: "What can you do?"
- RBAC (role-based): admin, editor, viewer.
- ABAC (attribute-based): rules using user, resource and context attributes ("owners can edit their own posts during business hours").
- ReBAC (relationship-based): permissions derived from relationships, like Google Docs sharing.
- Scopes in OAuth access tokens (
read:orders). - Failure status: 403 Forbidden (we know who you are, but you may not do this). Sometimes 404 is returned to avoid revealing that a resource exists.
Golden rules
- Enforce authorization on the server for every request and every resource. Hiding a button in the UI is UX, not security.
- Check object-level access, not only the route: "can user 42 edit post 99?" Missing this is broken object level authorization, the top API security risk.
- Deny by default; centralise policies in one place.
Code example
js
1// Authentication: establish identity (simplified)2const sessions = new Map([["sess-abc", { userId: 1 }]]);3const users = new Map([4 [1, { id: 1, name: "Ada", role: "editor" }],5 [2, { id: 2, name: "Linus", role: "viewer" }],6]);7const posts = new Map([[99, { id: 99, authorId: 1, title: "Hello" }]]);89function authenticate(sessionId) {10 const session = sessions.get(sessionId);11 if (!session) return { status: 401, error: "Not authenticated" };12 return { user: users.get(session.userId) };13}1415// Authorization: central, deny-by-default policy with object-level checks16const policies = {17 "post:read": () => true,18 "post:edit": (user, post) => user.role === "admin" || (user.role === "editor" && post.authorId === user.id),19 "post:delete": (user) => user.role === "admin",20};21const can = (user, action, resource) => policies[action]?.(user, resource) ?? false;2223function handleEditPost(sessionId, postId) {24 const auth = authenticate(sessionId); // 1. who are you?25 if (auth.error) return auth;26 const post = posts.get(postId);27 if (!post) return { status: 404 };28 if (!can(auth.user, "post:edit", post)) return { status: 403, error: "Forbidden" }; // 2. may you?29 return { status: 200, message: `${auth.user.name} may edit "${post.title}"` };30}3132console.log(handleEditPost("sess-abc", 99)); // { status: 200, ... }33console.log(handleEditPost("bad-session", 99)); // { status: 401, error: "Not authenticated" }34sessions.set("sess-xyz", { userId: 2 });35console.log(handleEditPost("sess-xyz", 99)); // { status: 403, error: "Forbidden" }
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the JavaScript set.