Skip to content
Get started
Bastionary › Knowledge base › Tokens and APIs

Private Key JWT: stronger OAuth client authentication than secrets

By Bastionary · Tokens and APIs

A client secret is a password your service shares with the authorization server. Like every shared password, it has a habit of ending up where it should not: hardcoded in a repository, baked into a container image, pasted into a CI variable that forty people can read, or logged by a well-meaning HTTP debug middleware. Once it leaks, anyone holding it can impersonate your client completely — request tokens, refresh them, act as the application — and rotating it means a coordinated redeploy across everything that holds a copy.

The private_key_jwt client authentication method removes the shared secret entirely. Instead of sending a secret to the token endpoint, the client signs a short-lived JWT assertion with a private key it never transmits, and the server verifies it with the corresponding public key. There is nothing shared to steal. This is the client-side analogue of why we stopped storing plaintext passwords, and for high-value integrations it should be the default.

How it works

Built on the OAuth 2.0 assertion framework (RFC 7521) and its JWT profile (RFC 7523), and named private_key_jwt by OpenID Connect Core, the method replaces the client_secret parameter with two others: a client_assertion_type naming the JWT bearer assertion type, and a client_assertion that is itself a signed JWT.

POST /token HTTP/1.1
Host: auth.example.com
Content-Type: application/x-www-form-urlencoded

grant_type=authorization_code
&code=SplxlOBeZQQYbYS6WxSbIA
&redirect_uri=https://app.example.com/callback
&client_id=app-7f2
&client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
&client_assertion=eyJhbGciOiJSUzI1NiIsImtpZCI6ImNsaWVudC0yMDI2In0...

The assertion's claims are specific and the server checks every one. A token that is missing or wrong on any of them is rejected.

{
  "iss": "app-7f2",
  "sub": "app-7f2",
  "aud": "https://auth.example.com/token",
  "jti": "a3f9c21e-7b44-4e1d-9c2a-0f81d6e4b7aa",
  "iat": 1776700000,
  "exp": 1776700060
}

The iss and sub are both the client ID — the client is asserting its own identity. Crucially, aud is the token endpoint's URL. The assertion is bound to the exact endpoint it is for, so it cannot be captured at one server and replayed at another.

Why it is stronger than a secret

The security gain comes from three properties that a shared secret cannot offer.

The credential never crosses the wire

With client_secret_post or client_secret_basic, the secret travels in every token request. TLS protects it in transit, but it is still present at both endpoints, in request logs, and in any proxy that terminates TLS. The private key in private_key_jwt stays on the client; only signatures over short-lived assertions ever leave.

Assertions are single-use and short-lived

Give the assertion a 60-second lifetime and a unique jti, and the server can reject reuse. Even if an attacker captures one assertion from a log, it expires in seconds and cannot be replayed because the server remembers the jti within the validity window.

The server must actually enforce jti uniqueness and a tight exp for the replay protection to be real. A server that accepts a 24-hour assertion and does not track jti has the ceremony of private_key_jwt without its benefit — a captured assertion becomes a day-long bearer credential. Reject assertions with a lifetime beyond a couple of minutes, and keep a short-term cache of seen jti values.

Rotation is a key operation, not a secret scramble

You rotate by publishing a new public key and signing with the new private key — the same overlap-window discipline as any key rotation. You never have to distribute a new secret to every service that authenticates, because there was never a secret to distribute.

Publishing the public key

The server needs the client's public key to verify assertions. There are two ways to give it one, and the second is strictly better operationally.

  • Static registration: the client uploads its public key (a JWK) at registration time. Simple, but rotation requires a manual re-upload.
  • A jwks_uri: the client hosts its own JWKS endpoint and registers the URL. The server fetches the current public keys from it, exactly like a resource server fetches the authorization server's JWKS. Now the client can rotate keys on its own schedule by updating its own endpoint — no coordination with the server at all.

The jwks_uri approach makes client key rotation symmetric with server key rotation, which means your team already knows how to operate it.

Signing the assertion

import { SignJWT, importPKCS8 } from 'jose';
import { randomUUID } from 'node:crypto';

async function clientAssertion(clientId: string, tokenEndpoint: string) {
  const key = await importPKCS8(process.env.CLIENT_PRIVATE_KEY!, 'RS256');
  return new SignJWT({})
    .setProtectedHeader({ alg: 'RS256', kid: 'client-2026-a' })
    .setIssuer(clientId)
    .setSubject(clientId)
    .setAudience(tokenEndpoint)   // bind to the exact endpoint
    .setJti(randomUUID())
    .setIssuedAt()
    .setExpirationTime('60s')
    .sign(key);
}

Note the kid in the header: it lets the server pick the right public key from the client's set during a rotation overlap, the same way a resource server selects a verification key.

When to require it

You do not need private_key_jwt for a low-stakes first-party web app where the secret lives in a secrets manager and the blast radius of a leak is small. You should strongly prefer it for:

  • Service-to-service and machine-to-machine clients, which have no user in the loop to notice anomalies and often run in environments where secrets sprawl.
  • Any client with broad or sensitive scopes, where impersonation would be high-impact.
  • Integrations you expose to partners or third parties, where you cannot audit how carefully they store a secret but you can require them to prove possession of a key.
  • Regulated environments; profiles like FAPI mandate private_key_jwt (or mTLS) precisely because shared secrets are not good enough.
If you are choosing between private_key_jwt and mutual TLS for client authentication, they solve the same problem by different means: one proves possession of a private key via a signed assertion, the other via a TLS handshake, and mTLS additionally binds the issued token to the certificate. Pick based on what your infrastructure already terminates cleanly — adding either is a large improvement over a shared secret.

The underlying shift

Moving from secrets to signed assertions is the same move the industry made for user passwords: stop sharing a reusable string, start proving possession of a key. The payoff is that a credential leak stops being catastrophic — there is no longer a single value that, once copied, grants full impersonation.

Requiring it per client, enforcing jti and tight expiry, and accepting client jwks_uri rotation are all policy decisions at the token endpoint — which is another reason teams that care about this configure it on an authorization server they operate themselves, where "require private_key_jwt for all machine clients" is a setting they can turn on rather than a feature request.

Run it yourself

Bastionary is free, open-source software. Follow the quick start to have it running on your own server, or browse the releases on Git.

Get started