Skip to content

OAuth2 Authorization Code Flow with PKCE

This walkthrough explains how to complete the authorization code grant against the OAuth2 authorization server. That grant is the sole OAuth2 flow the server accepts. Apps that cannot hold a secret — desktop, mobile, or browser-based clients — are treated as public clients and must apply PKCE. Server-side apps that can protect credentials are confidential clients; for them PKCE is optional, though adding it strengthens the exchange. The walkthrough proceeds from client registration through the PKCE pair, the authorize redirect, the token exchange, refresh handling, metadata discovery, and error responses.

Flow overview

The flow has seven steps, preceded by an optional registration step that applies only to public clients: 1. Public clients register through Dynamic Client Registration (DCR) on first use. 2. Generate a PKCE verifier and challenge, then redirect the user to the authorize endpoint. 3. The user authenticates. 4. The user grants or denies permission. 5. The browser returns to your redirect URI with a code and the state value. 6. Exchange the code for tokens at the token endpoint. 7. Use the access token as a Bearer credential. Confidential clients skip DCR because their client_id and client_secret are issued manually by the authorization server's operators. The exact request and response contracts for each step are provided in the protected code modules referenced below.

Step 0: Register a public client

Desktop apps, CLI tools, and MCP clients register themselves on first use. A single request returns a client_id you can reuse. The request body includes client_name and redirect_uris. The response includes client_id, client_name, redirect_uris, grant_types, response_types, and token_endpoint_auth_method. The exact request is in code0001 and the response shape is in code0002. The full registration contract and rate limits are documented on the Endpoints page under POST /oauth/register.

Confidential clients do not call this endpoint; their credentials are issued manually.

Step 1: Build the PKCE pair and redirect to authorize

Create a code verifier of 43–128 URL-safe random characters. Derive the code challenge by hashing the verifier with SHA-256 and encoding the result in base64url. The example in code0003 generates a verifier from 64 bytes (512 bits) of entropy, which produces an 86-character verifier — above RFC 7636's 256-bit recommendation and below the 128-character cap. The challenge is the base64url-encoded SHA-256 of the verifier.

Store the verifier server-side, or in platform secure storage, keyed by the state value. You will need it during the token exchange. Then redirect the user's browser to the authorize endpoint with the parameters shown in code0004. The parameter table below lists each field, whether it is required, and its purpose.

Authorize request parameters

The authorize request accepts the following parameters. The client_id and redirect_uri must match what you registered. The state value is an opaque CSRF token that you must verify unchanged in the callback. scope defaults to openid if omitted. response_type must be code and defaults to code. code_challenge is required for public clients and optional for confidential clients; code_challenge_method is required whenever code_challenge is present and must be S256.

Step 4: Receive the authorization code

On approval, the browser is redirected to your redirect_uri with a code and the state value you sent. The callback URL looks like the example in code0005. Verify that the returned state matches the value from Step 1. The authorization code is single-use and expires after 10 minutes.

Step 5: Exchange the code for tokens

Retrieve the code_verifier you stored in Step 1 and POST to /oauth/token. Public clients send client_id and code_verifier without a secret, as shown in code0006. Confidential clients authenticate with client_secret, as shown in code0007. If Step 1 included a code_challenge, code_verifier is required regardless of client type.

Step 6: Receive tokens

A successful exchange returns access_token, id_token, refresh_token, token_type, and expires_in, as shown in code0008. Both token fields hold the same OpenID Connect ID Token, so either value works as the Bearer credential. The duplication is deliberate: OAuth clients that insist on an aud claim inside the access token can validate it without any custom logic. Access tokens remain valid for 60 minutes. Refresh tokens remain valid for 365 days and can be redeemed through the refresh_token grant to obtain a new access token.

Step 7: Use the access token

Send the access token as a Bearer credential in the Authorization header. The example in code0009 calls /oauth/me.

Refreshing tokens

After the access token expires, redeem the refresh token to obtain a replacement access token. Confidential clients prove their identity by adding client_secret or by using Basic authentication; public clients identify themselves with client_id alone. The request is shown in code0010. Because refresh tokens are not rotated, the response returns the same refresh token you submitted.

Metadata discovery

