What is OAuth? — Open Authorization Framework Explained
Definition
OAuth 2.0 (Open Authorization) is an industry-standard authorization framework that enables applications to obtain limited access to user accounts on an HTTP service. Instead of sharing a user's password with a third-party application, OAuth allows the user to grant a token that provides scoped, temporary access to specific resources.
OAuth 2.0 is defined in RFC 6749 and is used by virtually every major API platform including Google, GitHub, Facebook, Twitter, Microsoft, and Stripe. It is the foundation of modern API authorization.
OAuth 2.0 Roles
OAuth defines four distinct roles that interact during the authorization process:
| Role | Description | Example |
|---|---|---|
| Resource Owner | The user who owns the data and can grant access | You, the end user |
| Client | The application requesting access to the resource owner's data | A mobile app, web app, or server-side application |
| Authorization Server | The server that authenticates the resource owner and issues tokens | Google's OAuth 2.0 endpoint |
| Resource Server | The server hosting the protected data, accepting access tokens | Google Drive API |
The OAuth Flow at a Glance
Resource Owner (User)
│
▼
Client (App) ─────────► Authorization Server
│ │
│ ├── Authenticates user
│ ├── Gets consent
│ └── Issues authorization code
│
├── Exchanges code for token ──► Authorization Server
│
├── Uses access token ─────────► Resource Server
│ │
│ └── Returns protected data
▼
Successful API access
Grant Types
OAuth 2.0 defines several grant types — different flows for different application scenarios:
Authorization Code Grant (Recommended for Web Apps)
The most common and most secure flow. The client receives an authorization code after the user authenticates, then exchanges it for an access token on the server side:
- User clicks "Sign in with Provider"
- Browser redirects to the authorization server
- User authenticates and grants consent
- Authorization server redirects back with a code
- Client server exchanges the code for an access token (using client secret)
- Client uses the access token to call the resource server
Best for: Server-side web applications where the client secret can be stored securely.
Authorization Code with PKCE (Recommended for Mobile & SPAs)
PKCE (Proof Key for Code Exchange) extends the authorization code flow to prevent authorization code interception attacks. The client generates a cryptographic challenge before the redirect:
- Client generates a
code_verifier(random string) andcode_challenge(SHA-256 hash of the verifier) - The challenge is sent with the authorization request
- When exchanging the code, the client sends the original verifier
- The authorization server verifies the challenge matches
Best for: Mobile apps, single-page applications (SPAs), and any client that cannot securely store a client secret.
Client Credentials Grant
The client authenticates itself (not on behalf of a user) to access resources:
- Client sends its
client_idandclient_secretdirectly to the authorization server - Authorization server returns an access token
- Client uses the token to call the resource server
Best for: Server-to-server communication, machine-to-machine (M2M) APIs, cron jobs, and backend services.
Implicit Grant (Deprecated)
The implicit grant was designed for browser-based apps and returned the access token directly in the URL fragment. It is now deprecated due to security concerns — the token is exposed in the browser history and vulnerable to interception. Use PKCE instead.
Access Tokens vs Refresh Tokens
| Aspect | Access Token | Refresh Token |
|---|---|---|
| Purpose | Authorizes API requests | Obtains new access tokens |
| Lifetime | Short-lived (minutes to hours) | Long-lived (days to months) |
| Format | Often JWT or opaque string | Opaque string |
| Storage | Memory or secure storage | Secure storage (never in browser) |
| Usage | Sent with every API call (Authorization: Bearer <token>) |
Sent only to token endpoint |
| Revocable | Yes, but must wait for expiry | Yes, immediate revocation possible |
Why Two Tokens?
- Security: Short-lived access tokens limit the damage if compromised
- UX: Refresh tokens allow the client to obtain new access tokens without bothering the user to re-authenticate
- Control: Servers can revoke refresh tokens without impacting active sessions
Scopes
Scopes define the specific permissions the client is requesting. They limit what the access token can do:
| Scope | Permission |
|---|---|
read:user |
Read user profile information |
write:repo |
Create and modify repositories (GitHub) |
openid |
Identity authentication (OpenID Connect) |
email |
Access user's email address |
profile |
Access user's profile information |
offline_access |
Obtain a refresh token |
Scopes are requested by the client during authorization and displayed to the user for consent. The user can often choose to grant a subset of the requested scopes.
OAuth vs SAML vs OpenID Connect
| Aspect | OAuth 2.0 | SAML 2.0 | OpenID Connect (OIDC) |
|---|---|---|---|
| Purpose | Authorization | Authentication & SSO | Authentication (built on OAuth 2.0) |
| Protocol | HTTP/JSON | HTTP/XML | HTTP/JSON |
| Token Format | JWT or opaque | SAML assertions (XML) | JWT (ID token) |
| Primary Use | API authorization | Enterprise SSO | User identity & SSO |
| Complexity | Moderate | High | Moderate |
| Mobile Support | Excellent | Poor | Excellent |
| Standard | RFC 6749 | OASIS | OpenID Foundation |
When to Use What
- OAuth 2.0 — When you need to authorize third-party app access to user data (e.g., "Allow this app to post to my Twitter feed")
- OpenID Connect (OIDC) — When you need to authenticate users (e.g., "Sign in with Google"), OIDC extends OAuth 2.0 with an
id_token(a JWT containing user identity) - SAML 2.0 — In enterprise environments where XML-based SSO is standard (e.g., corporate Okta/ADFS integrations)
The Key Distinction
OAuth 2.0 is about what you can do (authorization). OpenID Connect is about who you are (authentication). They are often used together.
Common OAuth Flow (Authorization Code)
Step-by-Step Diagram
┌──────────┐ ┌──────────┐ ┌──────────┐
│ User │ │ Client │ │ Auth │
│ (Browser)│ │ (App) │ │ Server │
└────┬─────┘ └────┬─────┘ └────┬─────┘
│ │ │
│ 1. Click "Login" │ │
│────────────────────►│ │
│ │ │
│ 2. Redirect to Auth │ │
│◄────────────────────│ │
│ │ │
│ 3. Authenticate │ │
│──────────────────────────────────────────►│
│ │ │
│ 4. Consent grant │ │
│──────────────────────────────────────────►│
│ │ │
│ 5. Auth code redirect │
│◄──────────────────────────────────────────│
│ │ │
│ 6. Code to server │ │
│────────────────────►│ │
│ │ 7. Exchange code │
│ │ + client secret │
│ │────────────────────►│
│ │ │
│ │ 8. Access + refresh │
│ │◄────────────────────│
│ │ │
│ │ 9. API call + token │
│ │────────────────────►│
│ │ (Resource Server)│
│ │ │
│ 10. Success data │ │
│◄────────────────────│ │
┌────┴─────┐ ┌────┴─────┐ ┌────┴─────┐
│ User │ │ Client │ │ Auth │
│ (Browser)│ │ (App) │ │ Server │
└──────────┘ └──────────┘ └──────────┘
Common OAuth Implementation Mistakes
- Storing access tokens in localStorage — vulnerable to XSS attacks. Use httpOnly cookies or secure in-memory storage.
- Not validating the redirect_uri — allows attackers to intercept authorization codes.
- Using implicit grant — deprecated for good reason; always use authorization code + PKCE for browser-based apps.
- Not using state parameter — without a
stateparameter, your app is vulnerable to CSRF attacks on the redirect callback. - Exposing client_secret in client-side code — secrets in mobile or SPA code are not secrets; use PKCE instead.
- Ignoring token expiry — always check expiration before using an access token.
- Missing scope validation — validate that the token has the required scopes before granting access.
LangStop API & Security Tools
- JWT Encoder — Create and sign JWTs for OAuth tokens
- JWT Decoder — Inspect OAuth access tokens
- REST API Client — Test OAuth-protected API endpoints
- API Tools — Collection of API debugging utilities
- URL Encoder — Encode OAuth callback URLs and parameters