← Back to System Design, page 5

system-design-0099 · Easy fresher

What is the difference between authentication and authorisation, and how do sessions, JWTs and role-based access control fit together in a web application?

Authentication answers "who are you?" by checking credentials such as a password plus a second factor; authorisation answers "what are you allowed to do?" by checking permissions for each action. After login, the server remembers the user with a session ID in a secure cookie or with a signed token such as a JWT. Each request is then authorised, often with role-based access control (roles grant permissions) plus ownership checks such as "a learner can see only their own submissions".

Full answer

Authentication (authN)

Proving identity:

  • Something you know (password), have (phone with an authenticator app, security key) or are (fingerprint).
  • Passwords are stored as slow, salted hashes (bcrypt, scrypt, Argon2), never in plain text.
  • Multi-factor authentication stops most account takeovers from leaked passwords.
  • Sign-in with Google or another identity provider delegates authentication (see OAuth and OpenID Connect).

Remembering the user

Server-side sessions. After login, the server creates a random session ID, stores session data (user ID, expiry) in a database or Redis, and sends the ID in a cookie marked HttpOnly, Secure and SameSite. Logging out deletes the session, which takes effect immediately.

Tokens (JWT). The server signs a token containing claims (user ID, roles, expiry). Any service with the public key can verify it without a lookup, which suits APIs and microservices. But a JWT cannot be revoked before it expires, so keep access tokens short-lived (for example 15 minutes) and use refresh tokens that can be revoked.

Authorisation (authZ)

Checking each action:

  • RBAC (role-based): users have roles (learner, instructor, admin); roles have permissions (course:edit, grades:view).
  • Ownership and attribute checks (ABAC): "an instructor can edit only courses they own", "a learner can view a submission only if it is theirs".
  • Check on the server for every request; hiding a button in the UI is not security.
  • Deny by default, and centralise checks in middleware or a policy service so they are not forgotten in one endpoint.

Example request

  1. A learner sends GET /v1/submissions/s_731 with a session cookie.
  2. Authentication middleware looks up the session: user u_42, role learner.
  3. Authorisation: learners need submission:view (granted) and the submission's owner must be u_42. It belongs to u_77, so return 403 Forbidden (or 404 to avoid revealing that it exists).

401 Unauthorized means "not authenticated"; 403 Forbidden means "authenticated but not allowed".

Common mistakes in practice

Insecure direct object references (changing an ID in the URL shows someone else's data), long-lived JWTs with no revocation, storing tokens in localStorage where injected scripts can read them, and checking permissions only in the front end.

By level

Fresher: Explain authentication as logging in and authorisation as what you are allowed to do after logging in. A fresher should give an example of each.

Mid: Compare sessions and JWTs, describe RBAC with ownership checks and the 401 versus 403 difference. Mention password hashing.

Senior: Discuss revocation strategies, token storage in browsers, ABAC and policy engines, central enforcement and common access-control vulnerabilities. Expect a defence-in-depth view.

Key points

  • Authentication proves identity; authorisation checks permissions
  • Sessions in secure cookies are revocable; JWTs are verifiable without lookups but hard to revoke
  • Keep access tokens short-lived with revocable refresh tokens
  • RBAC plus ownership checks on every server request
  • 401 means not authenticated; 403 means not allowed

Common mistakes

  • Checking permissions only in the user interface
  • Forgetting ownership checks so users can read others' records by changing an ID
  • Long-lived JWTs with no revocation plan
  • Storing passwords with fast hashes or in plain text

Follow-ups

  • How would you log out a user everywhere when they change their password?
  • When would you choose ABAC over RBAC?
  • How do you store tokens safely in a browser?

Tips

  • Define both terms in one sentence each
  • Walk through one request with both checks
  • Explain 401 versus 403

Evaluation rubric

  • Clear distinction between authN and authZ
  • Correct session and JWT trade-offs
  • RBAC and ownership checks on the server
  • Awareness of common vulnerabilities