All tutorials

OAuth 2.0 + OpenID Connect

Authorization Code + PKCE for a backend web application, step by step

Authorization codeAccess tokenID tokenRefresh tokenResource ownerUser approving accessWeb browserDisplays pagesApplication backendOAuth clientAuthorization serverSign-in serviceResource serverProtected API1Click Sign inUser starts sign-in2Prepare sign-inSave transaction data3Browser redirectOpen authorization endpoint4Authorization requestclient_id + redirect_uri + scope5Authentication + consentAuthorization server checks identity6Authorization responseReturn authorization code + state7Check returned stateMatch returned + saved state8Token requestcode + code_verifier to /token9Token responseaccess + ID (+ refresh)10ID token validationCreate application session11Resource requestAuthorization: Bearer <access_token>12API checks + responseToken validity + scope + object access13Refresh token grantgrant_type=refresh_token14Refresh responseNew access + refresh token

Details

Who’s involved

Resource owner

You, approving access to your data.

Application backend

The OAuth client requesting access.

Authorization server

Signs you in and issues tokens.

Resource server

The protected API that enforces access.

Introduction

OAuth two point zero enables a client application to obtain limited access to a protected API.

00:00 / 05:42

Press play to hear the narration, or click any step to jump to it. Keyboard: Space to play or pause, ← → to change steps. Best viewed in landscape on phones.

Security checks in this flow

PKCE verification

Apply S256 to the code_verifier and compare it with the stored code_challenge.

State validation

Match the returned state against the value saved for this authorization attempt.

ID token validation

Check the signature, iss, aud, exp and nonce. Identify the account by issuer + subject.

Resource authorization

Validate the JWT or introspect opaque tokens. Enforce scope and object-level permissions.

This walkthrough shows a web application whose OAuth client runs on its backend. Mobile and command-line apps use the same flow with PKCE, without a client secret. Example values are shortened.

Transcript

  1. Introduction

    OAuth two point zero enables a client application to obtain limited access to a protected API. OpenID Connect, or OIDC, adds authentication, allowing the client to verify who signed in. We will follow the authorization code flow with Proof Key for Code Exchange, commonly called PKCE. In this flow, the user is the resource owner who can approve access. The client application requests that access. The authorization server authenticates the user and issues tokens. The resource server provides the protected API and enforces access to it.

  2. 1. Click Sign in

    The flow begins when the user selects Sign in in the browser.

  3. 2. Prepare sign-in

    The browser sends the request to the application backend. Here, the backend acts as the OAuth client, requesting access for the application. Before redirecting the browser, the backend creates a secret random string called the PKCE code verifier. It keeps that secret and calculates a code challenge from it, a value it can safely send ahead. The backend also saves state, a random marker for matching the returning browser request, and an OIDC nonce, a separate random marker for checking the identity message later. These values belong to this browser’s sign-in attempt.

  4. 3. Browser redirect

    The backend now replies with a redirect, telling the browser to open the authorization server’s sign-in address. The browser will carry the authorization request there. The secret verifier stays on the backend.

  5. 4. Authorization request

    The browser follows that redirect and sends the authorization request. Its client ID identifies the application. Its redirect URI specifies the application’s return address. The authorization server checks that this address matches one registered for the application. The requested scopes describe the access the application wants. Including the openid scope also asks for OpenID Connect sign-in. The request carries state, the nonce, and the code challenge, but not the secret verifier.

  6. 5. Authentication + consent

    The authorization server checks who you are, using its sign-in page or an existing signed-in session. It asks you to approve the requested access when required, then decides which permissions this application will receive.

  7. 6. Authorization response

    After approval, the authorization server redirects the browser back with an authorization code and the original state value. The code is short-lived and single-use. It will let the backend request tokens. It is not an access token.

  8. 7. Check returned state

    The browser delivers that code and state to the backend. Before continuing, the backend compares the returned state with the marker saved for this browser’s sign-in attempt. If they do not match, it stops.

  9. 8. Token request

    With state checked, the backend sends the authorization code and its original secret verifier directly to the token endpoint, the authorization server’s address for issuing tokens. This backend also authenticates itself using its registered credentials. The authorization server checks the code and applies the same challenge calculation to the supplied verifier. If the result matches the earlier challenge, the PKCE check passes. This proves knowledge of the secret created when the flow began.

  10. 9. Token response

    The authorization server returns an access token for calling the protected API, and an ID token describing the authenticated user. They serve different purposes. The backend will check the ID token before trusting that identity. The backend may also receive a refresh token, which lets it request another access token later. In this flow, the backend keeps these tokens on the server.

  11. 10. ID token validation

    The backend validates the ID token’s signature using the expected authorization server’s trusted verification keys, checking that the identity message is genuine and unchanged. It also checks that the token came from the expected issuer, names this application as its audience, and has not expired. The backend then compares the token’s nonce with the value saved for this sign-in attempt. Once validation succeeds, it identifies the account by issuer and subject, the user’s identifier, and creates the application’s signed-in session.

  12. 11. Resource request

    The backend now calls the resource server, the service providing the protected API. It sends the access token in the request’s Authorization header. This bearer token must stay protected because anyone holding it can present it.

  13. 12. API checks + response

    The resource server validates the access token and checks its audience, scopes, and permissions for the requested data or action. A valid token alone does not grant access to every resource. If those checks pass, the API sends the permitted result back to the application backend. That completes the request.

  14. 13. Refresh token grant

    Later, when the access token expires, the backend can send its refresh token to the authorization server’s token endpoint. This refresh token grant requests a new access token without making you repeat sign-in, as long as continued access is allowed.

  15. 14. Refresh response

    The authorization server returns a new access token. In this example, it also replaces the refresh token and invalidates the old one, called refresh token rotation. The backend saves the replacement and uses the new access token for later API requests.

  16. Recap

    So the browser handles the redirects, the backend holds the tokens, and the API checks each request. OpenID Connect establishes who signed in. OAuth gives the application the approved API access.

Sources

Reviewing an OAuth or OIDC integration?

We can assess your identity flows, token handling and API authorization.

Get in touch