wyuc e15780835a
feat(media): convert legacy references to allocated asset ids (#1101)
Lands epic #1007 part 2(c): the app-side reference converter, the `audioUrl`
removal, and the `DSL_VERSION` bump as one delivery unit.

`convertDocumentAssetRefs` walks a loaded document (slide canvases, whiteboards,
video-manifest keys, speech actions) and rewrites legacy handles to allocated
pool ids: placeholders and `audioId`s with local bytes are ingested, a co-present
`audioUrl`/`audioId` pair collapses to one asset, a definitively dead URL
converts to an emptied reference, and a transient failure keeps both handles for
a later retry. Unavailable bytes leave the document untouched for a later open,
and a converted document returns by identity with no writes.

Conversion runs lazily on document open and at fetch time for server-generated
classrooms, with conversion and the first document save sharing the per-stage
document lock: once the document commits it owns every allocation, and every
failure exit rolls its pass ledger back. Legacy URL probes are bounded by a
per-probe timeout and a shared 60-second pass budget.

`SpeechAction.audioUrl` is removed from the DSL and every consumer reads the
converted shape; the server classroom generator keeps its derived-id + serving-URL
pair as an explicitly typed pre-conversion transport, consumed before persistence.
`DSL_VERSION` becomes 0.2.0 with the first real ladder entry, and
`@openmaic/dsl` goes to 0.9.0 per the release rule.

Deferred follow-ups are tracked in #1129: production principal derivation with
document/asset ownership aligned, conditional document writes, and hoisting the
aggregate probe budget into the export and video-timeline paths.

Refs #1007
2026-08-15 17:38:04 +08:00

56 lines
2.7 KiB
TypeScript

/**
* DEVELOPMENT-ONLY authentication for the embedded persistence route.
*
* The token is NOT a secret: NEXT_PUBLIC_PERSISTENCE_TOKEN is compiled into
* the public browser bundle, so it is fully visible to every visitor and
* provides no confidentiality and no user isolation — anyone who can load the
* page can read and write EVERY learner partition and all documents by
* supplying an arbitrary x-learner-key. Its only purpose is to keep unrelated
* network scanners out of a trusted-network endpoint. Suitable only for
* localhost or trusted-network, single-user deployments. Production must
* replace this module with real session verification and derive learner
* identity from server-controlled claims.
*/
import { createHash, timingSafeEqual } from 'node:crypto';
import type { IncomingMessage } from 'node:http';
import type { AssetPrincipal } from '@openmaic/storage';
import type { RuntimeHttpPrincipal } from '@openmaic/storage/server';
type PersistencePrincipal = RuntimeHttpPrincipal & Partial<Pick<AssetPrincipal, 'key'>>;
/**
* The single asset partition for this deployment shape. Documents have no
* ownership partition; assets get the same treatment until real auth lands.
*/
const SHARED_ASSET_PRINCIPAL = 'shared';
function singleHeader(value: string | string[] | undefined): string | undefined {
return Array.isArray(value) ? value[0] : value;
}
function secureEqual(left: string, right: string): boolean {
const leftDigest = createHash('sha256').update(left).digest();
const rightDigest = createHash('sha256').update(right).digest();
return timingSafeEqual(leftDigest, rightDigest);
}
export async function authenticatePersistenceRequest(
req: IncomingMessage,
): Promise<PersistencePrincipal | undefined> {
const token = process.env.PERSISTENCE_DEV_TOKEN;
const authorization = singleHeader(req.headers.authorization);
if (!token || !authorization || !secureEqual(authorization, `Bearer ${token}`)) return undefined;
const learnerKey = singleHeader(req.headers['x-learner-key']);
// Documents are stored without any ownership partition, so assets are
// stored under one shared principal to match: this authenticator provides
// no user isolation either way (the header is client-supplied), and a
// per-header asset partition only meant a converted document's assets
// became unreadable to every other browser the document was shared with.
// The learner key still partitions runtime sessions, which are genuinely
// per-learner state. Production replaces this module with real session
// verification and derives both from server-controlled claims.
return { key: SHARED_ASSET_PRINCIPAL, ...(learnerKey ? { learnerKey } : {}) };
}