SYSTEM-DESIGN
    intermediate

    OAuth, OIDC & JWT

    Software Engineer path · lesson 85 of 99
    Not started
    User
    Client App
    Auth Server
    Resource Server

    OAuth 2.0 Authorization Flow

    OAuth 2.0 is an authorization framework that allows applications to obtain limited access to user accounts on an HTTP service.

    Security Analysis

    OAuth 2.0 Security Considerations:

    • PKCE: Proof Key for Code Exchange prevents authorization code interception
    • State Parameter: Prevents CSRF attacks during authorization flow
    • HTTPS: Required for all communications to prevent token interception
    • Scope Limitation: Request minimal necessary permissions

    JWT Security Best Practices

    • Short Expiration: Access tokens should expire within 15-60 minutes
    • Secure Storage: Use httpOnly cookies or secure storage mechanisms
    • Signature Validation: Always verify JWT signature and claims
    • Algorithm Verification: Explicitly check the 'alg' header to prevent algorithm confusion

    System Design Applications

    Microservices Authentication:

    • API Gateway: Centralized token validation and routing
    • Service-to-Service: JWT tokens for inter-service communication
    • Token Refresh: Refresh token rotation for enhanced security
    • Distributed Sessions: Stateless authentication across multiple services

    Performance Considerations

    Token Validation:

    Local validation: O(1)\mathcal{O}(1) time complexity

    Remote validation: Network latency + DB lookup

    Token Size:

    JWT: ~200-1000 bytes

    Session ID: ~32-128 bytes

    Authorization code flow

    Sequence

    Order a "Sign in with…" login using the OAuth authorization code flow.

    Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.

    1. 1Drop a step here
    2. 2Drop a step here
    3. 3Drop a step here
    4. 4Drop a step here
    5. 5Drop a step here
    5 pieces left to place

    Test your knowledge

    24 questions
    Question 1 of 24
    Score: 0

    What is the main difference between OAuth 2.0 and OpenID Connect?

    The short version

    Think of OAuth like a hotel key card system: the front desk (Authorization Server) verifies your identity and gives you a card (token) that grants access to specific areas (resources) without revealing your ID to every door scanner.

    In more depth

    OAuth, OIDC & JWT Authentication Flow

    OAuth 2.0 (Authorization)

    OAuth 2.0 is an authorization framework that allows applications to obtain limited access to user accounts. It works by delegating user authentication to the service that hosts the user account.

    Key Components:

    Resource Owner: The user who owns the data
    Client: The application requesting access
    Authorization Server: Issues access tokens (e.g., Google, Auth0)
    Resource Server: Hosts the protected resources

    Authorization Code Flow:

    1.Client redirects user to authorization server
    2.User authenticates and grants permission
    3.Authorization server redirects back with authorization code
    4.Client exchanges code for access token
    5.Client uses access token to access protected resources

    OpenID Connect (Authentication)

    OIDC extends OAuth 2.0 to provide authentication on top of authorization. It adds an identity layer.

    Key Additions:

    ID Token: Contains user identity information (JWT format)
    UserInfo Endpoint: Returns user profile information
    Standardized Scopes: openid, profile, email

    JSON Web Tokens (JWT)

    JWT is a compact, URL-safe means of representing claims between two parties.

    Structure:

    Header.Payload.Signature

    Header: Specifies algorithm and token type

    Payload: Contains claims (user info, permissions, expiration)

    Signature: Verifies token integrity

    Types:

    Access Tokens: Short-lived (15-60 minutes), authorize API access
    Refresh Tokens: Long-lived (weeks/months), obtain new access tokens
    ID Tokens: Contain user identity information (OIDC)

    Security Considerations

    Use HTTPS everywhere
    Implement PKCE for public clients
    Store tokens securely (httpOnly cookies for web apps)
    Validate tokens properly (signature, expiration, issuer)
    Implement token rotation and revocation

    Interview Questions

    Amazon
    Google
    Meta
    Microsoft
    Netflix
    Uber
    Twitter
    Microsoft
    Apple
    Salesforce

    Love this visualization?

    Sign up to track your progress, earn achievements, use the AI assistant, and access your personal learning dashboard.

    100% free • No credit card required