Bird
Raised Fist0
HLDsystem_design~7 mins

OAuth 2.0 flow in HLD - System Design Guide

Choose your learning style10 modes available
Problem Statement
When users want to access multiple services, asking them to share their passwords with every service creates security risks and poor user experience. Without a standard way to delegate access, services either store sensitive credentials or force users to create new accounts everywhere.
Solution
OAuth 2.0 flow allows users to grant limited access to their resources on one service to another service without sharing their passwords. It works by issuing tokens after user consent, which the requesting service uses to access resources securely and temporarily.
Architecture
┌───────────────┐       Authorization Code       ┌───────────────┐
│               │ ─────────────────────────────> │               │
│   Client App  │                                │ Authorization  │
│               │ <───────────────────────────── │    Server     │
└───────────────┘          Authorization Code    └───────────────┘
       │                                              │
       │                                              │
       │          Access Token Request                 │
       │ ───────────────────────────────────────────> │
       │                                              │
       │ <────────────────────────────────────────── │
       │               Access Token                    │
       │                                              │
       │                                              │
       │          Resource Request with Token         │
       │ ───────────────────────────────────────────> │
       │                                              │
       │ <────────────────────────────────────────── │
       │             Protected Resource                │
       ▼                                              ▼

This diagram shows the OAuth 2.0 Authorization Code flow where the client app requests an authorization code from the authorization server, exchanges it for an access token, and then uses the token to access protected resources.

Trade-offs
✓ Pros
→
Users do not share passwords with third-party apps, improving security.
→
Access tokens can be limited in scope and time, reducing risk if leaked.
→
Standardized flow supported by many platforms and libraries.
→
Supports delegated access without exposing user credentials.
✗ Cons
→
Requires multiple network calls, adding latency to the authentication process.
→
Implementation complexity is higher than simple password sharing.
→
Token management and revocation require careful handling to avoid misuse.
Use OAuth 2.0 when your system needs to allow third-party apps limited access to user data without sharing passwords, especially at scale with many users and services.
Avoid OAuth 2.0 for simple internal applications with no third-party access or when user base is very small (under 100 users) and simpler authentication suffices.
Real World Examples
Google
Google uses OAuth 2.0 to let users grant third-party apps access to their Google Drive files without sharing their Google passwords.
Facebook
Facebook uses OAuth 2.0 to allow apps to post on users' behalf or access profile information securely.
GitHub
GitHub uses OAuth 2.0 to enable developers to authorize external tools to access repositories and user data without exposing credentials.
Alternatives
API Keys
API keys are static tokens given to clients without user consent flow or token expiration.
Use when: Use API keys for simple service-to-service authentication without user delegation.
OpenID Connect
OpenID Connect builds on OAuth 2.0 to add user identity verification and profile information.
Use when: Choose OpenID Connect when you need both authentication and authorization with user identity.
SAML
SAML uses XML-based assertions for single sign-on, mainly in enterprise environments.
Use when: Use SAML for enterprise single sign-on where legacy systems require it.
Summary
OAuth 2.0 flow prevents password sharing by issuing limited access tokens after user consent.
It uses an authorization server to grant tokens that clients use to access protected resources securely.
This flow is widely adopted for delegated access across many popular platforms.

Practice

(1/5)
1. What is the main purpose of the OAuth 2.0 Authorization Code flow?
easy
A. To replace user authentication with biometric data
B. To store user passwords safely on the server
C. To allow apps to access user data securely without sharing passwords
D. To encrypt all user data before sending it

Solution

  1. Step 1: Understand OAuth 2.0 Authorization Code flow

    This flow lets apps get permission to access user data without needing the user's password directly.
  2. Step 2: Identify the main goal

    The goal is to keep user credentials safe while allowing apps to act on behalf of the user.
  3. Final Answer:

    To allow apps to access user data securely without sharing passwords -> Option C
  4. Quick Check:

    OAuth 2.0 Authorization Code flow = Secure app access without passwords [OK]
Hint: Remember: OAuth avoids sharing passwords directly [OK]
Common Mistakes:
  • Confusing OAuth with password storage
  • Thinking OAuth encrypts all user data
  • Assuming OAuth replaces authentication methods
