OAuth 2.0 and OIDC — The Visual Guide You Actually Need
Visual walkthrough of OAuth 2.0 and OpenID Connect. Understand authorization flows, JWT structure, grant types, and common security mistakes through animated diagrams.
Every developer uses OAuth. Very few actually understand it. You copy the library config from a blog post, it works, and you move on. Until it breaks — and then you’re staring at redirect loops, invalid_grant errors, and token validation failures with zero mental model of what’s happening.
Let’s fix that. From the ground up.
1. The Authorization Code Flow
This is the flow that 90% of web applications should use. It’s the most secure because the access token never touches the browser — it’s exchanged server-to-server using the authorization code.
Authorization Code Flow — Step by Step
The most common and most secure flow for web applications
Key insight: the authorization code is short-lived (usually 30 seconds) and single-use. Even if someone intercepts it, they can’t exchange it without the client_secret (which only your backend knows). That’s the security model — separate the user-facing redirect from the secret exchange.
2. OAuth vs OIDC — Finally Clear
People use “OAuth” and “OIDC” interchangeably. They’re not the same thing. OAuth handles authorization (permissions). OIDC handles authentication (identity). You usually want both — and OIDC gives you both in one flow.
OAuth 2.0 vs OpenID Connect
OAuth is Authorization (what can you access). OIDC adds Authentication (who are you).
The practical difference: if you just need “can this app read my Google Calendar?” — that’s pure OAuth 2.0 with an access_token. If you need “who is this person logging into my app?” — that’s OIDC with an id_token. Most login flows use OIDC because you need the user’s identity.
3. JWT Anatomy
The id_token (and often the access_token) is a JSON Web Token. It looks like random characters, but it’s just Base64-encoded JSON with a cryptographic signature. Understanding the structure makes debugging “invalid token” errors much easier.
Inside a JWT — Three Parts, Dot-Separated
"typ": "JWT"
"email": "jas@example.com",
"iat": 1716239022,
"exp": 1716242622,
"iss": "https://auth.example.com"
base64(header) + "." + base64(payload),
secret_key
)
Critical security point: JWTs are signed, not encrypted. Anyone can decode the payload with Base64. Don’t put sensitive data in JWT claims — put identifiers (user_id) not secrets (SSN, credit card). If you need encrypted tokens, use JWE (JSON Web Encryption), but that’s rare.
4. Grant Types — Picking the Right One
OAuth 2.0 defines multiple “grant types” — different flows for different situations. The wrong choice creates security vulnerabilities. The right choice depends on whether you have a backend, what device you’re on, and whether a human is involved.
Which Grant Type Should You Use?
Authorization Code + PKCERecommended
The gold standard. Works for web apps, mobile apps, SPAs. PKCE adds security for public clients (no client_secret stored on device).
Client CredentialsMachine-to-Machine
No user involved. Service A authenticates to Service B using its own credentials. Perfect for backend microservice communication.
Device AuthorizationLimited Input
For devices without a browser (smart TVs, CLI tools, IoT). Shows a code on device, user enters it on their phone/laptop.
Implicit (DEPRECATED)Don't Use
Returns token directly in URL fragment. No code exchange. Vulnerable to token leakage via browser history, referrer headers. Replaced by Auth Code + PKCE.
Resource Owner PasswordDon't Use
User gives their password directly to the app. Defeats the entire purpose of OAuth (delegated auth without sharing credentials). Only for legacy migration.
The decision tree is simple: human user login? Authorization Code + PKCE. Machine-to-machine? Client Credentials. No browser (TV/CLI)? Device Authorization. That’s it. The other grants are deprecated — don’t use them in new projects.
5. Security Mistakes
I’ve audited dozens of OAuth implementations. The same five mistakes appear in almost every one. None of them are caught by automated testing — they’re logic bugs in the authentication flow that create exploitable vulnerabilities.
5 Mistakes That Get You Hacked
The meta-lesson: OAuth security isn’t about the protocol being insecure. The protocol is well-designed. The vulnerabilities come from implementation shortcuts — skipping state validation, storing tokens insecurely, accepting any redirect_uri. Follow the spec exactly. Every “optional” security feature in the spec exists because someone got hacked without it.