Sequence diagram of the OAuth 2.0 authorization code flow with PKCE between the user, browser, your app, the authorization server and the resource server: create state and code verifier, redirect to authorize, sign in and consent, redirect back with the code, check state, exchange the code and verifier for tokens, then call the API with the access token.
Diagram Source — edit it live in the browser
title OAuth 2.0 Authorization Code Flow with PKCE

actor User
participant Browser
participant "Your App" as App
participant "Authorization Server" as AS
participant "Resource Server" as RS

User->Browser: Click "Log in"
Browser->App: GET /login
App->App: Create state and code_verifier\ncode_challenge = BASE64URL(SHA-256(code_verifier))
App-->Browser: 302 redirect to /authorize
Browser->AS: GET /authorize?response_type=code&client_id=...\n&redirect_uri=...&scope=...&state=...\n&code_challenge=...&code_challenge_method=S256
AS-->Browser: Login and consent page
User->AS: Sign in and approve
AS-->Browser: 302 to redirect_uri?code=...&state=...
Browser->App: GET /callback?code=...&state=...
App->App: Check state matches
App->AS: POST /token: code, redirect_uri,\ncode_verifier (+ client authentication)
AS->AS: Check code_verifier against code_challenge
AS-->App: Access token (+ refresh token)
App->RS: GET /api/... with Authorization: Bearer access_token
RS-->App: Protected data
App-->Browser: Signed in (session cookie)

What this diagram shows

The diagram has two distinct legs. In the first, everything goes through the browser — the redirect to the provider, the login and consent screen, the redirect back with the auth code. In the second, the code is exchanged for a token server-to-server, away from the browser entirely.

That separation is the security model. An authorization code intercepted from the browser redirect is useless on its own: redeeming it takes the code_verifier that only your app holds (and, for a server-side app, its client credentials). The access token never reaches the browser, so it never ends up in history or logs. The diagram makes that boundary obvious in a way code alone doesn't.

Step-by-step flow breakdown

1
User starts the loginThe user clicks "Log in". Your app generates a random state value and a random code_verifier, and derives the code_challenge from it: the SHA-256 hash, base64url-encoded. It keeps both secrets on its side.
2
Browser is sent to the authorization serverYour app redirects the browser to the /authorize endpoint with response_type=code, client_id, redirect_uri, scope, state, code_challenge and code_challenge_method=S256. Only the hash travels through the browser, never the verifier.
3
User signs in and consentsThe authorization server shows its own login page and a consent screen listing the requested scopes. Your app never sees the password.
4
Authorization code comes backThe authorization server redirects the browser to your redirect_uri with a short-lived, single-use code and the same state.
5
Your app checks stateIf state doesn't match the value your app stored in step 1, stop. That check is what protects the callback against cross-site request forgery.
6
Code is exchanged for tokensYour app calls the /token endpoint directly, server to server, with the code, the same redirect_uri and the original code_verifier. A confidential client also authenticates itself, for example with its client secret.
7
Authorization server verifies and issues tokensIt hashes the code_verifier and compares it with the code_challenge from step 2. If they match, it returns an access token, and often a refresh token.
8
App calls the API and signs the user inYour app sends the access token as a Bearer token to the resource server, gets the protected data, and starts its own session for the user.

When to use an OAuth sequence diagram

Why PKCE is part of the main flow

PKCE (Proof Key for Code Exchange, RFC 7636) ties the token request to the browser session that started it. The redirect only carries a hash of the verifier, so someone who steals the authorization code still can't redeem it. RFC 9700, the current OAuth security best practice, says public clients such as single-page and mobile apps must use PKCE, recommends it for server-side apps too, and says clients should no longer use the implicit grant.

OpenID Connect variant

"Sign in with Google" and many other social logins are OpenID Connect, which runs the same code flow with openid in the scope. Two things change: your app should also send a random nonce, and the token response includes an ID token, a signed JWT that says who the user is. Before trusting it, your app checks the signature against the provider's published keys, that iss is the provider, that aud is your client ID, that exp hasn't passed, and that nonce matches.

OpenID Connect sign-in — source
title OpenID Connect Sign-in (Code Flow + PKCE)

actor User
participant Browser
participant "Your App" as App
participant "OpenID Provider" as OP

User->Browser: Click "Sign in"
Browser->App: GET /login
App->App: Create state, nonce and code_verifier
App-->Browser: 302 redirect to /authorize
Browser->OP: GET /authorize?response_type=code&client_id=...\n&redirect_uri=...&scope=openid email&state=...\n&nonce=...&code_challenge=...&code_challenge_method=S256
OP-->Browser: Login and consent page
User->OP: Sign in and approve
OP-->Browser: 302 to redirect_uri?code=...&state=...
Browser->App: GET /callback?code=...&state=...
App->App: Check state matches
App->OP: POST /token: code, redirect_uri,\ncode_verifier (+ client authentication)
OP-->App: id_token + access token
App->App: Validate id_token: signature, iss,\naud = client_id, exp, nonce
App-->Browser: Signed in as the id_token's subject

Common mistakes this diagram helps catch

Refreshing an expired access token

Access tokens are short-lived. When one expires, your app sends the refresh token to /token with grant_type=refresh_token and gets a new access token, without sending the user through the login again. It's worth drawing as its own short sequence.

Related sequence diagram examples

Frequently asked questions

How does the OAuth 2.0 authorization code flow work?

Your app redirects the user to the authorization server with a state and a PKCE code_challenge → the user logs in and grants consent → the server redirects back with a one-time authorization code → your app checks state and exchanges the code plus the code_verifier for an access token, server to server → your app uses the access token to call APIs on the user's behalf.

What is the difference between OAuth 2.0 and OpenID Connect?

OAuth 2.0 is an authorization framework — it grants apps access to resources. OpenID Connect (OIDC) adds an identity layer: it returns an ID token (JWT) with user profile claims, enabling authentication (login) not just resource access.

Why is the authorization code exchanged server-side?

The exchange needs the code_verifier, and for a server-side app its client credentials, neither of which should be exposed in the browser. Doing it server to server also keeps the access token out of browser history and front-end logs.

Can I edit this OAuth 2.0 diagram?

Yes. Click Open in Editor on either diagram, rename the participants for your provider (GitHub, Auth0, Okta, Microsoft Entra ID), or add a refresh-token step. Export as PNG or SVG, or as Mermaid or PlantUML; here's how the two formats compare. Free, no account required.

Document your own OAuth flow

Type your auth steps, get a live diagram. Export PNG, SVG, or Mermaid. Free — no account needed.

Open Editor Free →