If your OAuth client library supports RFC 8414, point it at the metadata endpoint shown in code0011. The response advertises every endpoint and capability, including supported grant types, PKCE methods, and registration_endpoint, so the library can configure itself without hard-coded URLs.

Error handling

Both the authorize and token endpoints return errors as JSON. The example in code0012 shows an invalid_grant error with a description that the code verifier did not match the challenge. The Endpoints page lists the complete set of error codes per endpoint.

A mismatch between the verifier and challenge is distinct from a state mismatch or an expired code. Verify state before exchanging the code, and treat a single-use code that is older than 10 minutes as expired rather than retrying the exchange.

Authorize request parameters
ParameterRequiredDescription
client_idYesYour registered OAuth client ID (from DCR or manual registration)
redirect_uriYesMust exactly match a registered redirect URI
stateYesOpaque CSRF token. Verify unchanged in the callback.
scopeNoSpace-separated. Defaults to openid .
response_typeNoMust be code . Defaults to code .
code_challengeRequired for public clients, optional for confidentialBase64url-encoded SHA-256 hash of your verifier
code_challenge_methodRequired if code_challenge is presentMust be S256

This table reflects the captured parameter contract. Field names and requiredness are exact.

Frequently asked questions

Which OAuth2 flow does the authorization server support?

The authorization code grant is the only supported OAuth2 flow. It works for public clients and confidential clients. PKCE is required for public clients and optional but recommended for confidential clients.

How long should the PKCE code verifier be?

The verifier must be 43–128 URL-safe random characters. The example generates 64 bytes of entropy, which produces an 86-character verifier — above RFC 7636's 256-bit recommendation and below the 128-character cap.

How long does the authorization code last?

The authorization code is single-use and expires after 10 minutes.

How long do the access and refresh tokens last?

The access token has a 60-minute lifetime. The refresh token has a 365-day lifetime and is not rotated when used.

Is code_verifier required for confidential clients?

code_verifier is required if Step 1 included a code_challenge, regardless of client type. Confidential clients may omit PKCE entirely, but if they send a challenge they must send the matching verifier.

curl -X POST {{AUTH_BASE_URL}}/oauth/register \
  -H "Content-Type: application/json" \
  -d '{
    "client_name": "My Desktop App",
    "redirect_uris": ["http://127.0.0.1:53126/callback"]
  }'
{
  "client_id": "6a8cd456-...",
  "client_name": "My Desktop App",
  "redirect_uris": ["http://127.0.0.1:53126/callback"],
  "grant_types": ["authorization_code", "refresh_token"],
  "response_types": ["code"],
  "token_endpoint_auth_method": "none"
}
GET {{AUTH_BASE_URL}}/oauth/authorize
  ?client_id=your-client-id
  &redirect_uri=https://yourapp.com/callback
  &state=random-csrf-token
  &scope=openid email profile
  &response_type=code
  &code_challenge=<sha256-base64url>
  &code_challenge_method=S256
curl -X POST {{AUTH_BASE_URL}}/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=authorization_code" \
  -d "code=a1b2c3d4-..." \
  -d "redirect_uri=https://yourapp.com/callback" \
  -d "client_id=your-client-id" \
  -d "code_verifier=<your-stored-verifier>"
curl -X POST {{AUTH_BASE_URL}}/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -u "your-client-id:your-client-secret" \
  -d "grant_type=authorization_code" \
  -d "code=a1b2c3d4-..." \
  -d "redirect_uri=https://yourapp.com/callback" \
  -d "code_verifier=<your-stored-verifier>"
{
  "access_token": "eyJ...",
  "id_token": "eyJ...",
  "refresh_token": "eyJ...",
  "token_type": "Bearer",
  "expires_in": 3600
}
curl {{AUTH_BASE_URL}}/oauth/me \
  -H "Authorization: Bearer eyJ..."
curl -X POST {{AUTH_BASE_URL}}/oauth/token \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=refresh_token" \
  -d "refresh_token=eyJ..." \
  -d "client_id=your-client-id"
{{AUTH_BASE_URL}}/.well-known/oauth-authorization-server
{
  "error": "invalid_grant",
  "error_description": "Code verifier did not match the challenge"
}