Skip to main content

OAuth 2.0 Course for Beginners: Build Authorization Servers, Resource Servers, and Client Apps

freeCodeCamp.orgJanuary 6, 20261h 30min43,658 views
43 connections·40 entities in this video→

Understanding OAuth 2.0

  • πŸ’‘ OAuth 2.0 is an industry-standard framework for delegated authorization, allowing users to grant limited access to their data without sharing passwords.
  • πŸ”‘ It uses temporary access tokens for secure, limited access across web, mobile, and desktop applications, enabling features like "Sign in with Google" or connecting fitness trackers to health data.
  • 🎭 The core roles in OAuth 2.0 are the Resource Owner (the user), the Client (the application requesting access), the Authorization Server (issues tokens), and the Resource Server (protects the API and data).

Key OAuth 2.0 Concepts and Flows

  • πŸš— The valet key system analogy simplifies OAuth 2.0: users grant apps permission to use their data (like a car) for specific purposes, receiving a short-lived token instead of their password.
  • πŸ”’ PKCE (Proof Key for Code Exchange) is a crucial security enhancement for public clients (like mobile apps) that cannot securely store secrets, protecting the authorization code flow from interception.
  • πŸ“ Tokens in OAuth 2.0 include authorization codes (temporary receipts), access tokens (short-lived, for API calls), and refresh tokens (longer-lived, to obtain new access tokens).

Building the Authorization Server

  • βš™οΈ The authorization server is responsible for issuing tokens, simulating user logins, creating authorization codes, and publishing public keys (JWKS) for resource servers to verify tokens.
  • πŸ’Ύ Demo data stores (clients, authorization codes, refresh tokens) are managed using Map objects, with a registered demo client and its redirect URI.
  • πŸ”‘ JWT signing requires a private key (PKCS8 format) and an issuer URL, with a key ID for identification. The server publishes public keys via a JWKS endpoint.

Developing the Resource Server

  • πŸ›‘οΈ The resource server acts as a protected API, validating incoming bearer tokens by fetching JWKS from the authorization server and verifying the token's signature, issuer, audience, and expiry.
  • 🚫 It never trusts the client directly; trust is placed solely on valid tokens signed by the authorization server.
  • πŸŽ›οΈ Middleware functions like requireAuthorization and requireScope are used to enforce token validation and permission checks before allowing access to API endpoints.

Implementing the Client Application

  • πŸ’» The client app initiates the OAuth flow, sending the user to the authorization server's /authorize endpoint and handling the callback at /callback.
  • πŸ”„ It exchanges the received authorization code and code verifier (via PKCE) for access and refresh tokens at the /token endpoint.
  • πŸš€ The client then uses the access token in the Authorization: Bearer header to make requests to the protected resource server API.

Running and Debugging the System

  • πŸš€ A full system requires running three independent servers: the Authorization Server (port 3000), the Resource Server (port 5000), and the Client App (port 4000).
  • πŸ› οΈ Common errors include JWKS endpoint issues (malformed keys, incorrect format) and Axios 400 errors during token exchange, often due to lost code verifier cookies or incorrect client configurations.
  • βœ… Best practices emphasize validating tokens on the resource server, keeping access tokens short-lived, using refresh tokens securely, implementing PKCE for public clients, and using HTTPS in production.
Knowledge graph40 entities Β· 43 connections

How they connect

An interactive map of every person, idea, and reference from this conversation. Hover to trace connections, click to explore.

Hover Β· drag to explore
40 entities
Chapters20 moments

Key Moments

Transcript322 segments

Full Transcript

Topics13 themes

What’s Discussed

OAuth 2.0Authorization ServerResource ServerClient AppAccess TokensRefresh TokensPKCEAuthorization Code FlowJWTJWKSBearer TokensAPI SecurityDelegated Authorization
Smart Objects40 Β· 43 links
ConceptsΒ· 23
ProductsΒ· 9
CompaniesΒ· 4
LocationsΒ· 2
PersonΒ· 1
EventΒ· 1