Understanding the fundamentals of OAuth 2.0.
What is OAuth 2.0?
OAuth 2.0 is an authorization framework that enables applications to obtain limited access to user resources without exposing credentials.
The Problem OAuth Solves
Without OAuth, applications would need:
Direct access to user credentials
Full access to all resources
No way to revoke access without changing passwords
OAuth provides:
Delegated authorization (no password sharing)
Scoped access (limited permissions)
Revocable tokens (easy access removal)
The user who owns the data
The application requesting access
Issues tokens after authorization
Hosts the protected resources (API)
Scopes define what access is granted:
Clients request scopes:
Users consent to scopes:
Authorization Code (Most Common)
Best for: Web applications with a backend
The client never sees the user's password.
Authorization Code + PKCE
Best for: Mobile apps, single-page applications
Same as authorization code, but with proof key:
Client generates random code_verifier
Client sends code_challenge = SHA256(code_verifier)
Client proves identity with code_verifier
Client Credentials
Best for: Machine-to-machine
No user involved - the service itself is authorized.
Best for: TVs, CLI tools, IoT
The Authorization Flow
User wants to access protected resource
User clicks "Login with Authority" in your app.
Client redirects to Authorization Server
User authenticates
Authority shows login page. User enters credentials.
User authorizes
Authority shows consent screen. User approves scopes.
Authorization Server redirects back
Client exchanges code for tokens
Security Concepts
State Parameter
Prevents CSRF attacks:
Client generates random state
Client stores state in session
Client includes state in authorization request
Authority returns state in callback
Client verifies state matches
Prevents authorization code interception:
Short-lived access tokens - Limit exposure window
Token rotation - New refresh token each use
Secure storage - Never store in URL or logs
Common Misconceptions
"OAuth is for authentication"
OAuth is for authorization (what you can do), not authentication (who you are). OpenID Connect adds authentication.
"The access token contains user data"
Access tokens authorize access - they don't necessarily contain user info. Use the UserInfo endpoint or ID tokens for identity.
"Longer token lifetimes are more secure"
Shorter lifetimes with refresh tokens are more secure. If a token is compromised, the damage window is limited.