Stage 2 · Building blocks · Medium · 15 min
Authentication and authorization
Who is this user, and what may they do? Sessions against signed tokens, signing in through another site, and roles.
What each interview level is expected to know
Learn first: HTTP, TCP, UDP and connections, Reverse proxies and API gateways
In AI systems: Calling a model from your code, Security: prompt injection and data boundaries, Agents that call tools
After this lesson you can:
- Tell who a user is apart from what they are allowed to do.
- Compare sessions with signed tokens, including what happens at logout.
- Explain how signing in through another site works, and what a leaked signing key breaks.
Start here
See it in 60 seconds
A session or a signed token
After one login, a server remembers you with a session it looks up on every request, or a signed token it only checks.
Scene 1 of 7 · Log in once
Asha types her password once on each site, and each server checks that it is right.
- Browser — Asha — healthy
- Server — keeps sessions — healthy — password is right
- Session store — on the server side — healthy
- Browser — Asha — healthy
- Server — signs tokens — healthy — password is right
- Session store reads
- 0
- Signature checks
- 0
- Log in once. Asha types her password once on each site, and each server checks that it is right.
- A session id comes back. The first server saves a session in its store, and sends back only a random id.
- Every request, a lookup. The browser sends that id with every request, and the server reads the store to learn who it is.
- A signed token comes back. The second server saves nothing: it sends a token that names Asha, signed with its secret key.
- Every request, a check. On each request the server checks the signature with its key, and needs no store at all.
- Log out: the session. Asha logs out, the server deletes the session, and a stolen copy of the id is refused.
- Log out: the token. This server keeps no list of ended tokens, so a stolen copy works until its 15 minutes run out.
- Now you try. Asha logs out, and her token was new at that moment. A thief uses stolen copies some minutes later. Which still work? For example: The session id is refused, because its record is gone. The token still works for 10 more minutes. The server cannot take a token back without a list to look up, so tokens are given short lives.
Scene 1 of 7: Log in once. Asha types her password once on each site, and each server checks that it is right.
Key points
- A session lives on the server; the browser holds only a random id, usually in a cookie.
- A signed token carries who you are, and a server with the right key checks it without a lookup.
- Sessions cost a lookup on every request, but logging out works at once.
- With no lookup, a token cannot be taken back, so keep its life short, such as 15 minutes.
In plain words
Asha logs in to a shop with her password. The server must remember her on the next click, without asking again. One way is a (A record the server keeps for a logged-in user. The browser holds only its id, and sends it with every request.): the server saves a record and gives her browser a random id, such as 7f3a. Another way is a (A short note that says who the user is and when it expires, with a signature that proves the server made it.): a short note that says "this is Asha, until 10:15", with a signature only the server can make. Checking who someone is is called (Checking who someone is, for example with a password.). Deciding what they may do, such as view an order but not refund it, is called (Deciding what a known user may do, such as read a note but not delete it.).
On every later click, the server makes two checks in order. First it reads the session id or the token and works out that this is Asha. With a session it looks the id up in its store; with a token it checks the signature, with no lookup. Then it checks what Asha may do, such as view her own order. A request that fails the first check gets 401, and one that fails the second gets 403.
The problem it solves
(The rules an app or browser uses to ask a server for something and get an answer, such as GET /profile/42 and then 200 OK.) forgets. Each request arrives alone, with nothing to say it comes from the person who logged in a minute ago. And a system with ten services cannot ask for the password ten times, or let each service keep its own copy of every password.
Why it matters
Every design has a login. An interviewer who hears "the gateway checks the token" will ask what a token is and who made it. Then they ask what happens when a user logs out.
A worked example
A token made at 10:00 with a 15-minute life is accepted until 10:15. Say it is stolen at 10:05, and the user logs out at 10:06. With no list of ended tokens, the thief can use it for 9 more minutes, until 10:15. A session deleted at 10:06 is refused from 10:06.
Think of it like…
Think of a swimming pool. A session is a locker key with a number: the desk looks the number up in its book. A token is a stamped day ticket: any guard can check the stamp, with no book.
Where this analogy stops working
A lookup against a stamp is the useful part. The comparison fails with copies. A paper ticket is hard to copy, but a stolen token is a perfect copy, and the guard cannot tell. That is why tokens are given short lives.
Try it
See it working
Also try: Rate limiting
Mechanics
How it works
The pictures follow Asha from this page's example. The first compares the two ways a server remembers her login. The second shows the two checks on every request: who she is, then what she may do.
Check the password once
A careful server does not store the password itself. It stores a hash: a scrambled form that cannot be turned back. It mixes a random value, called a salt, into each one, so equal passwords get different hashes. At login it scrambles what was typed the same way and compares the two. The method, such as bcrypt, is slow on purpose, so guessing millions of passwords costs too much.
Remember the login with a session
The server saves a record such as "7f3a is Asha" in a fast store, and sends the id to the browser in a (A small value that a site asks the browser to keep, and to send back with every request to that site.). A cookie is a small value that the browser sends back with every request. Each request brings the cookie, and the server looks the id up. Deleting the record logs her out at once.
Or remember it with a signed token
Pick a case to follow belowWith this case: With a session, every click cost a lookup, but logout ended the login at once: the copied cookie got 401.
Browser, Asha Browser Asha Shop server, checks each request Shop server checks each request Session store, id → user, NOW Session store id → user 7f3a → Asha NOW - Browser — Asha — healthy
- Shop server — checks each request — healthy
- Session store — id → user — healthy — NOW — 7f3a → Asha
- Request
- Refused
Step 1 of 4Step 1 of 4 · Session store
Done: Asha logs in, and the server saves a session
- What happens
- The server saves the record "7f3a is Asha" in the store, and sends the id 7f3a to her browser in a cookie.
- Why
- The id is random and means nothing by itself. Only the store knows whose it is.
- What changes
- The browser sends the cookie with every request from now on.
- Next
- Her next click: the server looks the id up
The server checksa lookupin the session storeStore lookups0A copied login after logoutnot tried yetAsha has not logged outStep 1 of 4: Session store. Asha logs in, and the server saves a session. The server saves the record "7f3a is Asha" in the store, and sends the id 7f3a to her browser in a cookie.
The server sends a token that names the user and an expiry time, and signs it. A (A common format for a signed token: the facts about the user, an expiry time, and a signature that servers can check.) is the common format. A server that has the key checks the signature with no lookup. If anyone changes the token, the signature no longer matches. Often the login service signs with a private key, and other servers check with a public key that cannot sign. With no lookup, a token cannot be taken back before it expires.
In the picture, Asha's token says she is logged in until 10:15. Step through both cases: she clicks, logs out at 10:05, and at 10:10 someone sends a copy of her login.
The session needs a lookup on every click, but deleting the record ends the login at once, so the copy gets 401. The token needs no lookup, but nothing can cancel it, so the copy works until 10:15. The next step shows how short-lived tokens limit that risk.
Keep tokens short-lived
Call this short-lived token an (The short-lived token sent with each request, to show that it comes from a logged-in user.), and give it a life such as 15 minutes. A second, long-lived (A long-lived value, looked up by the login service, that is used to get a new access token.) gets a new one, and the login service looks that refresh token up each time. Logout deletes the refresh token. So new access tokens stop, and the last one dies within 15 minutes.
Let another site check the user
With "Sign in with Google", the user types the password at Google only. Google sends the app a code that works once, and the app's server swaps it for a token that says who the user is. These steps are a standard called (The standard steps that let an app use your account at another site without seeing your password. OpenID Connect adds who you are, as in "Sign in with Google".) with OpenID Connect.
Then check what they may do
Pick a case to follow belowWith this case: Asha passed both checks: the server knew who she was, and a viewer may view.
Browser, Asha Browser Asha Shop server, checks each request, NOW Shop server checks each request NOW Session store, id → user Session store id → user - Browser — Asha — healthy
- Shop server — checks each request — healthy — NOW
- Session store — id → user — healthy
- Request
- Answer
Step 1 of 3Step 1 of 3 · Shop server
Done: A request arrives: GET /orders/7
- What happens
- It brings Asha's login.
- Why
- Every request is checked, not only the first one.
- What changes
- First the server checks who is asking.
- Next
- Authentication: it is Asha
Who is it?–not checked yetMay they do it?–a viewer may viewAnswer–Step 1 of 3: Shop server. A request arrives: GET /orders/7. It brings Asha's login.
What these pictures simplify. These pictures follow one user, Asha, at stated times. A real login service also limits password guesses, and a real token is often refreshed every few minutes. The API gateway lab above checks logins on live traffic.
Authorization runs on every request, after authentication. A common rule uses roles: a viewer may read, an editor may also edit, an admin may also delete. Check it in the service that owns the data, not only at the gateway. A request with no valid login gets a 401 answer. A known user who may not do the action gets a 403.
In the picture, Asha is signed in to the shop's admin page as a viewer. Switch between the three requests to see which check stops each one.
Use it when
- Sessions for a single web app: they are simple, and logout works at once.
- Signed tokens when many services or regions must check a login without sharing one session store.
- Sign-in through Google or a company login, so your app never handles passwords.
Skip it when
- A token that lasts for weeks with no way to refuse it: a stolen copy works for weeks.
- Secrets or personal data inside a token: it is signed, not hidden, so anyone who holds it can read it.
- Roles checked only in the browser or only at the gateway: a request that reaches the service another way skips the check.
Check yourself
Also
More in 60 seconds
What "Sign in with Google" does
Signing in with Google lets an app learn who you are without ever seeing your password: Google checks you, then tells the app.
Scene 1 of 6 · Click the button
You click "Sign in with Google", and the notes app never asks you for a password.
- Your browser — you are Asha — healthy
- Notes app — wants to know you — healthy
- Google — checks who you are — healthy
- Passwords the notes app saw
- 0
- Click the button. You click "Sign in with Google", and the notes app never asks you for a password.
- Sent to Google. The app sends your browser on to Google, with a note saying which app is asking.
- Sign in at Google. You type your password at Google only, and agree to share your name and email.
- Back with a code. Google sends your browser back to the app with a short code that works only once.
- Code swapped for a token. The app's server sends Google the code and its own secret, and gets back a signed token about you.
- The app logs you in. The app reads your name from the token and starts its own session, as after a password.
- Now you try. Asha is signed in to the notes app. Her role decides what she may do. Pick her role. For example: As a viewer, Asha may read a note. The app refuses the other 3 actions with a 403 answer, which means "not allowed". Signing in proved who she is; the role check, run on every request, decides what she may do.
Scene 1 of 6: Click the button. You click "Sign in with Google", and the notes app never asks you for a password.
Key points
- Your password goes to Google only; the app never sees it.
- The code in the browser works once, and is useless without the app's own secret.
- The token travels from server to server, so it never passes through your browser.
- Proving who you are is authentication; what you may do next is authorization.
Judgement
Trade-offs and how it fails
What you give up
- Sessions
- Easy to end and to change, but every request costs a lookup, and every server needs the same store.
- Tokens
- No lookup and no shared store, but hard to take back, so they need short lives and a refresh step.
Failure modes
- The session store goes down, and every user is logged out at once.
- A signing key leaks: anyone can make a valid token for any user until the key is changed.
- A stolen token is used after logout, because nothing checks a list of ended tokens.
- A service trusts a user id sent by the client, and one user reads another user's orders.
In the real world
- A JWT is three parts joined by dots: a header, the facts about the user, and a signature. The facts are encoded, not hidden.
- OpenID Connect adds an ID token to OAuth 2.0, so an app learns who signed in. "Sign in with Google" uses it. Source: OpenID Foundation: how OpenID Connect works (checked 14 October 2026)
- Auth0, Okta and the open-source Keycloak run login as a service, so an app does not store passwords itself.
- Redis is a common session store, because it keeps data in memory and a lookup by id is fast: often about a millisecond, network included.
Trade-offs to be ready to discuss: Sessions vs signed tokens · Short vs long token life
Check yourself
Interview
What interviewers look for
- Say where the login is checked: "The gateway checks the token's signature; each service still checks what this user may do."
- Name the trade-off before you are asked: sessions end at once, and tokens scale without a shared store.
- For logout with tokens, say: "short-lived access tokens, and a refresh token that the login service can refuse".
Say it out loud: Why do access tokens have short lives?
Expected depth by interview level
Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.
- JuniorUseful
- What a session and a token are, and where a login is checked.
- MidExpected
- Sessions against tokens, token expiry, and where roles are checked.
- SeniorExpected
- Logout and stolen tokens, sign-in through another site, and checks between services.
- StaffUseful
- Which services accept which tokens across teams and regions, and one sign-in for many apps.
Practice
Apply it
Three harder questions
The first two use the idea on a new case. The last asks you to judge a design.
Apply it
Apply it
Judge a design
Go deeper
Sources and further reading
First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- StandardRFC 6749: The OAuth 2.0 Authorization FrameworkIETFHow an app gets limited access on a user’s behalf, with access and refresh tokens.
- StandardOpenID Connect Core 1.0OpenID FoundationThe sign-in layer on top of OAuth 2.0: the ID token and what it proves.
- Engineering guideAuthentication Cheat SheetOWASPRules you can use for passwords, sign-in error messages, locking an account, and a second sign-in step.