NATS WebCrypto Proof-of-Possession Authentication Example
This shows the authentication flow for browser-based Proof-of-Possession using a forked version of the nats.js client for nats v2.12.15. This extends your NATS zero trust network all the way to the web browser, enforcing Proof of Possession(PoP) instead of requiring a simple Bearer Token.
The purpose of this article is to advocate for the PR (https://github.com/nats-io/nats.js/pull/431) and showcase animation-building-skill (https://github.com/geekdotdev/animation-building-skill).
NATS is a distributed messaging system where who can connect and what they can touch is governed by a cryptographic chain of trust. This chain of trust allows permissions (traditionally a separate look-up) to be embedded directly into a credential, and Proof-of-Possession means a leaked credential cannot be used by anyone that doesn't also have the private key (leaking a private key is a colossal fuck up). This example shows: 1) "Operator Mode": A hierarchy of Ed25519-signed JWTs that allow different organizations to share infrastructure, and control client access within each organization's tenant account, guaranteeing their data only ever goes to the clients authorized for it. 2) NATS permissions mapped from roles configured in an OIDC identity server using the "Auth-Callout" integration.
Intended audience: Developers that are already familiar with NATS authentication.
Nov 2026 Update: nats.js maintainers merged an update (66c865e70) allowing your authenticator to return a Promise! Its marked as experimental and they did not provide the WebCrypto helper or the creds factory method, but Claude can help with those. You can now retire your bearer tokens in favor dpop, so your broker will always know exactly who it is talking to.
After log in to an OIDC identity server like keycloak, the browser generates a WebCrypto key pair and requests a NATS Zero Permission JWT from the SPA BFF, passing the public key. The SPA BFF validates the OIDC access token from an httpOnly cookie, and relays it to an internal signing service. That internal signing service re-verifies the token and mints a new NATS User JWT with no actual access to any subjects, but having 2 critical claims: 1) the browser's public key is given in the sub claim, anchoring it to the browser's WebCrypto key pair. 2) The OIDC access token, encrypted with wife-swapped key pairs pre-configured with auth-callout. This Zero Permission JWT is passed back to the browser.
Then the browser opens a connection to NATS, which challenges the connection with a nonce. The browser uses WebCrypto to sign that nonce with its non-extractable private key, returning a Promise that yields the signed challenge. The signed nonce and the ZP token containing the encrypted OIDC Access Token to NATS in the CONNECT frame. NATS uses the public key in the sub claim to verify possession of the corresponding private key that signed the nonce challenge. NATS delegates authorization to Auth-Callout, which validates the OIDC token and maps its claims to a NATS account and permissions - minting and signing a new User JWT then returning it to the broker. NATS retains that JWT internally to validate any requests from the browser as long as that connection is alive.
A Simpler Flow: KeyCloak DPoP
For Identity Providers that offer DPoP (like KeyCloak) , the resulting access token itself is bound to the browser's WebCrypto key pair's thumbprint, making the flow simpler, while shifting some trust diligence to your Auth-Callout impl. In this case, the OIDC access token holds the DPoP subject and the Zero Permission User JWT is just a 'ticket' bearer token with no authority and no claims.
As a DPoP token, the access token returned by KeyCloak has the cnf.jkt claim, which references the WebCrypto generated public key. The ZP User JWT is created as a static pre-signed bearer token, eliminating the need for a secure signing service. When the web browser connects to the NATS broker, it signs the nonce challenge with the WebCrypto private key as before. The web browser returns the following items to the NATS broker in the CONNECT frame: (1) ZP bearer token, (2) keycloak access token, (3) WebCrypto public key, and (4) the signed nonce.
When the NATS User JWT is generated as a bearer token, the broker still requires it, but can't and won't verify the nonce challenge. Cant because the sub refers to an AUTH Account (broker would need the browser's public key there for it to carry any meaning), and wont because its not part of the trust chain anyway. Therefore, the Auth-Callout service must perform the nonce signature verification or Proof of Possession will not be enforced. Where in the previous example, Auth-Callout only had to perform permission mapping, now there are two additional requirements for Auth-Callout:
- It must verify the nonce signature using the browser's public key.
- It must verify the trust chain by checking the access token's cnf.jkt thumbprint matches the hash of the browser's public key.
Any service that accepts an access token with a cnf.jkt claim is expected to implement the Proof of Possession protocol. Both of these use cases require async webCryptoAuthenticator or similar WebCrypto support. Acceptance of PR 431 would strengthen the NATS js client, whether running in a browser, node, or deno. And no changes are required for the server to support DPoP authentication.