feat: verify context attestations via ES256/JWKS + rename client to appshell-crm

Proof #3 now supports the production asymmetric path, not just the dev shared secret.

- context-attestation.ts: PublicKeyResolverPort seam; verify() picks ES256 (JWKS by
  kid) when the client has a jwksUri, else HS256 (dev). signDevAttestationES256 helper.
- attestation-stores.ts: JwksPublicKeyResolver (jwks-rsa, one cached client per URI,
  refetch on rotation, fail-closed to NO_KEY).
- attestation.module.ts: inject the resolver into the verifier; dev-seed the client as
  clientType APPSHELL (it is the CRM browser-flow parent per the July-12 notes).
- rename the registered client crm-support-widget -> appshell-crm (the name reflects the
  attesting parent, not a widget).
- specs: ES256 (valid via JWKS, wrong key -> BAD_SIGNATURE, unknown kid -> NO_KEY, no
  resolver -> NO_KEY) + two stolen-token gate cases (wrong app binding -> APP_MISMATCH,
  wrong audience -> WRONG_AUDIENCE).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-07-15 12:56:10 +05:30
parent 85a78eb21e
commit e39caa3c80
8 changed files with 252 additions and 29 deletions
@@ -1,7 +1,13 @@
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { JwksClient } from 'jwks-rsa';
import { PrismaService } from '../prisma/prisma.service';
import type { ClientRegistryEntry, ClientRegistryPort, NonceStorePort } from './context-attestation';
import type {
ClientRegistryEntry,
ClientRegistryPort,
NonceStorePort,
PublicKeyResolverPort,
} from './context-attestation';
/** Client registry backed by Postgres (IiosClientRegistry). */
@Injectable()
@@ -40,3 +46,31 @@ export class PrismaNonceStore implements NonceStorePort {
}
}
}
/**
* Resolves a client's public signing key from its JWKS (prod ES256 path). Keeps ONE JwksClient
* per jwksUri — the client caches keys and refetches on a cache miss (so key rotation is picked up
* without a restart). Returns null on any failure so the verifier fails closed with NO_KEY.
*/
@Injectable()
export class JwksPublicKeyResolver implements PublicKeyResolverPort {
private readonly clients = new Map<string, JwksClient>();
private clientFor(jwksUri: string): JwksClient {
let c = this.clients.get(jwksUri);
if (!c) {
c = new JwksClient({ jwksUri, cache: true, cacheMaxEntries: 8, cacheMaxAge: 10 * 60_000, rateLimit: true, jwksRequestsPerMinute: 12 });
this.clients.set(jwksUri, c);
}
return c;
}
async resolve(jwksUri: string, kid: string): Promise<string | null> {
try {
const key = await this.clientFor(jwksUri).getSigningKey(kid);
return key.getPublicKey(); // PEM (SPKI) — works for EC (ES256) and RSA keys
} catch {
return null; // unknown kid / unreachable JWKS / malformed key → fail closed
}
}
}