2. Which of the following is the correct sequence in the OAuth 2.0 Authorization Code flow?
easy
A. User logs in -> App receives access token -> App requests authorization code
B. App sends password -> User grants access -> App receives token
C. App directly receives access token -> User logs in -> Access granted
D. User logs in -> App gets authorization code -> App exchanges code for access token

Solution

  1. Step 1: Recall the OAuth 2.0 Authorization Code flow steps

    User logs in and authorizes the app, which then receives an authorization code.
  2. Step 2: Understand token exchange

    The app exchanges the authorization code for an access token to access user data.
  3. Final Answer:

    User logs in -> App gets authorization code -> App exchanges code for access token -> Option D
  4. Quick Check:

    Authorization Code flow sequence = Login, code, token [OK]
Hint: Authorization code comes before access token [OK]
Common Mistakes:
  • Thinking app gets token before authorization code
  • Confusing password sending with OAuth flow
  • Mixing order of login and token receipt
3. Given this OAuth 2.0 flow snippet, what is the expected output after exchanging the authorization code?
POST /token
Headers: Content-Type: application/x-www-form-urlencoded
Body: grant_type=authorization_code&code=abc123&redirect_uri=https://app.com/callback&client_id=client123&client_secret=secretXYZ

Response:
{
  "access_token": "token456",
  "token_type": "Bearer",
  "expires_in": 3600
}
medium
A. An access token 'token456' valid for 3600 seconds
B. An error because client_secret is missing
C. A refresh token instead of access token
D. No token, only a confirmation message

Solution

  1. Step 1: Analyze the token request

    The app sends a POST request with grant_type authorization_code and includes the code and client credentials.
  2. Step 2: Interpret the response

    The response contains an access_token 'token456', token_type 'Bearer', and expires_in 3600 seconds, meaning the token is valid for one hour.
  3. Final Answer:

    An access token 'token456' valid for 3600 seconds -> Option A
  4. Quick Check:

    Token response includes access_token and expiry [OK]
Hint: Token response includes access_token and expiry time [OK]
Common Mistakes:
  • Expecting refresh token instead of access token
  • Missing client_secret causes error
  • Confusing confirmation message with token response
4. A developer implements OAuth 2.0 Authorization Code flow but the app never receives an access token after exchanging the code. What is the most likely cause?
medium
A. The app forgot to include the client_id in the authorization request
B. The redirect URI used in the token request does not match the one registered
C. The user did not enter their password correctly
D. The access token expired before the request

Solution

  1. Step 1: Identify common OAuth token exchange errors

    One frequent error is a mismatch in redirect URI between authorization and token requests.
  2. Step 2: Understand impact of redirect URI mismatch

    If the redirect URI differs, the authorization server rejects the token request, so no access token is returned.
  3. Final Answer:

    The redirect URI used in the token request does not match the one registered -> Option B
  4. Quick Check:

    Redirect URI mismatch blocks token issuance [OK]
Hint: Check redirect URI matches exactly in all requests [OK]
Common Mistakes:
  • Blaming user password errors for token issues
  • Ignoring client_id in authorization request
  • Assuming token expires before it is issued
5. You are designing a system that uses OAuth 2.0 Authorization Code flow for a mobile app and a web app. Which approach best secures the access tokens and refresh tokens?
hard
A. Use Authorization Code flow with PKCE for the mobile app and standard Authorization Code flow for the web app
B. Use Implicit flow for both apps to simplify token handling
C. Store access tokens in local storage for both apps to allow easy access
D. Send access tokens directly in URL query parameters for both apps

Solution

  1. Step 1: Understand security needs for mobile and web apps

    Mobile apps cannot keep client secrets safe, so PKCE (Proof Key for Code Exchange) enhances security for Authorization Code flow.
  2. Step 2: Choose best practices for token storage and flow

    Web apps can use standard Authorization Code flow with client secret. Implicit flow is deprecated due to security risks. Storing tokens in local storage or URLs exposes them to attacks.
  3. Final Answer:

    Use Authorization Code flow with PKCE for the mobile app and standard Authorization Code flow for the web app -> Option A
  4. Quick Check:

    PKCE secures mobile OAuth; standard flow suits web apps [OK]
Hint: Use PKCE for mobile apps, standard flow for web apps [OK]
Common Mistakes:
  • Using Implicit flow despite security risks
  • Storing tokens insecurely in local storage
  • Passing tokens in URL parameters
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard