Files
documenso/packages/ee/server-only/signing/csc/cookies/sad-session-cookie.ts
T
Lucas Smith d5ce222482 feat: add CSC AES/QES signing (v1 instance-wide config) (#2874)
Adds Cloud Signature Consortium (CSC) integration for AES/QES signing
against a configured TSP. v1 ships as instance-wide configuration via
environment variables, with per-envelope signature level selection,
license gating, and an OAuth-driven signing flow (capture + FIFO
signers, SAD session, blocking/in-progress recipient pages).

Includes signature level compatibility checks (role, signing order,
dictate next signer), envelope mutability assertions, Prisma migration
for signature level and CSC tables, and docs for the new signing
certificate options.
2026-06-16 23:37:34 +10:00

62 lines
2.3 KiB
TypeScript

import type { Context } from 'hono';
import { deleteCookie, getSignedCookie, setSignedCookie } from 'hono/cookie';
import { parseSigned } from 'hono/utils/cookie';
import { CSC_SAD_SESSION_COOKIE_NAME, cscCookieBaseOptions, getCscCookieSecret } from './shared';
/**
* `csc_sad_session` — HMAC-signed `CscSession` cuid. Set after the
* credential-scope OAuth callback exchanges code → SAD; pointed at the
* server-side session row that owns the SAD + the prep-time item hashes.
*
* Lifetime mirrors the TSP-asserted SAD expiry (`sadExpiresAt`) so the cookie
* cannot outlive its server-side authorisation. Cleared by the sync sign
* mutation on success; otherwise decays naturally with the browser TTL.
*/
type SetCscSadSessionCookieOptions = {
c: Context;
sessionId: string;
/** Mirror of `CscSession.sadExpiresAt`; cookie expires no later than the SAD. */
expiresAt: Date;
};
export const setCscSadSessionCookie = async (options: SetCscSadSessionCookieOptions): Promise<void> => {
const { c, sessionId, expiresAt } = options;
await setSignedCookie(c, CSC_SAD_SESSION_COOKIE_NAME, sessionId, getCscCookieSecret(), {
...cscCookieBaseOptions,
expires: expiresAt,
});
};
export const getCscSadSessionCookie = async (c: Context): Promise<string | null> => {
const value = await getSignedCookie(c, getCscCookieSecret(), CSC_SAD_SESSION_COOKIE_NAME);
// `getSignedCookie` returns `false` on signature mismatch, `undefined` when
// the cookie is absent. Both collapse to `null` for the caller's sake.
return value ? value : null;
};
export const clearCscSadSessionCookie = (c: Context): void => {
deleteCookie(c, CSC_SAD_SESSION_COOKIE_NAME, cscCookieBaseOptions);
};
/**
* Remix-compatible reader: parses + HMAC-verifies the SAD-session cookie
* from a raw `Cookie` header on a standard `Request`. Mirrors
* `getCscSadSessionCookie` but works outside Hono's `Context`.
*/
export const readCscSadSessionFromRequest = async (request: Request): Promise<string | null> => {
const cookieHeader = request.headers.get('cookie');
if (!cookieHeader) {
return null;
}
const parsed = await parseSigned(cookieHeader, getCscCookieSecret(), CSC_SAD_SESSION_COOKIE_NAME);
const value = parsed[CSC_SAD_SESSION_COOKIE_NAME];
return typeof value === 'string' ? value : null;
};