In short
OAuth 2.0 lets an app act on a user’s behalf at an API without seeing the user’s password: the app gets an access token from an authorization server and presents it to the API. OpenID Connect adds a sign-in layer on top, an ID token that tells the app who the user is. For almost every app with a user, the flow to use is the authorization code flow with PKCE; machines use client credentials, and devices without a browser use the device authorization grant.
Key points
- OAuth answers “may this app call this API for this user?”. OpenID Connect answers “who is this user?”. Do not use an access token as proof of identity.
- Use the authorization code flow with PKCE for web, single-page and mobile apps. RFC 9700 says the implicit grant should not be used and the password grant must not be.
- Access tokens are for the API, ID tokens are for the client, and refresh tokens are only ever sent to the authorization server.
- Validate a JWT with a library and an allow-list of algorithms, then check
iss,aud,expand, for ID tokens,nonce. Never let the token’s header choose the algorithm, and never acceptnone. - Public clients must rotate or sender-constrain refresh tokens. In browsers, a backend-for-frontend that keeps tokens server-side exposes the least.
- Logout has three layers: the app’s session, the provider’s session and the tokens. Clearing a cookie handles only the first.
OAuth is delegated authorization; OpenID Connect adds sign-in
OAuth 2.0, defined in RFC 6749, solves one problem: letting an application access a protected resource on a user’s behalf, with the user’s permission, without the user handing the application a password. The application gets an access token, a credential scoped to what the user allowed, and presents it to the API. The token says the app may do certain things. It does not, by itself, say who the user is.
OpenID Connect Core 1.0 calls itself “a simple identity layer on top of the OAuth 2.0 protocol.” A client asks for it by including the openid scope, and the authorization server, now called the OpenID Provider, returns an ID token alongside the access token. The ID token is a signed JWT about the sign-in: which issuer authenticated the user, the user’s stable identifier (sub), which client it was issued to, and when. OIDC also defines a UserInfo endpoint that returns profile claims when called with the access token, and discovery and logout specs around them.
The common mistake is “signing in with OAuth” by accepting any access token the app receives as proof of who the user is. An access token is meant for an API, and an app that accepts one minted for a different client can be fooled into logging in the wrong person. If you need identity, use OIDC and validate the ID token. How the user proves who they are at the provider, with a password, a one-time code or a passkey, is the provider’s business; your app receives the result.
The four roles, and public versus confidential clients
- Resource owner: in RFC 6749’s words, an entity capable of granting access to a protected resource. Usually the user.
- Client: the application making requests on the resource owner’s behalf and with their authorization. A web app, a mobile app, a CLI, another service.
- Authorization server: the server that authenticates the resource owner, gets their consent and issues tokens. In OIDC it is the OpenID Provider.
- Resource server: the API that hosts the protected resources and accepts access tokens.
RFC 6749 also splits clients by whether they can keep a secret. A confidential client runs on a server you control and can authenticate to the token endpoint with a secret or a key. A public client cannot: a single-page app ships its code to every browser, and a mobile app can be unpacked. RFC 8252 says plainly that native apps are public clients and that a secret compiled into an app should not be treated as confidential. That distinction decides which flows and protections apply below.
Why the implicit and password grants are deprecated
RFC 9700, the OAuth 2.0 Security Best Current Practice published in January 2025, updates RFC 6749 with what has been learned from attacks since it was published.
- Implicit grant (
response_type=token). It returned the access token straight in the redirect URL, where it could leak through browser history, logs and referrers, could be injected by an attacker, and could not be bound to the client. RFC 9700 says clients should not use it unless those risks are mitigated, and in practice the code flow with PKCE replaces it, including in single-page apps. - Resource owner password credentials grant. The app collects the user’s username and password and trades them for a token. RFC 9700 says it must not be used: it exposes the credentials to the client, it was not designed for multi-factor or other sign-in steps, and it teaches users to type their password into any app that asks.
The same document makes PKCE mandatory for public clients and recommended for confidential ones, requires authorization servers to support it, and asks that access tokens be limited to the privileges and the audience they need.
Access tokens, ID tokens and refresh tokens
| Token | Who it is for | Format | Where it is sent |
|---|---|---|---|
| Access token | The resource server, the API | Opaque string or a JWT; RFC 6749 says it is usually opaque to the client | To the API, typically as Authorization: Bearer |
| ID token | The client, to learn who signed in | Always a signed JWT with iss, sub, aud, exp and iat | Nowhere; the client reads it. Optionally back to the provider as a logout hint |
| Refresh token | The authorization server | Usually opaque | Only to the token endpoint; RFC 6749 says never to resource servers |
Access tokens are kept short-lived so that a leaked one expires quickly. The refresh token lets the client get a new access token without sending the user through the login again, which makes it the most valuable token to steal. RFC 9700 says that refresh tokens issued to public clients must be either sender-constrained, cryptographically bound to the client instance with mutual TLS or DPoP, or rotated.
Rotation works like this: every refresh returns a new refresh token and invalidates the old one, while the server remembers which tokens belong to the same grant. If an attacker and the real client both hold a copy, one of them will eventually present a token that was already used. The server cannot tell which party is the attacker, so it revokes the active refresh token for that grant, and the real user signs in again. For browser apps, RFC 10017 adds that the server must also cap refresh token lifetime or expire unused ones, and should tie the lifetime to the user’s session.
JWT structure and validation
A JWT (RFC 7519) is three base64url-encoded parts joined by dots: a header naming the algorithm and key, a payload of claims, and a signature over the first two. Anyone can decode the payload, so it is not a place for secrets. The registered claims are iss (issuer), sub (subject), aud (audience), exp (expires), nbf (not before), iat (issued at) and jti (token ID).
Validation has two halves. The cryptographic half belongs to a maintained JOSE library: fetch the issuer’s published signing keys, select the key by its kid, and verify the signature with an algorithm from your own allow-list. RFC 7519 defines an unsigned JWT with alg set to none and an empty signature, and RFC 8725, the JWT best current practices, says libraries must let callers fix the allowed algorithms and should not accept none unless explicitly asked. It describes attacks that rewrite the header to none, or switch an RSA algorithm to an HMAC one keyed with the public key. Hand-rolled verification is where those bugs live. The claim half is application logic, and the function below shows it around a payload the library has already verified.
type Claims = { iss?: string; aud?: string | string[]; exp?: number; nbf?: number; iat?: number; nonce?: string };
type Expected = { issuer: string; audience: string; nonce?: string; leeway?: number; now?: number };
// `payload` must come from a JOSE library that has ALREADY verified the signature
// with the issuer's published keys and an allow-list such as ['RS256', 'ES256'].
// Never pick the algorithm from the token's own header; never accept 'none'.
export function checkClaims(payload: Claims, want: Expected): string[] {
const now = want.now ?? Math.floor(Date.now() / 1000);
const leeway = want.leeway ?? 60; // seconds of clock skew, kept small
const aud = Array.isArray(payload.aud) ? payload.aud : payload.aud ? [payload.aud] : [];
const problems: string[] = [];
if (payload.iss !== want.issuer) problems.push('iss is not the expected issuer');
if (!aud.includes(want.audience)) problems.push('aud does not name this client or API');
if (typeof payload.exp !== 'number') problems.push('exp is missing');
else if (now > payload.exp + leeway) problems.push('token has expired');
if (typeof payload.nbf === 'number' && now + leeway < payload.nbf) problems.push('token is not valid yet');
if (typeof payload.iat === 'number' && payload.iat > now + leeway) problems.push('iat is in the future');
if (want.nonce && payload.nonce !== want.nonce) problems.push('nonce does not match the login request');
return problems; // empty means every claim check passed
}issmust match the expected issuer exactly, and OIDC Core requires the same for ID tokens. RFC 8725 adds that the signing key must belong to that issuer.audmust name you. RFC 7519 says a recipient that does not find itself in the audience must reject the token. This is what stops a token minted for another app or API being replayed at yours.expmust be in the future. RFC 7519 allows a small leeway for clock skew, usually no more than a few minutes.- For ID tokens,
noncemust equal the value the client sent in step 2, which defeats replay of an old ID token. - Where one issuer mints several kinds of JWT, RFC 8725 recommends explicit typing in the
typheader, so an ID token cannot pass as an access token.
Client credentials, the device flow, and which flow to use
Client credentials is OAuth without a user. A backend service, a scheduled job or a worker authenticates as itself at the token endpoint and receives an access token for the scopes it is allowed. RFC 6749 says this grant must only be used by confidential clients, and that a refresh token should not be included: the client can simply ask again. This is the usual answer for machine-to-machine calls between your own services and for many partner APIs.
The device authorization grant, RFC 8628, is for devices that have no browser or no easy way to type: TVs, consoles, CLIs on a remote server. The device posts to the device authorization endpoint and receives a device_code, a short user_code, a verification_uri and an interval. It shows the user the code and the address, the user opens it on a phone or laptop and approves, and meanwhile the device polls the token endpoint with grant_type=urn:ietf:params:oauth:grant-type:device_code. The answer authorization_pending means keep polling; slow_down means add 5 seconds to the interval for this and every later request; the default interval is 5 seconds.
| Flow | Use it for | Client type | Notes |
|---|---|---|---|
| Authorization code with PKCE | Any app where a user signs in: server-rendered web, single-page, mobile, desktop | Public or confidential | The default. PKCE is required for public clients and recommended for all |
| Client credentials | Service-to-service calls, jobs, workers | Confidential only | No user and no refresh token; scope it narrowly |
| Device authorization grant | TVs, consoles, CLIs and devices without a usable browser | Usually public | The user approves on a second device while the device polls |
| Refresh token grant | Renewing access tokens without a new sign-in | Any client that was issued one | Rotate or sender-constrain for public clients |
| Implicit | Nothing new | Was used by single-page apps | RFC 9700: should not be used; use code with PKCE |
| Resource owner password | Nothing | Was used by first-party apps | RFC 9700: must not be used |
Remote MCP servers are a recent example of the same building blocks: the protocol’s authorization rules build on the code flow with PKCE, covered in MCP server transports and auth.
Storing tokens in browsers and mobile apps
The IETF guidance for browser apps was a long-running draft, “OAuth 2.0 for Browser-Based Applications”, published in August 2026 as RFC 10017. It describes three architectures, from least to most exposed:
- Backend for frontend (BFF). A small server of your own is the confidential OAuth client. It runs the code flow, keeps the tokens server-side and gives the browser only a session cookie; API calls go through the BFF, which attaches the access token. The RFC strongly recommends it for business applications, sensitive applications and applications that handle personal data. Its cookies must be
HttpOnlyand HTTPS-only, and should beSameSite=Strict. - Token-mediating backend. The backend still does the token exchange and holds the refresh token, but hands the access token to the browser so it can call APIs directly. Simpler for cross-origin APIs, but the access token is now in reach of any script on the page.
- Browser-only client. The single-page app is a public client and runs code with PKCE itself. The RFC says this pattern is not recommended for business applications, sensitive applications or applications that handle personal data, because injected script can not only read tokens but also run a fresh flow and get new ones.
On storage inside the browser, the RFC is blunt: localStorage does not protect against malicious JavaScript, sessionStorage slightly reduces exposure by being per tab, in-memory storage and workers can isolate tokens better, and none of these options can fully stop token theft once an attacker can run code in the page. That is why the defenses that matter most are preventing script injection in the first place, covered in the web app security checklist, and keeping tokens out of the browser with a BFF.
Native apps follow RFC 8252. They must send the user to an external user-agent, the system browser or an in-app browser tab, and must not use an embedded web view for the login, because the host app could read what the user types. They must use PKCE. Redirects come back through a claimed https link, which the RFC prefers, a private-use URI scheme, or a loopback address on desktop. Tokens then belong in the platform’s protected storage, such as the iOS Keychain or the Android Keystore, rather than in plain files or preferences.
Scopes, consent and logout
Scopes are the permissions a client asks for, such as openid profile email or orders:read, and what the user sees on the consent screen. Ask for the least that works and ask for more when a feature needs it, not up front. RFC 9700 asks that access tokens carry only the privileges and audience needed. Scopes limit what an app may do on a user’s behalf; they do not replace your own authorization rules about what that user may do, which is where role-based access control comes in. The API should check both.
Logout is three separate things. Clearing the app’s session cookie logs the user out of your app, but the provider still has its own session, so the next login may happen silently. To end that too, OpenID Connect RP-Initiated Logout 1.0 has the app redirect the browser to the provider’s end_session_endpoint with an id_token_hint, and optionally a post_logout_redirect_uri, which must exactly match one registered in advance, and a state. Front-Channel and Back-Channel Logout let the provider tell other apps that the user signed out. Finally, revoke the refresh token at the provider so a copy cannot outlive the session; access tokens that are already issued stay valid until they expire, which is one more reason to keep them short.
BYD Tech builds web apps with sign-in, roles and APIs: the code lives in the client’s GitHub from day one, the founders are on the calls, and AI-assisted work is reviewed by an engineer.
Sources
Checked when this post was written.
- RFC 6749: The OAuth 2.0 Authorization Framework
- OpenID Connect Core 1.0 incorporating errata set 2
- RFC 7636: Proof Key for Code Exchange by OAuth Public Clients (PKCE)
- RFC 9700: Best Current Practice for OAuth 2.0 Security
- RFC 7519: JSON Web Token (JWT)
- RFC 8725: JSON Web Token Best Current Practices
- RFC 8628: OAuth 2.0 Device Authorization Grant
- RFC 10017: OAuth 2.0 for Browser-Based Applications (formerly draft-ietf-oauth-browser-based-apps)
- RFC 8252: OAuth 2.0 for Native Apps
- OpenID Connect RP-Initiated Logout 1.0