Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

Implementing a client

Every client-side check from the earlier lessons has to live somewhere in real code, and where it lives decides whether it runs every time. The printer's rebuilt client wraps a maintained library, reads its endpoints from metadata, gives each job one home, and keeps its environments and secrets apart.

Starting from a library

The printing company is rebuilding its photo connection. The first version grew one function at a time: a handler for the Connect button, another for the callback, a December calendar job with its own copy of the refresh logic, and a helper for calling the photo API. Each piece worked when it was written. Together they kept tokens in three places, refreshed them in two different ways, and read the photo service's token endpoint from a configuration file that nobody updated when the address changed.

Every client-side check from Correlating requests and responses to OAuth security best practices was missing from at least one of those pieces. The rebuild starts by deciding where each check lives, so that it also runs in the code someone adds next year.

Much of a client's protocol work is precisely specified and easy to get slightly wrong. Generating a PKCE verifier and challenge, encoding an authorization URL, comparing the iss response parameter, form-encoding a client secret before HTTP Basic, signing a client assertion, and parsing an error response each have a correct answer, and each has caught out hand-written code. A well-maintained OAuth library has already met these problems, and its maintainers change it when security guidance changes.

Choosing one deserves some care. Look for active maintenance and a visible history of security fixes. Check that it supports what this integration needs: PKCE with S256, the iss response parameter, authorization server metadata, and the client authentication method in the printer's registration. Prefer a library whose defaults are already safe, so that PKCE is on unless someone deliberately turns it off and the implicit grant is not offered at all. For OpenID Connect, and for FAPI, a stricter profile of OAuth for high-value APIs such as banking, the OpenID Foundation certifies implementations that pass its conformance tests, and Interoperability and conformance testing, in Advanced OAuth, describes what such a result does and does not prove.

A library implements the protocol. It does not know what a printer account is, which session started an attempt, or what "connected" means to the printer's customers. The printing company therefore wraps the library in a small module of its own, and the rest of the application talks only to that module. Updating the library then means changing one module, and the team treats those updates like any other security fix.

Configuration from metadata

All domains, identifiers, and credentials in these examples are fictional. The printer's module starts from a handful of values:

issuer:        https://auth.photos.example
client_id:     photo-printer
redirect_uri:  https://printer.example/oauth/callback
scope:         photos.read
api_base:      https://api.photos.example

Endpoint addresses are missing on purpose. An authorization server can publish a small JSON document, its metadata, at a well-known address under its issuer, listing its endpoints and the features it supports. The module reads authorization_endpoint, token_endpoint, and revocation_endpoint from the photo service's metadata document at https://auth.photos.example/.well-known/oauth-authorization-server, after checking its issuer against the configured value. It handles that document the way Authorization server metadata, in Advanced OAuth, describes: it refreshes it on a schedule, keeps the last validated copy when a fetch fails, and raises an alert, rather than quietly changing its behavior, if a capability it relies on such as S256 disappears. If an endpoint answers 404 between scheduled fetches, the module reads the metadata again, once, and retries at the address it now lists. When the photo service moves its token endpoint, nobody at the printing company has to notice.

What stays in the module's own configuration is everything the printer decides for itself: which issuer it trusts, which client it is, where its callback lives, what access it asks for, and which API may receive its tokens. That division is worth keeping strict. A value the client should decide, such as its scope, never comes from a document the server publishes, and a value the server publishes, such as its token endpoint, is never copied into a file where it can go stale.

The parts of a client

Inside the module, the printer separates five jobs:

PartIts job
Transaction storeCreates the pending transaction when Ava selects Connect, and lets the callback claim it once, from the same session.
Callback handlerChecks the response against its transaction and expected issuer, exchanges the code, and saves the result.
Token storeKeeps each connection's tokens on the backend, encrypted, keyed by printer account and issuer.
Refresh coordinatorThe only code that refreshes. It allows one refresh per connection at a time and saves a replacement refresh token before anything uses the new access token.
API callerThe only code that attaches an access token to a request, and only to the configured photo API.

One function at a time

Tokens kept
In three places
Refreshed
In two different ways
Token sent to
Wherever a caller points
Token endpoint from
A file nobody updated

The endpoint moves: refreshes fail

