Before OAuth, an app that wanted your calendar asked for your email password and stored it. OAuth 2.0 replaced that pattern. The user signs in at the service that owns the data and approves a specific request ("read your calendar"). The app receives an access token that works only for that scope, expires quickly and can be revoked without changing the password. OAuth 2.0 is defined in RFC 6749 (2012). RFC 9700 (January 2025) is the current security best practice. OAuth 2.1, which folds that advice into one document, was still an IETF Internet-Draft in November 2026.
The four roles
- Resource owner: the user who owns the data.
- Client: the application asking for access (a web app, mobile app or backend job).
- Authorization server: issues tokens after the user approves (Auth0, Keycloak, Cognito, Google, Microsoft Entra ID).
- Resource server: the API that accepts the token.
How the authorization code flow works
This is the flow to use for web, mobile and single-page apps, always with PKCE (RFC 7636).
- The client creates a random
code_verifierand sends its hash (code_challenge) when it redirects the user to the authorization server. - The user signs in and approves the requested scopes.
- The authorization server redirects back with a one-time authorization code.
- The client exchanges the code plus the original
code_verifierfor an access token, and often a refresh token. - The client calls the API with
Authorization: Bearer <token>. PKCE means a stolen code is useless without the verifier. RFC 9700 says not to use the implicit grant or the resource owner password grant. For machine-to-machine calls with no user, use the client credentials grant.
Example: an accounting SaaS reading invoices
A spend-management app needs read-only access to a customer's accounting system. It redirects the finance manager with:
GET /authorize?response_type=code&client_id=spend-app
&redirect_uri=https://spend-app.example/callback
&scope=invoices.read&state=x7Fq2
&code_challenge=E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM
&code_challenge_method=S256
The manager approves "read invoices". The app exchanges the code for a token valid for one hour plus a refresh token, which it stores server-side in a secrets manager or encrypted database column. It can't create payments, because it never asked for payments.write. When the customer disconnects the integration, the refresh token is revoked and access stops.
OAuth vs OIDC
OAuth answers "what may this app access?" It does not tell the app who the user is. OpenID Connect adds that layer on top: the client asks for the openid scope and receives an ID token describing the user. Use OIDC for "Sign in with Google", and plain OAuth for API access.
OAuth vs JWT
They aren't alternatives. JWT (RFC 7519) is a token format. OAuth is the protocol for issuing tokens. Many authorization servers issue access tokens as JWTs, and others use opaque strings that the API checks with the server.
Common mistakes
- Requesting broad scopes "just in case". Ask for the least privilege the feature needs.
- Skipping the
stateparameter or exact redirect URI matching, which opens the door to CSRF and code theft. - Keeping long-lived access tokens in browser local storage.
- Treating an access token as proof of login. Use OIDC for that. Choosing an authorization server is often the bigger decision. See Clerk vs Auth0 vs Cognito vs Keycloak.