Skip to main content

🔑 OAuth

Valet parking with limited keys

The Valet Key Analogy

Your car has two types of keys:

Full key: Opens everything - doors, trunk, glovebox, starts engine Valet key: Limited access (for example, it can start the engine but not open everything)

When you give your car to a valet, you hand them the valet key. They can drive your car, but can't access your valuables in the trunk.

OAuth is a valet key for your online accounts. Apps get limited access to your data without your password.


The Problem OAuth Solves

Without OAuth

App: "I need to read your Google calendar"
You: "Here's my Google password"

Problems:
✗ App can end up with broad access to your account
✗ App can send emails as you
✗ App can delete your files
✗ You can't revoke access without changing password
✗ App might store your password insecurely

With OAuth

App: "I need to read your Google calendar"
You: "Let me authorize that through Google..."

[Google asks: Allow this app to view your calendar?]
You: [Yes, allow]

Result:
✓ App can view your calendar (within the scope you approved)
✓ The app doesn't need to see your password
✓ You can revoke access anytime
✓ Access can expire automatically

How OAuth Works

The Players

Resource Owner:  You (the human)
Client:          The app requesting access
Authorization Server:  Google, Facebook, etc. (issues tokens)
Resource Server: Where your data lives (Google Calendar, etc.)

The Flow (Authorization Code)

1. YOU want to use an app that needs your calendar

2. APP redirects you to GOOGLE
   "User wants to connect their calendar"

3. GOOGLE asks YOU
   "Allow this app to view your calendar?"
   [Authorize] [Deny]

4. YOU click Authorize

5. GOOGLE redirects back to APP
   With an "authorization code"

6. APP exchanges code for tokens
   (Happens in the background)

7. APP uses tokens to access your calendar
  Your password wasn't shared with the app

Visual Flow

┌──────┐     ┌─────────┐     ┌──────────────┐
│ User │────→│   App   │────→│   Google     │
│      │     │(Client) │     │(Auth Server) │
└──────┘     └─────────┘     └──────────────┘
    │             │                  │
    │   1. Click "Connect Calendar"  │
    │─────────────→│                 │
    │              │  2. Redirect    │
    │              │────────────────→│
    │              │                 │
    │ 3. "Allow access?"             │
    │←───────────────────────────────│
    │              │                 │
    │ 4. User approves               │
    │────────────────────────────────→
    │              │                 │
    │              │ 5. Auth code    │
    │              │←────────────────│
    │              │                 │
    │              │ 6. Exchange for token
    │              │────────────────→│
    │              │                 │
    │              │ 7. Access token │
    │              │←────────────────│

OAuth Tokens

Access Token

What: Short-lived key to access resources
Lifetime: Typically short-lived (varies by provider)
Use: Attached to every API request

Example header: Authorization: <access_token>

In practice, there's usually an auth-scheme prefix (so it looks like: `Authorization: <scheme> <access_token>`).

Many OAuth 2.0 APIs use the standardized scheme defined for access tokens (RFC 6750), but some providers use variations.

Note: OAuth doesn't require a specific token format. Access tokens might be opaque strings or structured tokens (like JWTs), depending on the provider.

Refresh Token

What: Long-lived token to get new access tokens
Lifetime: Often longer-lived than access tokens (varies by provider)
Use: Exchange for fresh access token when it expires

Typically not sent to the resource server

Token Lifecycle

1. User authorizes (and may sign in) → Get an access token (and sometimes a refresh token)
2. Make API calls with access token
3. Access token expires
4. Use refresh token to get new access token
5. Repeat until refresh token expires → User logs in again

Scopes: Limited Permissions

Scope defines WHAT the app can do:

Full access:
  scope=calendar.full drive.full mail.full
  (Too much! Dangerous!)

Limited access:
  scope=calendar.read

When you authorize, you see:
  "This app wants to:
   ✓ View your calendar events
   ✗ Send emails on your behalf"

OAuth Grant Types

Authorization Code (Most Common)

For: Web apps (and also SPAs/mobile apps when used with PKCE)
Flow: User → App → Auth Server → App gets code → Exchange for tokens
Often preferred: For server-side apps, tokens can be exchanged/stored on the server

Client Credentials

For: Machine-to-machine (no user involved)
Flow: App authenticates directly with ID + secret
Use: Backend services talking to each other

Implicit (Legacy)

For: Historically used for browser-based apps
Problem: Access credentials can end up in the browser address bar
Status: Generally avoid. Prefer Authorization Code + PKCE instead

PKCE (Proof Key for Code Exchange)

For: Mobile and single-page apps
Problem: Can't safely store client secrets
Solution: Dynamic one-time keys
Now: Commonly recommended for public clients

OAuth vs OpenID Connect

OAuth:
  "Allow this app to access my photos"
  Authorization (what can you do?)

OpenID Connect (OIDC):
  "Use Google to sign me in"
  Authentication (who are you?) + Authorization
OIDC = OAuth + Identity layer

Common Use Cases

Third-Party Sign-In

"Continue with Google"
"Continue with Facebook"
"Continue with GitHub"

App doesn't manage passwords.
Users don't create another account.

Note: This is usually implemented with OpenID Connect (OIDC) on top of OAuth.

Third-Party Integrations

Slack connecting to Google Drive
Zapier connecting to hundreds of apps
Mobile apps accessing cloud services

API Access

Developer builds app using Twitter API
Gets OAuth tokens through developer portal
App tweets on behalf of users who authorize

Security Tips

1. Use Authorization Code Flow

Avoid implicit flow (credentials can end up in logs, history)

2. Validate Redirect URIs


In general: avoid open redirects, require exact matches, and prefer HTTPS redirect URIs.

3. Use PKCE for Public Clients

Mobile and SPA apps can't keep secrets
PKCE adds dynamic verification

4. Short-Lived Access Tokens

If token leaked, damage is limited
Use refresh tokens for long sessions

5. Request Minimum Scopes

Don't ask for more than you need Users trust apps with narrow permissions


---

## Common Mistakes

### 1. Storing Tokens Insecurely

```text
If an attacker can run JavaScript in your page (XSS), they can often steal tokens stored in web-accessible browser storage.

Common approaches:
- Store tokens in memory (reduces persistence but still vulnerable to XSS)
- Use httpOnly cookies (reduces token exfiltration via JS, but requires CSRF defenses like SameSite and anti-CSRF tokens)

There's no one-size-fits-all answer — follow your framework/provider guidance and threat model.

2. Not Validating State Parameter

State parameter prevents CSRF attacks
Generate a new state value per request, and validate it on return

If you're using OpenID Connect for login, also validate the `nonce` in the ID token.

FAQ

Q: OAuth vs API keys?

API keys identify the app. OAuth grants scoped access on behalf of a user.

Q: Can OAuth be used for authentication?

OAuth alone = authorization. Add OpenID Connect for authentication.

Q: What if my refresh token is stolen?

Revoke it immediately. Use token binding or sender-constrained tokens for high-security apps.

Q: Do I need OAuth for my app?

If users grant third-party access to their data, yes. For simple logins, consider email/password or passkeys.


Summary

OAuth allows apps to access your data with limited, revocable permissions - without sharing your password.

Key Takeaways:

  • Valet key for your accounts
  • Apps typically don't need to see your password
  • Scopes limit what apps can do
  • Access tokens are short-lived
  • Refresh tokens get new access tokens
  • Use Authorization Code + PKCE flow
  • OpenID Connect adds authentication

OAuth (often via OpenID Connect for sign-in) powers millions of API integrations.

Leave a Comment

Comments (0)

Be the first to comment on this concept.

Comments are approved automatically.