DPoP with Keycloak: make stolen tokens useless in the browser

In short
  • With DPoP (RFC 9449), the browser holds a key pair it can't export, and every access token only works with a fresh proof signed by that key.
  • keycloak-js now supports DPoP: one init option, and keycloak.fetch adds the proof to every API call. It's merged but not yet released.
  • An API that checks the proof rejects a stolen token, even when the attacker signs a valid proof with their own key. Tested on Keycloak 26.4.

Why DPoP?

A bearer token is like cash: whoever holds it can spend it. Browser tokens leak through storage, logs, proxies and XSS, and a leaked one works from anywhere until it expires.

Bearer tokenDPoP-bound token
Whoever holds it can use it, from anywhereOnly usable with a proof signed by the client's private key
A leak from logs, storage or XSS is game overA leaked token alone gets a 401
Refresh tokens in a SPA are long-lived bearer secretsRefresh tokens are bound too: Keycloak won't refresh without the key

mTLS binds tokens to a TLS client certificate, which suits services but not browsers. DPoP does the binding in the application layer, so it works for single-page apps, mobile apps, and anything behind a TLS-terminating proxy.

Step 1: A client that requires DPoP

The app is a public client, because a browser app can't keep a secret, so it logs in with PKCE. One attribute makes Keycloak refuse any token request or refresh that doesn't carry a DPoP proof:

kcadm create clients -r acme -s clientId=notes-app -s publicClient=true \
  -s standardFlowEnabled=true -s directAccessGrantsEnabled=false \
  -s 'redirectUris=["http://localhost:3300/*"]' -s 'webOrigins=["http://localhost:3300"]' \
  -s 'attributes."pkce.code.challenge.method"=S256' \
  -s 'attributes."dpop.bound.access.tokens"=true'

Step 2: The switch in the admin console

The same setting is on the client's Settings tab, under Capability config → Require DPoP bound tokens. It's no longer on the Advanced tab, and since Keycloak 26.4 DPoP is a fully supported feature rather than a preview.

Step 3: The app, with keycloak-js

DPoP support was added to keycloak-js in PR #253. It's merged but not yet in an npm release (26.2.4 has no DPoP), so for now vendor lib/ from main and pin the commit.

import Keycloak from "/vendor/keycloak-js/keycloak.js";

const keycloak = new Keycloak({ url: "http://localhost:9080", realm: "acme", clientId: "notes-app" });
await keycloak.init({
  onLoad: "check-sso",
  pkceMethod: "S256",
  dpopConfig: { mode: "strict" } // fail loudly if Keycloak ever hands out a plain Bearer token
});

// Write a normal Bearer call: keycloak.fetch swaps in the DPoP scheme and attaches a fresh proof
const res = await keycloak.fetch("/api/notes", { headers: { Authorization: `Bearer ${keycloak.token}` } });
  • keycloak-js creates a non-extractable key pair in IndexedDB, signs a proof for every token and refresh request, and clears the key on logout.
  • strict throws if Keycloak returns a Bearer token; auto quietly falls back to Bearer.
  • Web Crypto needs a secure context: HTTPS, or http://localhost in development.

Gotcha: the current docs say useDPoP and secureFetch. The merged code reads dpopConfig and exposes keycloak.fetch. The wrong option name is silently ignored: no error, and no DPoP.

Step 4: The API checks every proof

A valid token isn't enough: each request needs a fresh proof, a small JWT signed by the browser's key with the public key in its header. After validating the token as usual, the API checks (RFC 9449 section 4.3):

CheckStops
Authorization: DPoP …, never BearerThe token replayed as a plain bearer token
Proof signature, typ=dpop+jwt, public jwk in the headerForged proofs
htm and htu match this requestA proof captured for one endpoint used on another
iat within about 60 s, jti never seen beforeReplaying a captured proof
ath = SHA-256 of the access tokenA proof made for a different token
Thumbprint of the proof's key = the token's cnf.jktA stolen token with the attacker's own key

Step 5: Sign in and look at the key

Sign in as Alice and decode her access token: the type is DPoP, and cnf.jkt is the thumbprint of the key in her browser.

{
  "typ": "DPoP",
  "azp": "notes-app",
  "preferred_username": "alice",
  "cnf": { "jkt": "nsLt1KHD1gw00DYuSxcW2Y7gPIH-8di_y7xy1-nuLUA" }
}

Then try to read the private key from DevTools:

> keys.privateKey
< CryptoKey {type: 'private', extractable: false, algorithm: {name: 'ECDSA', namedCurve: 'P-256'}, usages: ['sign']}
> await crypto.subtle.exportKey("jwk", keys.privateKey)
✖ Uncaught InvalidAccessError: Failed to execute 'exportKey' on 'SubtleCrypto': key is not extractable

The page can ask the key to sign, but nobody can copy it out.

Step 6: Steal the token

Copy Alice's token out of the page, like an attacker would, and try to use it:

AttemptResult
Stolen token as a normal Bearer token401 expected the DPoP scheme, got Bearer
Stolen token, no proof401 missing DPoP proof
Replay a proof sniffed from Alice's browser401 proof already used (replay)
Stolen token + a valid proof signed with the attacker's key401 token is not bound to your key
Stolen refresh token, no proofKeycloak: DPoP proof is missing
Stolen refresh token + the attacker's keyKeycloak: DPoP confirmation doesn't match DPoP proof

A stolen token, on its own, gets an attacker nothing.

Before you go to production

  • HTTPS everywhere: DPoP doesn't replace TLS.
  • Every API verifies proofs, or an API gateway does it in front.
  • Share the replay cache (jti) across API instances, for example in Redis.
  • For high-value APIs, add server nonces (DPoP-Nonce); keycloak.fetch handles the retry.
  • Keep fixing XSS: script in the page can't steal the key, but it can still ask it to sign.
  • Turn on Require DPoP bound tokens per client, starting with public clients.

Keycloak DPoP FAQ

Which Keycloak version supports DPoP?

DPoP is fully supported from Keycloak 26.4; earlier versions had it as a preview feature. The client switch is under Settings → Capability config.

Does keycloak-js support DPoP?

Yes, on main: support was merged in PR #253 but hasn't shipped in an npm release yet. Use dpopConfig and keycloak.fetch, not the useDPoP and secureFetch names in the current docs.

DPoP or mTLS?

Use mTLS for service-to-service traffic where you control the certificates. Use DPoP for browser and mobile apps, or anywhere TLS is terminated before your API.

Rolling out DPoP or worried about token theft? We implement Keycloak token binding for Australian organisations, from the client library to every API. Talk to us.

Talk to a Keycloak specialist

The first conversation is free.

Make an enquiry