Library / Documentation

OAuth 2.0 Overview

The canonical guide to OAuth 2.0 grants, tokens and security considerations.

IntermediateOAuthAPI Gateway

By OAuth Community · oauth.net · Added 2026-08-30

Open the docs

All content belongs to OAuth Community / oauth.net. We only link to it and give credit. Report a problem

Learn this doc

About 30 min left. Your work is saved as you go.

What you want from these docs

oauth.net documents a tool you may use. Decide what you want to do with it, then read only what answers that.

Ideas you will meet

OAuth
Delegated authorization flows for APIs and apps.
API Gateway
A single entry point for routing, auth and throttling.

What we already know about this

Our own lessons on these ideas. Read them here before you start, so it makes sense.

  • Core Concepts: How Things Actually Work · 9 minSSO, OAuth 2.0 and OpenID ConnectHow 'Sign in with Google' works, and the difference between authentication and authorisation.Read here

    Two questions, two words

    • Authentication: who are you?
    • Authorisation: what are you allowed to do?

    OAuth 2.0: delegated access

    OAuth 2.0 lets a user give an app limited access to their data at another service, without sharing their password. Roles:

    • Resource owner: the user.
    • Client: the app asking for access.
    • Authorization server: issues tokens (for example Google, GitHub).
    • Resource server: the API holding the data.

    The user grants specific scopes (for example "read my calendar"), and the client receives an access token that is limited to those scopes and expires.

    The authorization code flow

    Figure SDA-CORE-07-1 · Authorization code flow · © forcsguy.com

    Important details:

    • state: a random value you check on return, which stops attackers forging the callback (CSRF).
    • PKCE: a one-time proof that the party exchanging the code is the one that started the flow. Use it for all clients, essential for mobile and single-page apps.
    • Register exact redirect URIs and compare them strictly.

    OpenID Connect: adding identity

    OAuth alone says "this token may call the API", not "this is Jane". OpenID Connect (OIDC) adds an ID token (a signed JWT) with claims about the user: subject id, email, name. To sign someone in, validate the ID token's signature, issuer, audience and expiry.

    SSO

    With single sign-on, many applications trust one identity provider. The user signs in once; the provider vouches for them to each app. Enterprises use SAML or OIDC for this. Benefits: fewer passwords, central policies like MFA, and one place to revoke access when someone leaves.

    Single sign-on means the second application never sees your password. It trusts the sign-in service you already used:

    Figure SDA-CORE-07-2 · One sign-in, two applications · © forcsguy.com

    Common mistakes

    • Using the access token as proof of identity instead of validating an ID token.
    • Skipping state or accepting any redirect URI.
    • Storing tokens somewhere scripts can read them.
    • Requesting more scopes than the feature needs.

    More on this site

    Open this lesson with its quiz and exercise

  • Advanced Distributed Systems · 12 minLoad balancing: layer 4, layer 7 and gatewaysWhat a load balancer can see and do at each layer, the algorithms behind it, and how API gateways add routing, aggregation and policy.Read here

    What a load balancer sees

    A load balancer spreads incoming work across servers, removes failed servers and makes scaling invisible to clients. What it can do depends on which layer it works at.

    Layer 4 (transport)Layer 7 (application)
    SeesIP addresses, ports, TCP/UDP connectionsHTTP method, URL, headers, cookies, body
    Decides byConnection detailsRequest content
    TLSPasses through, or terminates in some productsUsually terminates TLS
    Speed and costVery fast, low overheadMore work per request
    Typical featuresSpread connections, health checksPath routing, header rules, sticky sessions, rewrites, caching, WAF rules
    ExamplesNetwork load balancers, LVS, HAProxy in TCP modeNGINX, Envoy, HAProxy HTTP mode, cloud application balancers

    Figure SDA-ADS-05-1 · Layers of balancing in front of an app · © forcsguy.com

    Many designs use both: an L4 tier for raw scale and an L7 tier for smart routing.

    Before: layer 4: ports and addresses

    After: layer 7: reads the request

    What changed

    • Layer 7 can route by path, header or cookie
    • Layer 7 can terminate TLS and retry safely
    • Layer 4 is simpler and faster, with less to configure

    Choosing a target: algorithms

    • Round robin / weighted round robin: simple rotation; weights favour bigger machines.
    • Least connections (and weighted): send to the server with fewest active requests; good when requests vary in length.
    • Least response time: combine active load with measured latency.
    • IP hash / consistent hashing: the same client or key goes to the same server, useful for caches and sticky state (see consistent hashing).
    • Resource-based: servers report load and the balancer follows.

    Health checks (active probes and passive error tracking) remove unhealthy servers; connection draining lets in-flight requests finish before a server is removed during a deploy.

    Global and DNS balancing

    Across regions, DNS-based or global server load balancing directs users to the nearest or healthiest region (see the DNS and CDN lessons). DNS answers are cached, so failover takes as long as the TTL; anycast and global balancers react faster.

    API gateway: policy at the edge

    An API gateway is an L7 component specialised for APIs. Beyond routing it commonly provides:

    • Authentication and authorisation (validate tokens, apply scopes),
    • Rate limiting and quotas (see the rate-limiter lesson),
    • Request and response transformation, versioning, and protocol translation,
    • Aggregation: one client call fans out to several services and merges results, saving round trips for mobile clients,
    • Caching, logging, metrics and tracing in one place.

    Figure SDA-ADS-05-2 · Gateway routing and aggregation · © forcsguy.com

    Risks: the gateway is a single entry point (run several instances, keep it stateless) and can become a bottleneck or a place where business logic piles up. Keep it to cross-cutting concerns. A variation is a backend for frontend: one gateway per client type (web, mobile) tailored to its needs.

    Inside the cluster

    Between services, balancing is done by client-side libraries or sidecar proxies that know the healthy instances from service discovery, so no central balancer is needed for east-west traffic. See the microservices lessons for service mesh ideas.

    Failure habits

    • Run at least two of every balancer and gateway instance across zones.
    • Keep timeouts and retry budgets (see the resilience lesson) so the balancer does not amplify an outage.
    • Use slow start for new instances so they warm up before taking a full share.

    Real-world reading

    Open this lesson with its quiz and exercise

Go deeper on this

A path through videos, articles, other case studies and code on the same ideas as OAuth 2.0 Overview.

  1. Warm upGet the vocabulary first.

  2. WatchHow others solve the same problem, on video.

    Videos belong to their creators and play from YouTube. More on YouTube (opens in a new tab)

  3. ReadEngineers explain it in their own words.

  4. See it in another companyThe same ideas, a different system.

  5. Read the codeSee how it looks when it is real.

Keep learning

Loading community…