One home for each job

Tokens kept
Token store
Refreshed
Refresh coordinator only
Token sent to
The configured photo API
Token endpoint from
Validated metadata

The endpoint moves: the module reads metadata again

A token goes only to the API it was issued for.

The printer's first version and its rebuilt module, asked the same questions. The rebuild keeps working when the photo service moves its token endpoint, because the module reads the address from metadata.

A shortened sketch shows how the last two fit together. It is written in TypeScript, the language of the printer's backend, and you can follow it without knowing the language: read await as "wait here for the database or the network to answer", and the comments say what each line is for. The oauth object stands for the wrapped library. withConnectionLock lets only one piece of code at a time work on a given connection; with one server that can be a lock on the connection's database row, and with several servers it must be a lock they all share, usually kept in the database or a cache:

async function currentAccessToken(connectionId: string): Promise<string> {
  return withConnectionLock(connectionId, async () => {
    const stored = await tokenStore.load(connectionId);
    if (!stored) throw new NeedsReconnection(connectionId);
    if (stored.accessTokenExpiresAt > Date.now() + 60_000) {
      return stored.accessToken;                      // good for at least a minute
    }
    const result = await oauth.refresh(stored.refreshToken);
    await tokenStore.replace(connectionId, result);   // save first, then use
    return result.accessToken;
  });
}

async function callPhotoApi(connectionId: string, path: string): Promise<Response> {
  const url = new URL(path, config.apiBase);
  if (url.origin !== new URL(config.apiBase).origin) {
    throw new Error('Refusing to send a photo API token to another host');
  }
  const token = await currentAccessToken(connectionId);
  return fetch(url, { headers: { Authorization: `Bearer ${token}` } });
}

The origin check writes a rule from Using access tokens into the one function that attaches tokens: a token goes only to the API it was issued for. It is needed because new URL treats a full address as replacing the base, so a path such as https://elsewhere.example/ would otherwise carry the token to another host. The photo picker and the December calendar job both call callPhotoApi, so both get the same refresh behavior and the same rule about where tokens may go. If two workers ask for the same connection at once, the second waits for the lock and then finds the fresh token the first one saved. When a refresh fails with invalid_grant, the coordinator marks the connection as needing reconnection, instead of letting each caller decide what that error means. A 401 from the API leads to one refresh and one retry, as Using access tokens described, and that logic belongs in the API caller for the same reason.

The callback handler follows the order the authorization code lessons taught in Correlating requests and responses and Errors and denied access: claim the transaction, compare the issuer, handle an error response, and only then exchange the code, sending it to the token endpoint taken from validated metadata. Keeping those steps in one handler makes their order visible and easy to test.

Environments and secrets

The printer's test website at https://test.printer.example runs the same code with a different configuration and its own registration at the photo service:

SettingProductionTest
Client IDphoto-printerphoto-printer-test
Redirect URIhttps://printer.example/oauth/callbackhttps://test.printer.example/oauth/callback
Client credentialFrom the production secrets storeFrom the test secrets store
Photo accountsCustomers' accountsAccounts created for testing

The issuer is the same in both, because the test site connects to the photo service's real authorization server with test accounts. Everything that identifies the printer differs. If the test site accidentally loaded the production client ID, its first authorization request would fail at the photo service, because the test site's redirect URI is not registered for photo-printer. Separate registrations turn that configuration mistake into an error instead of a leak.

The client credential is the one value here that must stay private. The module reads it at run time from a secrets store, not from the repository or a file built into the packaged copy of the program that gets deployed. It never appears in logs, in error messages, or in process arguments, which other users of a machine can often read. Each environment has its own credential, so a developer with access to test credentials holds nothing that works in production. When the credential needs replacing, the overlap from Rotating client credentials applies, and a registration that uses private_key_jwt removes the shared secret altogether.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

The printer's test website starts by mistake with the production client ID, photo-printer, while still using its own redirect URI. What happens when someone selects Connect there? 1 of 3

Choose one answer.

Why does every part of the printer reach the photo API through one API caller and one refresh coordinator? 2 of 3

Choose one answer.

The photo service moves its token endpoint while the printer's module still holds metadata it read earlier. What happens at the next refresh? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity