Authentication and authorisation
What breaks
Section titled “What breaks”The salon platform from lesson 1.6 has one login: the owner’s. She signs in on her phone once, and it stays signed in. It works.
Then she hires Ngozi for the front desk. Ngozi needs to book and move appointments. The owner gives her the password. Now Ngozi can also see the takings and change prices. Every change in the history says “owner”. When Ngozi leaves, the only way to lock her out is to change the password, which logs the owner out too.
Then a second salon signs up. One of its stylists opens appointment 1043, changes the address to 1042, and sees the first salon’s customer: name, phone number, the lot.
Then an accounting app offers to do the salon’s books, and asks for the owner’s password.
Three failures, one cause. The server knows a request carried the right password. It cannot tell who sent it, which business they act in, or what they may do there.
Terms used in this lesson:
- Authentication is checking who is asking. RFC 4949 defines it as “the process of verifying a claim” that an entity has some attribute, such as being user 7.
- Authorisation is checking what they may do: “an approval that is granted to a system entity to access a system resource” (RFC 4949). One approval is a permission.
- A tenant, from lesson 1.1, is one customer business on a shared platform.
Mental model
Section titled “Mental model”Every request answers three questions, in order: who are you, which tenant are you acting in, and may you do this there? Authentication answers the first once, at login, and hands the client a proof to show on every later request. Authorisation answers the other two on every request, on the server.
The status codes follow the questions (RFC 9110). A request that “lacks valid authentication credentials” gets a 401. A known user asking for what they may not do gets a 403, or a 404 when the server is “not willing to disclose that one exists”.
Diagram
Section titled “Diagram”Ngozi’s phone asks for appointment 1042, which belongs to the other salon.
Authentication: prove it once, then carry a proof
Section titled “Authentication: prove it once, then carry a proof”Why can the server not just remember Ngozi? Because HTTP remembers nothing. RFC 9110: “HTTP is defined as a stateless protocol, meaning that each request message’s semantics can be understood in isolation”. So login ends by giving the client a proof to send back each time, instead of the password. There are two ways to make it.
Sessions: the server remembers
Section titled “Sessions: the server remembers”The server creates a session: a record of who logged in and until when, kept on the server. The client gets only the session’s id, usually in a cookie, and the server looks it up on each request.
The id is a random key, nothing more. OWASP: it “must be meaningless”, with the user and expiry “stored on the server side”, and it needs “at least 64 bits of entropy” (randomness). Issue a new one at login (“after any privilege level change”), or an attacker who planted an id beforehand inherits the session.
Two cookie attributes from RFC 6265 matter. Secure sends the cookie “only if the request is transmitted over a secure channel”, which means TLS from lesson 1.1. HttpOnly tells the browser to “omit the cookie” from APIs that expose cookies to scripts, so an injected script cannot read it.
What does this buy? Control. When Ngozi leaves, the owner deletes her sessions and her next request gets a 401.
What does it cost? A lookup on every request, which is why lesson 1.6 put sessions in a key-value store. And the browser sends cookies by itself, so another site can trigger a request that carries them: cross-site request forgery (CSRF), which “tricks an authenticated user’s web browser into performing an unwanted action on a trusted site” (OWASP). Lesson 2.15 covers the defences.
Tokens: the client carries the proof
Section titled “Tokens: the client carries the proof”The other way puts the facts inside the proof. A token holds claims, such as “user 7, expires at
18:00”, and a signature over them. The common format is the JSON Web Token (JWT), “a compact
claims representation format” whose claims can be “digitally signed or integrity protected with a
Message Authentication Code (MAC)” (RFC 7519). The client sends it in a header:
Authorization: Bearer eyJhbGci....
Any server with the key can check it without a database: no lookup, no shared session store.
The cost is in the word “bearer”. RFC 6750: “any party in possession of the token … can use the
token in any way that any other party in possession of it can.” And a token stays valid until its
exp claim, the time “on or after which the JWT MUST NOT be accepted”. Ngozi leaves at 15:00 with a
token that expires at 18:00, and for three hours every server accepts it.
Can you revoke it? Only with a list of revoked tokens checked on each request, and then, says OWASP, “user sessions won’t be completely stateless anymore”.
RFC 6749 allows a middle path: two tokens. A short-lived access token, say 10 minutes, is checked by signature alone. A long-lived refresh token, one of the “credentials used to obtain access tokens” (RFC 6749), is checked against the server’s records each time it is used, and refused once the user is cut off. The worst-case delay is now one access token’s lifetime.
Two traps. RFC 8725: “the algorithm can be changed to “none” by an attacker, and some libraries would trust this value”, so fix the algorithms you accept. And local storage is “always accessible by JavaScript” (OWASP); keep a browser’s token in an HttpOnly cookie.
OAuth: letting another app act for you
Section titled “OAuth: letting another app act for you”Back to the accounting app. Why not just give it the password? RFC 6749, the OAuth 2.0 standard, opens with this case. When “the resource owner shares its credentials with the third party”, the app must store the password, gets “overly broad access”, and cannot be cut off “without revoking access to all third parties”.
OAuth replaces the password with an access token: “a string denoting a specific scope, lifetime, and other access attributes”. It names four roles. The resource owner is the salon owner. The client is the accounting app. The authorization server checks the owner’s identity and issues tokens. The resource server is the salon platform’s API.
The flow to know is the authorisation code flow with PKCE (Proof Key for Code Exchange):
- The app makes a random secret, the code verifier, and sends the owner’s browser to the authorization server with its hash, the code challenge, and a scope: “read payments”.
- The owner signs in there, not in the app, and approves.
- The authorization server redirects back with a short-lived authorisation code.
- The app sends the code and the verifier; the server hashes the verifier, compares, and returns an access token, often with a refresh token.
- The app calls the payments API with the token. The owner can revoke this one app later.
Why the verifier? Someone who steals the code in step 3 “is unable to redeem it”, lacking the verifier (RFC 7636). RFC 9700, OAuth’s security best practice from January 2025: “public clients MUST use PKCE”, clients “SHOULD NOT use the implicit grant”, and the grant that gives the app the user’s password “MUST NOT be used”.
One distinction matters. OAuth is authorisation: the token says what the app may do, not who the user is. To log a user in through another provider, the app needs OpenID Connect, “a simple identity layer on top of the OAuth 2.0 protocol” that adds an ID token, a JWT with “Claims about the Authentication of an End-User”.
Authorisation: roles, not people
Section titled “Authorisation: roles, not people”Now Ngozi has her own login. What may she do? Permissions per person work for two staff. At fifty salons every hire is a dozen checkboxes, and nobody can answer “who can refund?”.
Role-based access control (RBAC) puts a layer in between. Sandhu and colleagues, 1996: “permissions are associated with roles, and users are made members of appropriate roles. This greatly simplifies management of permissions.” NIST: RBAC, “formalized in 1992 by David Ferraiolo and Rick Kuhn”, is “the predominant model for advanced access control”.
For the salon, a permission is an action on a kind of thing: “move appointments”, “view takings”. A role is a named set of them. “Bookings desk” holds “view appointments”, “book appointments” and “move appointments”, and Ngozi gets it. The check is one question: does any role of this user, in this tenant, grant this permission?
In lesson 1.4’s terms, both links are many-to-many, so each is a join table. The tenant goes on the role assignment, not the user, so Ngozi can work one salon’s desk and own a studio, with one login.
Three rules from OWASP hold for any model. Least privilege: “only the minimum privileges necessary to complete their job”. Deny by default: when no rule matches, refuse. And check on every request, because if “a single access control check is “missed”, the confidentiality and/or integrity of a resource can be jeopardized”. Hiding a button in the app is not a check.
Multi-tenancy: whose data is this?
Section titled “Multi-tenancy: whose data is this?”Back to appointment 1042. The server checked who the stylist was and what her role allowed, and never asked whose appointment it was.
OWASP’s 2025 Top 10 keeps broken access control at #1: “100% of the applications tested were found to have some form of broken access control”. Its first-named form is “viewing or editing someone else’s account by providing its unique identifier”, an insecure direct object reference (IDOR).
Random ids do not fix it: they “are not authorization controls” (OWASP). The fix: “Treat client-supplied tenant identifiers as selectors only. Verify that the authenticated principal is authorized to act in the selected tenant.” Then filter every query for tenant data by that tenant.
Where do the tenants’ rows live? OWASP lists three choices; AWS calls the shared end pool and the dedicated end silo:
| Storage | Boundary | Cost |
|---|---|---|
| Shared tables, a tenant column on every row | the query’s filter, or a database policy | one missed filter leaks; needs “negative-path tests” |
| A schema per tenant | database grants | “disciplined grants” and a migration per schema |
| A database per tenant | separate credentials | “higher operational cost” |
Shared tables can get a second guard. PostgreSQL’s row security policies “restrict, on a
per-user basis, which rows can be returned” or changed; with no policy, “a default-deny policy is
used”. The catch: superusers and BYPASSRLS roles “always bypass” it, and table owners do unless
forced, so the application must not connect as the owner. Noisy neighbours are lesson 3.9.
Real case
Section titled “Real case”Roost, a booking platform for businesses that sell their time, lets each owner decide what her staff can do.
The problem. Owners delegate in business language: this person handles bookings, the money stays with me. Technical permission lists do not match that, and no fixed set of roles fits every business.
The design. RBAC, with the owner defining the roles. The platform defines a fixed catalogue of permissions an owner may grant, each with a plain-language name. The owner builds roles from it, or starts from a preset role and edits it; she cannot invent a permission. Roles belong to one business. A staff member is given a role in that business, and every check asks what the person may do in the business the request is for.
Two choices handle change. A preset is copied into the business when used, so the platform can improve its presets without rewriting a role an owner has shaped. And a permission the platform adds later starts switched off for existing roles. The owner can use the new feature at once and is told it exists; her staff get it only when she grants it. Nobody gains access by surprise.
The trade-off. Owners opt in to each new permission, so a feature can reach staff later than owners. And copied roles drift from their presets, which is the point, but a preset’s name no longer means one set of permissions across the platform.
Honest about scale. Roost is early-stage, and each business has a small team. A chain with many branches would want a role defined once and reused across locations, which adds a level above the business.
Trade-offs
Section titled “Trade-offs”Session or token. A session ends at once and costs a lookup per request; a token costs no lookup and lives until it expires. This course’s suggestion: for a web app with its own backend, a server-side session in an HttpOnly cookie. Short access tokens with refresh tokens earn their extra parts when many services or third parties must check identity without a shared store. Name the revocation delay either way.
Roles or something finer. The industry does not agree. NIST calls RBAC “the predominant model”. OWASP says attribute-based (ABAC) and relationship-based (ReBAC) control “should typically be preferred for application development”: RBAC “does a poor job of supporting object-level” checks, suffers “role explosion” as roles multiply, and is “poorly suited” to many organisations sharing one system. ReBAC grants access by a link, such as “allowing only the user who created a post to edit it”. This course’s suggestion: roles per tenant for what a job may do, plus an ownership check for the few rules about whose record it is.
Pool or silo. Shared tables are cheap and one migration covers every tenant, but one missed filter is a leak. A database per tenant is a hard boundary, at the price of running thousands of databases. AWS: data’s “regulatory profile” and “noisy neighbor attributes might steer” part of a system to silo, and a bridge mixes both.
Drill 1 — interview question
Section titled “Drill 1 — interview question”“Design login for a salon platform with a web dashboard and a phone app for staff. Sessions or JWTs? A stylist is dismissed at 15:00. When does she lose access?”
Model answer
Two problems. Authentication proves who she is once; the proof rides on every request. Authorisation is checked on every request regardless.
Sessions. A random id with at least 64 bits of entropy, issued fresh at login, in a Secure, HttpOnly cookie; data in a key-value store. Dismissal: delete her sessions; next request, 401. Cost: a lookup per request, and CSRF protection.
Tokens. A signed JWT with a short exp, say 10 minutes, plus a refresh token checked against the
server’s records. Dismissal: revoke the refresh token; she keeps access for at most 10 minutes. Cost:
that delay, key management, and fixing the accepted algorithms.
Choice. Sessions for one backend; tokens when many services must verify without a shared store. Either way, removing her role takes effect on the next request.
Rubric
- Separates authentication from authorisation — 2
- Describes a session correctly: random id, server-side data, cookie attributes — 2
- Describes a token correctly: signed claims, expiry, refresh token — 2
- States the revocation delay of each, with a number — 2
- Makes a choice and names its cost — 2
Total 10. 8 or more is a pass.
Drill 2 — interview question
Section titled “Drill 2 — interview question”The interviewer follows up: “Now there are 5,000 salons, and some staff work at two of them. A stylist edits the appointment id in a request and sees another salon’s customer. Her session was valid and her role allows viewing appointments. What failed, and how do you make sure it cannot happen again?”
Model answer
What failed. An insecure direct object reference: no check of which tenant the record belonged to. Random ids make guessing harder; they are not a check.
Fix the model. Roles are assigned per tenant: (user, role, tenant). The request names the tenant as a selector; the server verifies she is a member there and loads her role for that tenant only.
Fix every query. Filter all tenant data by the verified tenant, in one shared data-access layer so no handler can forget. Return 404 for another tenant’s record.
Defence in depth. With shared tables, add PostgreSQL row security policies on the current
tenant, and connect as a role that is not the table owner and lacks BYPASSRLS. Add negative tests:
a salon A user asks for each kind of salon B record and must fail. For a hard boundary, a schema or
database per tenant, at higher operating cost.
Rubric
- Names IDOR or a missing tenant check, and says random ids are not the fix — 2
- Assigns roles per tenant and derives the tenant from verified membership — 3
- Scopes every query by tenant, in one place — 2
- Adds a database-level guard and names its bypass catch — 2
- Mentions negative tests or a silo option with its cost — 1
Total 10. 8 or more is a pass.
Drill 3 — hands-on exercise
Section titled “Drill 3 — hands-on exercise”Build the three checks into a small API in any language, with PostgreSQL.
- Create users, tenants, roles, permissions and the two join tables, with the tenant on the role assignment. Seed two salons, an owner each, and Ngozi as “Bookings desk” at salon A.
- Add login with a server-side session in a Secure, HttpOnly cookie, and
GET /appointments/:id. Predict what Ngozi gets for a salon B appointment, test it, and make it a 404. - Add
POST /refunds. Predict Ngozi’s status code, then make it pass for the owner only. - Delete Ngozi’s session and resend her cookie. Then switch to a 2-minute JWT, delete her user, and time how long the token still works.
- Turn on row-level security for appointments with no policy, query as a non-owner role, then add a tenant policy.
Write one sentence per step on what surprised you.
Exit test
Section titled “Exit test”Answer without notes.
- What is the difference between authentication and authorisation? Which status code does a request get when each fails?
- Compare a server-side session and a signed JWT: where the facts live, what each request costs, and how fast each can cut off a user.
- What problem did OAuth 2.0 solve? Walk through the authorisation code flow, and say what PKCE stops and what OpenID Connect adds.
- Design RBAC for a platform where one person can have different roles at different businesses. Name one rule that roles alone handle badly.
- What is an insecure direct object reference? Name two layers that stop one tenant reading another’s rows, and the catch with PostgreSQL row security.
Further reading
Section titled “Further reading”- RFC 6749, The OAuth 2.0 Authorization Framework — https://www.rfc-editor.org/rfc/rfc6749.
- RFC 7636, PKCE — https://www.rfc-editor.org/rfc/rfc7636; RFC 9700, OAuth 2.0 Security Best Current Practice (January 2025) — https://www.rfc-editor.org/rfc/rfc9700.
- OpenID Connect Core 1.0 — https://openid.net/specs/openid-connect-core-1_0.html.
- RFC 7519, JWT — https://www.rfc-editor.org/rfc/rfc7519; RFC 8725, JWT Best Current Practices — https://www.rfc-editor.org/rfc/rfc8725; RFC 6750, bearer tokens — https://www.rfc-editor.org/rfc/rfc6750; RFC 6265, cookies — https://www.rfc-editor.org/rfc/rfc6265.
- Sandhu and others, Role-Based Access Control Models (1996) — https://csrc.nist.gov/CSRC/media/Projects/Role-Based-Access-Control/documents/sandhu96.pdf; NIST RBAC project — https://csrc.nist.gov/projects/role-based-access-control.
- OWASP cheat sheets (checked 2026-11-29): Session Management — https://cheatsheetseries.owasp.org/cheatsheets/Session_Management_Cheat_Sheet.html; Authorization — https://cheatsheetseries.owasp.org/cheatsheets/Authorization_Cheat_Sheet.html; JSON Web Token — https://cheatsheetseries.owasp.org/cheatsheets/JSON_Web_Token_Cheat_Sheet.html; Multi-Tenant Security — https://cheatsheetseries.owasp.org/cheatsheets/Multi_Tenant_Security_Cheat_Sheet.html.
- OWASP Top 10:2025, A01 Broken Access Control — https://owasp.org/Top10/2025/A01_2025-Broken_Access_Control/.
- PostgreSQL 19 manual, Row Security Policies — https://www.postgresql.org/docs/current/ddl-rowsecurity.html.
- AWS SaaS Lens, Silo, Pool, and Bridge Models — https://docs.aws.amazon.com/wellarchitected/latest/saas-lens/silo-pool-and-bridge-models.html.