Authentication vs authorization explained: sessions vs JWT, OAuth 2.0, OpenID Connect and SSO
Authentication checks who you are; authorization decides what you may do. A web app proves your identity once (with a password stored as a slow, salted hash, a passkey or a second factor), then remembers you on every request, either with a session (a random ID in a cookie, the data on the server) or with a token such as a JWT (signed claims the server checks without a lookup). OAuth 2.0 lets an app act on your behalf without your password, OpenID Connect adds a real login on top of it, and single sign-on shares one login across many apps.
Video · Authentication Explained: Sessions vs JWT vs OAuth 2.0, OpenID Connect and SSO · 18:58
Authentication vs authorization, in one paragraph
Prefer to watch? The 18-minute video covers the same ground with diagrams for every concept. In a hurry? The one-minute Shorts: How does a JWT work? and What is OAuth?.
Authentication (often shortened to AuthN) answers the question who are you? You prove your identity with something like a password, a fingerprint or a one-time code. Authorization (AuthZ) answers what are you allowed to do? Now that the system knows it is you, may you open this document, or delete that project? Authentication always comes first, because you cannot decide what someone may do before you know who they are.
At a conference, showing your ID at the front desk is authentication; the badge that opens some rooms and not others is authorization.
Throughout this guide we use one running example: Quill, a small notes app with a web front end and an API behind it. You can sign in with an email and a password or with a "Sign in with Google" button, and Quill can read your Google Calendar to turn a meeting into a note. Each of those features uses a different piece of the authentication puzzle.
| Authentication (AuthN) | Authorization (AuthZ) | |
|---|---|---|
| Question | Who are you? | What may you do? |
| Happens | First, at sign-in | On every protected action |
| Proof or rule | Password, passkey, one-time code | Roles, ownership, scopes, policies |
| Failure in HTTP | 401 Unauthorized | 403 Forbidden |
| Quill example | Sam signs in | Sam may open his own notes, not Lee's |
401 vs 403: authentication and authorization errors in HTTP
The two words show up in HTTP status codes, which travel in every web response (the guide to how the internet works covers HTTP itself). If you ask the Quill API for a note without signing in, it answers 401 Unauthorized. The name is misleading: RFC 9110 defines 401 as a request that lacks valid authentication credentials, so it really means unauthenticated.
If Sam is signed in but asks for Lee's note, the API answers 403 Forbidden: the server knows exactly who is asking, and the answer is still no. A useful rule of thumb: 401 means "please sign in", 403 means "signing in again will not help".
How passwords are stored: hashing and salting
Most logins still start with a password, so the first question is how Quill should store it. Never as plain text: if the database leaks, every account is exposed, along with every other site where people reused that password. Not encrypted either: encryption is reversible, and whoever obtains the key obtains every password.
The answer is a hash, a one-way fingerprint of the password. At sign-in, Quill hashes what you typed and compares it with the stored hash. If they match, the password was right, and Quill never needed to keep the password itself.
Two details make this safe. The first is a salt: a random value, different for every user, mixed into the password before hashing. The same password then produces different hashes for different people, which makes precomputed tables of common passwords useless. Modern password hashing libraries create and store the salt for you.
The second detail is speed, or rather the lack of it. General-purpose hashes such as SHA-256 are designed to be fast, which is exactly wrong for passwords: an attacker with graphics cards can try billions of guesses a second. Password hashing functions are slow on purpose, and some also need a lot of memory.
The OWASP Password Storage Cheat Sheet recommends, in this order: Argon2id (minimum 19 MiB of memory, 2 iterations), scrypt when Argon2id is not available, bcrypt for legacy systems (work factor of at least 10, and passwords are limited to 72 bytes), and PBKDF2 with 600,000 iterations when FIPS-140 compliance is required. One sign-in stays fast enough for a person; billions of guesses become far too expensive.
Sessions and cookies: how a server remembers you
Once you are signed in, a new problem appears. HTTP is stateless: every request stands alone. So how does the next request know it is still you?
The classic answer is a session. After checking your password, the server creates a small record that says "this visitor is Sam" and gives it a long random ID. It sends that ID back in a cookie, a small value the browser stores and sends back automatically with every request to the same site. On the next request, the server reads the session ID from the cookie, looks it up and knows it is Sam.
Sessions live on the server, in a session store: often a fast key-value store such as Redis, or a database table. The cookie holds only a random ID with nothing inside, and all the real information stays on the server.
That gives the server full control. To log someone out, or to cut off a stolen session, it deletes the record, and the next request with that ID is refused. The cost is a lookup on every request, and a shared store that every server behind the load balancer can reach.
The session cookie flags: HttpOnly, Secure, SameSite
Because the session cookie is the key to the account, it gets three protective attributes in its Set-Cookie header (MDN documents each one):
- HttpOnly: JavaScript on the page cannot read the cookie, so an injected script cannot steal it.
- Secure: the browser sends the cookie only over HTTPS, never in plain text.
- SameSite: controls whether the cookie is sent when another site triggers a request to yours.
StrictorLaxblocks most of those requests, which helps against forged requests;Nonemust be combined withSecure.
Session management rules
The OWASP Session Management Cheat Sheet adds a few rules. Session IDs need at least 64 bits of entropy from a secure random generator, so nobody can guess one. The app must issue a new session ID right after login, so an ID planted before login (session fixation) becomes useless. Sessions need both an idle timeout and an absolute timeout, enforced on the server. And logout must destroy the session on the server, not only delete the cookie in the browser.
What is a JWT? Tokens explained
The other big approach is a token. A session ID is like a coat check ticket: just a number that the server must look up. A self-contained token is more like a signed pass: the facts are written on it, with a signature that proves who wrote it. Any server holding the right key can check it on its own, with no lookup. The most common format is the JSON Web Token, or JWT (RFC 7519, which suggests pronouncing it "jot").
A JWT has three parts separated by dots:
| Part | What it holds | Quill example |
|---|---|---|
| Header | How the token is signed | algorithm RS256, type JWT |
| Payload | The claims, facts about the user | sub (subject) sam-42, iss (issuer), aud (audience), exp (expiry) |
| Signature | A signature over header and payload, made with the server's key | changes completely if one character changes |
Signed, not encrypted
Here is the part that surprises many developers: a normal JWT is signed, not encrypted. The header and payload are only base64url encoded, and anyone can decode them. RFC 7519 is explicit that the encoding is not encryption, and that tokens carrying sensitive information must be encrypted or sent only over encrypted transport.
So never put secrets in a JWT payload. What the signature gives you is integrity: anyone can read the token, but nobody can change it without the key. With a shared secret (such as HS256), the same key signs and checks. With a key pair (such as RS256 or ES256), only the issuer can sign, and anyone holding the public key can check.
How an API checks a JWT without a lookup
After login, the Quill server returns a JWT called an access token. The web app sends it with every request in the Authorization header as a bearer token; bearer means whoever holds it can use it, like cash. The API checks the signature, the expiry (exp), the issuer (iss) and the audience (aud). If all of them pass, it trusts the claims, with no session store and no database lookup. That is why JWTs are popular when many services, for example in a microservices architecture, must check the same login.
The JWT revocation problem, and access and refresh tokens
The same property creates the main weakness. A session can be deleted on the server; a JWT cannot. Once issued, it stays valid until it expires. If Sam logs out, or someone steals the token, it keeps working. A deny list of revoked tokens works, but then every request checks a store again, and you have rebuilt sessions.
The usual answer is to keep access tokens short lived, minutes rather than days, and pair them with a refresh token. At login the app receives both. The access token goes with every request. The refresh token is a long-lived credential used only to obtain new access tokens (RFC 6749, section 1.5).
When the access token expires, the API answers 401, and the app quietly trades the refresh token for a fresh pair. That trade is the moment the server can check that the login is still allowed. The OAuth 2.0 Security Best Current Practice (RFC 9700) requires refresh tokens for public clients, such as browser and mobile apps, to be either sender-constrained or rotated: each refresh token works once and is replaced, so a stolen copy is noticed quickly.
Where to store tokens: cookie vs localStorage
In a browser, each place to keep a token has a different risk. Cross-site scripting (XSS) is when an attacker gets their own JavaScript to run on your page. Cross-site request forgery (CSRF) is when another site makes your browser send a request to yours, with your cookies attached.
| Where | Can JavaScript read it? | Main risk | The defence |
|---|---|---|---|
| localStorage | Yes | XSS: an injected script copies the token | Keep scripts out; no storage-level defence |
| HttpOnly cookie | No | CSRF: cookies are sent automatically | SameSite and anti-CSRF tokens |
| On the server (backend for frontend) | Never reaches the browser | The web app's own session | A session cookie, as above |
Because XSS can read anything in localStorage and CSRF has well-known defences, many teams keep tokens in HttpOnly, Secure cookies, or keep them entirely on the server behind the web app's own session.
Session vs JWT: which one to use
Neither approach is more secure by default. They make different trade-offs:
| Sessions | JWT access tokens | |
|---|---|---|
| Where the state lives | On the server | Inside the token |
| Each request | A lookup in the session store | Check the signature, no lookup |
| Logout and revocation | Instant: delete the record | Wait for expiry, or keep a deny list |
| What the browser holds | A short random ID | A larger token anyone can read |
| Fits best | One web app and its own server | Many services, APIs, mobile apps |
What is OAuth 2.0?
Quill wants to read your Google Calendar. Early apps solved that by asking for your password, which let them do anything as you until you changed it. OAuth 2.0 (RFC 6749) solves it properly. It is a standard for delegated authorization: you let an app do a few specific things on your behalf without ever handing it your password.
OAuth defines four roles:
- Resource owner: you, the person who owns the data.
- Client: the app that wants access, here Quill.
- Authorization server: signs you in, asks for your consent and issues tokens, here Google's sign-in.
- Resource server: the API that holds the data, here Google Calendar, which accepts valid access tokens.
The authorization code flow
The most important OAuth flow is the authorization code flow:
- You click "Connect calendar". Quill sends your browser to Google with its client ID, the scopes it wants and a random
statevalue (protection against CSRF). - Google signs you in and asks for your consent.
- Google sends your browser back to Quill with a short-lived authorization code.
- Quill's server exchanges the code for an access token directly with Google, so the token never passes through the address bar.
- Quill calls the Calendar API with the token and receives your events.
PKCE: protecting the code
What if an attacker intercepts the code on its way back, for example a malicious app registered for the same redirect on a phone? PKCE (Proof Key for Code Exchange, RFC 7636, pronounced "pixy") closes that gap. Before starting, the app creates a random secret, the code verifier, and keeps it. It sends only a hash of it, the code challenge, in the first redirect.
When it exchanges the code, it sends the verifier itself, and the authorization server checks that its hash matches the challenge. A stolen code is useless without the verifier.
RFC 9700 says public clients must use PKCE, and it is recommended for confidential clients too.
Scopes, and two flows to avoid
A scope is a named permission, such as read-only access to a calendar. The consent screen lists the scopes, and the access token only works for what the user approved, so asking for less is both safer and easier for people to accept.
You will still read about two older flows. The implicit flow returned the access token directly in the redirect address, and the resource owner password credentials grant let the app collect your password. RFC 9700 says clients should not use the implicit grant and must not use the password grant. The draft OAuth 2.1 specification drops both.
OAuth is also how agents and tools connect to remote services: the Model Context Protocol builds its authorization for remote servers on OAuth.
OpenID Connect: why OAuth alone is not login
A common mistake is to use OAuth alone to log users in. As oauth.net puts it, OAuth 2.0 is not an authentication protocol. An access token says what an app may do at an API. It does not tell Quill who you are, when you signed in, or even that the token was issued for Quill: its audience is the API. An app that treats any valid access token as a login can be fooled with a token that was issued to a different app.
OpenID Connect (OIDC) fixes this with a thin identity layer on top of OAuth 2.0. The app adds the openid scope to the same authorization code flow and receives an ID token next to the access token. The ID token is a JWT about the sign-in itself:
| Claim | Meaning |
|---|---|
| iss | Who issued it, for example Google |
| sub | The user's stable identifier |
| aud | Must contain the app's own client ID |
| exp, iat | When it expires, when it was issued |
| nonce | The random value the app sent, to stop replays |
Quill verifies the signature, the issuer, the audience, the expiry and the nonce, and then it knows who you are. A "Sign in with Google" button is OpenID Connect; plain OAuth is only needed when Quill also wants your calendar.
What is SSO (single sign-on)?
Single sign-on means you sign in once and get into many apps. At work, the apps stop checking passwords themselves and all trust one identity provider, such as Okta, Microsoft Entra ID or Google Workspace. When you open the company wiki, it redirects you to the identity provider. You already have a session there from signing in this morning, so you come straight back with an ID token, with no password typed.
SSO usually runs on OpenID Connect or on the older SAML standard. It also gives one place to enforce MFA, and one switch to remove someone's access when they leave.
MFA and passkeys
Passwords get phished, guessed and reused. Multi-factor authentication (MFA) asks for proof of a second kind: something you know (a password, a PIN), something you have (a phone, a security key) or something you are (a fingerprint). Not every second factor is equal: text message codes can be intercepted, and any code a person types can also be typed into a convincing fake site.
Passkeys, built on the FIDO and W3C WebAuthn standards, remove the password entirely. When you create one, your device makes a key pair. Quill keeps only the public key; the private key stays on your device or is synced by your password manager, and it is never sent to Quill. To sign in, Quill sends a fresh random challenge, you unlock with your face, fingerprint or PIN, and the device signs the challenge, which Quill checks with the public key.
There is no shared secret in Quill's database for attackers to steal, and a passkey only works on the real site it was created for, so a look-alike domain cannot use it. That is why the FIDO Alliance describes passkeys as phishing-resistant.
Choosing: a short decision guide
- One web app with its own server: sessions in an HttpOnly, Secure cookie. Simple, and logout really works.
- Many services that check the same user: short-lived JWT access tokens with rotated refresh tokens.
- Your app reads someone's data from another service: OAuth 2.0 with the authorization code flow and PKCE.
- Sign in with Google, or with a company account: OpenID Connect, and SSO across many apps.
- Always: protect the sign-in with MFA and offer passkeys.
And one rule above the rest: use a well-tested library or identity service rather than writing your own. Test the sign-in, logout and expiry paths like any other critical journey (the guide to the types of software testing explains which tests fit).
If your app also uses real-time connections, remember that a WebSocket starts with an ordinary HTTP request, so the same cookies or tokens authenticate it. For AI features, keep authorization checks in code rather than in prompts, as the article on guardrails in code, not prompts explains, and let a person approve the riskiest actions (human in the loop).
Recap
Authentication is who you are; authorization is what you may do, and HTTP marks the difference with 401 and 403. Passwords are stored as slow, salted hashes such as Argon2id.
Sessions keep the state on the server behind a random ID in a cookie; JWTs carry signed claims that anyone can read, so they are kept short lived and paired with refresh tokens. OAuth 2.0 gives an app limited, revocable access through the authorization code flow with PKCE, OpenID Connect adds the ID token for real login, SSO shares one login across many apps, and MFA and passkeys protect the sign-in itself.
Sources: RFC 7519, JSON Web Token · RFC 6749, OAuth 2.0 · RFC 7636, PKCE · RFC 9700, OAuth 2.0 Security Best Current Practice · OAuth 2.1 draft · OpenID Connect Core 1.0 · End User Authentication with OAuth 2.0, oauth.net · RFC 9110, HTTP Semantics · OWASP Password Storage Cheat Sheet · OWASP Session Management Cheat Sheet · MDN, Set-Cookie · FIDO Alliance, Passkeys · W3C Web Authentication
FAQ
What is the difference between authentication and authorization?
Authentication answers who you are: you prove your identity with a password, a passkey or a second factor. Authorization answers what you may do once the system knows who you are, such as opening one note but not another. In HTTP, a 401 status means the request lacks valid authentication, and a 403 means the server knows who you are and still refuses.
Is a JWT encrypted?
Usually not. A normal JWT is signed, not encrypted: its header and payload are only base64url encoded, so anyone holding the token can read the claims. The signature guarantees that nobody changed them without the key. Never put secrets in a JWT payload, and send tokens only over HTTPS.
Should I use sessions or JWT?
For one web app with its own server, sessions in an HttpOnly, Secure cookie are simpler and logout takes effect at once. JWT access tokens fit when many services or APIs must check the same user without a shared session store. Keep them short lived and pair them with refresh tokens, because a JWT stays valid until it expires.
What is OAuth 2.0 in simple terms?
OAuth 2.0 is a standard for delegated authorization. It lets you allow an app to do a few specific things on your behalf, such as reading your calendar, without giving it your password. The app receives an access token limited to the scopes you approved, through the authorization code flow with PKCE.
What is the difference between OAuth and OpenID Connect?
OAuth 2.0 grants an app access to an API; it does not tell the app who the user is. OpenID Connect is an identity layer on top of OAuth: the app adds the openid scope and receives an ID token, a signed JWT that names the issuer, the user (sub) and the app it was issued for (aud). Use OpenID Connect for login, such as a Sign in with Google button.
Read next
- How Does the Internet Work? Every Networking Concept Explained
- What Is a WebSocket? WebSocket vs SSE vs Polling vs HTTP
- What Is MCP? The Model Context Protocol Explained
- Big O Notation Explained Simply, With Everyday Examples
- What Is Docker? Docker and Kubernetes Explained Simply
- Software Engineering Unpacked: home
- The CCAR-F video playlist on YouTube