Skip to content

Reference

JOSE and typed JWTs

Auth uses effect/jose for JWT signing, verification and public key sets. Applications own their claims Schema, keys, accepted algorithms, issuers, audiences and token purpose. Supply a crypto backend for signing and verification, and an Effect HttpClient for remote keys.

bun add effect @effect/platform-node
import { NodeCrypto } from "@effect/platform-node";
import { Effect, Redacted, Schema } from "effect";
import { Jwk, Jwt } from "effect/jose";
const Claims = Schema.Struct({
sub: Schema.String,
role: Schema.Literals(["admin", "member"]),
});
// Read these from application-owned key configuration and the incoming request.
declare const publicJwk: unknown;
declare const token: Redacted.Redacted<string>;
const verified = Effect.gen(function* () {
const credential = yield* Schema.decodeUnknownEffect(
Schema.Redacted(Schema.NonEmptyString.check(Schema.isMaxLength(16384))),
)(token);
const jwk = yield* Schema.decodeUnknownEffect(Jwk.PublicJwk)(publicJwk);
const key = yield* Jwk.importPublic(jwk, "ES256");
return yield* Jwt.verify(Claims, credential, key, {
issuer: "https://issuer.example",
audience: "api",
typ: "JWT",
requiredClaims: ["exp"],
});
}).pipe(Effect.provide(NodeCrypto.layer));

The imported key fixes the algorithm. Verification authenticates the signature, checks registered claims, then decodes the application Schema. The result contains claims, registeredClaims, and protectedHeader. Jwt.sign(schema, claims, privateKey, header) encodes claims before signing. Both operations preserve Schema failures and service requirements.

Decode untrusted JWK values with Jwk.PublicJwk or Jwk.PrivateJwk before importing them. Private inputs and signed tokens use Redacted; imported signing keys are opaque Effect crypto keys. Jwt.decodeUnverified returns unauthenticated claims and must never establish identity or authority.

Effect’s JOSE modules impose no token-size limit; bound credentials before parsing them. Auth’s OAuth resource verifier accepts at most 16,384 characters, client assertions at most 8,192, and the OIDC verifier at most 65,536. JWT clockTolerance and maxTokenAge accept Effect durations: use explicit values such as "10 minutes"; numeric durations mean milliseconds. exp and nbf are checked whenever present; use requiredClaims to require them.

Use Jwks.layerLocal({ keys }) for fixed public keys. For a remote issuer, build and share one scoped Layer:

import { Layer } from "effect";
import { FetchHttpClient } from "effect/http";
import { Jwks } from "effect/jose";
const Http = FetchHttpClient.layer.pipe(
Layer.provide(Layer.succeed(FetchHttpClient.RequestInit, {
redirect: "manual",
credentials: "omit",
})),
);
const TrustedKeys = Jwks.layerRemote({
url: "https://issuer.example/keys",
timeToLive: "10 minutes",
cooldown: "30 seconds",
timeout: "5 seconds",
maxResponseBytes: 131072,
maxKeys: 64,
}).pipe(Layer.provide(Http));

The shown options are Effect’s defaults. The HTTPS URL is trusted application configuration; tokens cannot select it. The supplied client must enforce network egress policy and refuse redirects. Effect rejects responses from another URL, but that check cannot prevent a client from sending a redirected request.

Provide this Layer to Jwt.verifyWithKeySet with an explicit algorithms allowlist, or to Auth’s resource verifier. Key selection must find exactly one matching public key. Unknown key families and curves are ignored; private and symmetric keys reject the set.

Effect coalesces concurrent refreshes, applies a cooldown after failures or unknown keys, and never falls back to expired keys after a failed refresh. A refresh can outlive an individual waiting caller; closing its owning Scope interrupts it. On Workers, retain bounded verification through ctx.waitUntil at the request boundary. Remote resolvers do not impose a waiter limit; applications own admission policy when needed.

JOSE failures use JoseError with a typed reason, including ClaimValidationFailed and KeySetUnavailable. Auth maps these to its public credential and availability errors. Avoid logging raw JOSE errors, unverified claims or private material; backend causes can carry diagnostic data.