@invokta/auth-jwt
@invokta/auth-jwt verifies signed JWT access tokens from one configured issuer
and returns the function that serveMcpHttp expects as its authenticate
hook. It is optional: the core and the MCP adapter do not depend on it, and any
host-written hook remains valid.
The package adds no execution path. The principal it returns reaches
capabilities only through engine.invoke, where each capability’s access
rule still decides authorization.
Install
Section titled “Install”yarn add @invokta/auth-jwtThe package is native ESM and requires Node.js 22.20.0 or later. Its only
runtime dependency is jose 6.2.15, pinned exactly.
The package root exports:
createJwtBearerAuthenticatorandJwtBearerAuthenticatorOptions;JwtBearerPolicyOptionsandJwtKeySource;JwtBearerAuthenticator,JwtBearerRequest, andJwtBearerHeaders;JwtPrincipal,JwtPrincipalMapper,VerifiedJwtClaims, anddefaultJwtPrincipal;JwtSignatureAlgorithmandisJwtSignatureAlgorithm; andJwtVerificationUnavailableError.
function createJwtBearerAuthenticator( options: JwtBearerAuthenticatorOptions,): JwtBearerAuthenticator;
type JwtBearerAuthenticator = ( request: JwtBearerRequest,) => Promise<JwtPrincipal | null>;
interface JwtBearerRequest { readonly headers: { get(name: string): string | null }; readonly signal?: AbortSignal;}
interface JwtPrincipal { readonly id: string; readonly attributes?: Readonly<Record<string, unknown>>;}The request view is structural. The MCP HTTP and plain HTTP
(@invokta/http) authentication requests, a Web
Request, and any adapter exposing the same header lookup fit without
conversion. JwtPrincipal is structurally identical to the core Principal.
Use it with MCP HTTP
Section titled “Use it with MCP HTTP”import { createJwtBearerAuthenticator } from "@invokta/auth-jwt";import { serveMcpHttp } from "@invokta/mcp";
const requiredScopes = ["engine:invoke"];
await serveMcpHttp(engine, { auth: { mode: "required", authenticate: createJwtBearerAuthenticator({ issuer: "https://identity.example.com", audience: "https://engine.example.com/mcp", algorithms: ["RS256"], jwksUrl: "https://identity.example.com/.well-known/jwks.json", requiredScopes, }), resourceMetadata: { resource: "https://engine.example.com/mcp", authorizationServers: ["https://identity.example.com"], scopesSupported: requiredScopes, }, challengeScopes: requiredScopes, },});Options
Section titled “Options”| Option | Required | Contract |
|---|---|---|
issuer |
Yes | Exact iss value, a non-empty string without surrounding whitespace |
audience |
Yes | A string or non-empty list; the token’s aud must contain one |
algorithms |
Yes | Non-empty, duplicate-free allowlist of JwtSignatureAlgorithm values |
jwksUrl |
One key source | HTTPS URL, or loopback HTTP for development, without credentials or a fragment |
jwks |
One key source | Local JSON Web Key Set containing public keys only |
clockToleranceSeconds |
No | Integer from 0 to 300 applied to exp and nbf; default 0 |
requiredScopes |
No | Non-empty, duplicate-free list of RFC 6749 scope tokens |
maxTokenLength |
No | Integer from 1 to 16,384 characters; default 8,192 |
principal |
No | Synchronous JwtPrincipalMapper; default defaultJwtPrincipal |
JwtSignatureAlgorithm is one of RS256, RS384, RS512, PS256, PS384,
PS512, ES256, ES384, ES512, EdDSA, or Ed25519. none, HS256,
HS384, HS512, unknown names, and duplicates are rejected.
isJwtSignatureAlgorithm narrows a configuration string to that union.
createJwtBearerAuthenticator validates and snapshots every option. An invalid
option throws a TypeError whose message never repeats the configured value.
Mutating an options object afterward does not change the authenticator.
Key sources
Section titled “Key sources”jwksUrl uses the jose remote key set. Nothing is fetched until the first
credential arrives. Each fetch has a 5-second timeout and follows no redirect.
| Key-set state | Fetch behavior |
|---|---|
| After a successful fetch | Cached for 10 minutes; a token with an unknown key identifier triggers a refetch at most once every 30 seconds |
| After a failed fetch | Nothing is fetched for 5 seconds on the monotonic clock; a fresh cached key set keeps answering, otherwise requests reject at once |
| Concurrent lookups that need a fetch | Share one in-flight fetch |
Random key identifiers therefore cause at most one refetch every 30 seconds while the issuer is healthy and one fetch attempt every 5 seconds while it is failing. The response body is read whole, bounded only by the fetch timeout, so configure only an issuer endpoint you trust.
jwks verifies against a local set of public keys, for tests, offline
development, or pinned keys. A key carrying private material (d, k, or
priv) is rejected.
Configure the URL from the issuer’s OpenID Connect or OAuth metadata
jwks_uri. The package does not discover it.
Verification outcomes
Section titled “Verification outcomes”The authenticator reads only the authorization header and accepts
Bearer <token> with a case-insensitive scheme, exactly one space, and a
compact JWS of three non-empty base64url segments within maxTokenLength.
| Request | Result | MCP HTTP |
|---|---|---|
Token verifies, carries sub and exp, grants every required scope, and maps to a valid principal |
Principal | Continues to engine.invoke |
| Missing, malformed, or oversized credential | null |
401 with the Bearer challenge |
Expired or not yet valid, wrong issuer or audience, algorithm outside the allowlist, unknown key, bad signature, or missing sub or exp |
null |
401 |
Missing required scope, or a mapper result of null or a malformed principal |
null |
401 |
A kid missing from a fresh cached key set while a refetch fails or within the 5-second failure window, including a key the issuer has just rotated in |
null |
401 until a refetch succeeds |
| Key set times out, is unreachable, answers anything but 200, or is malformed, including during the 5-second failure window without a fresh cache; the request is aborted | Rejects with JwtVerificationUnavailableError |
Sanitized 500 |
A token without a kid header that fits several published keys, as during an
issuer’s key rotation, is tried against at most eight of them in key-set order.
It verifies if one of them signed it; otherwise it resolves null, so a forged
token without kid is a 401, never a 500. Each candidate costs one signature
check against an issuer key, so the worst case for one token is eight checks:
roughly 3 ms of CPU with RS256 or EdDSA keys and about 21 ms with ES512 keys
on a shared 4-CPU machine. The caller controls only the header and signature;
the number and size of keys come from the issuer.
During an issuer outage with a fresh cache, an unknown kid fails closed as
null rather than as an unavailable key set. A token signed with a newly
rotated key is therefore rejected until a refetch succeeds, and a forged
unknown kid cannot turn the outage into a 500 for every caller.
A null result never states which check failed.
JwtVerificationUnavailableError always has the message
JWT bearer verification is unavailable. and no cause, so no token, key-set
URL, or provider response can reach a response or a log through it. An error
thrown by a custom mapper propagates unchanged.
The optional signal is observed before and during verification. jose takes
no per-call signal, so a key-set fetch already in flight completes in the
background within its timeout and refreshes the cache.
Principal mapping
Section titled “Principal mapping”type VerifiedJwtClaims = Readonly<JWTPayload>;
type JwtPrincipalMapper = ( claims: VerifiedJwtClaims,) => JwtPrincipal | null;
function defaultJwtPrincipal( claims: VerifiedJwtClaims,): JwtPrincipal | null;defaultJwtPrincipal returns a frozen principal whose id is sub and whose
attributes contain only:
| Attribute | Source |
|---|---|
scopes |
The space-delimited scope claim, split and de-duplicated; [] when absent or not a string |
issuer |
iss |
clientId |
client_id, when it is a non-empty string |
It returns null for a blank sub or an empty iss. A custom mapper receives
the verified claims only, never the raw token. Copy only verified,
authorization-relevant claims into the principal.
Limits
Section titled “Limits”The package does not:
- issue, refresh, introspect, or revoke tokens;
- discover
jwks_urifrom issuer metadata; - accept opaque, encrypted, or shared-secret tokens, or several issuers;
- enforce the RFC 9068
typheader; - verify DPoP or mTLS sender constraints, or bind sessions;
- bound the size of the remote key-set response beyond its fetch timeout; or
- retry a failed key-set fetch or log anything.
Write a host hook for those requirements. Read the HTTP authentication guide for the hook contract and the JWT bearer recipe for a runnable engine.