{"version":3,"file":"authorize.mjs","names":[],"sources":["../../../src/services/preview-sessions/authorize.ts"],"sourcesContent":["import type { ServiceFn } from \"../../utils/services/types.js\";\nimport resolveRelationVersionType, {\n\tresolvePreviewCollectionVersionType,\n} from \"../documents/helpers/resolve-relation-version-type.js\";\nimport resolveSession from \"./helpers/resolve-session.js\";\nimport type { PreviewSessionDocumentTarget } from \"./types.js\";\n\n/**\n * Authorizes a preview token and resolves the requested collection's version.\n *\n * A scoped preview deliberately applies different access rules to its entry\n * collection and to auxiliary collections. Reads against the entry collection\n * stay locked to its document and version target. Latest and environment\n * targets remain fluid; revision and snapshot targets use their pinned version.\n * Reads against other collections are allowed so that the rendered page can load\n * supporting content such as menus, settings, or article carousels. Projectable\n * preview perspectives use configured or same-named collection mappings. Exact\n * revisions and collections without a mapping use the request's explicit version.\n *\n * The preview token is validated before either path. This distinction limits\n * access to other documents in the previewed collection without breaking pages\n * that are composed from several collections.\n */\nconst authorize: ServiceFn<\n\t[\n\t\t{\n\t\t\ttoken: string;\n\t\t\tcollectionKey: string;\n\t\t\tversionType: string;\n\t\t\tversionId?: number;\n\t\t},\n\t],\n\tPreviewSessionDocumentTarget\n> = async (context, data) => {\n\tconst sessionRes = await resolveSession(context, { token: data.token });\n\tif (sessionRes.error) return sessionRes;\n\n\tconst session = sessionRes.data;\n\tconst fallbackTarget = {\n\t\tversionType: data.versionType,\n\t\tversionId: data.versionId,\n\t};\n\tconst resolveCollectionTarget = (\n\t\tsourceVersionType: Exclude<typeof session.entry_version_type, \"revision\">,\n\t) => {\n\t\tconst versionType = resolvePreviewCollectionVersionType({\n\t\t\tconfig: context.config,\n\t\t\tsourceCollectionKey: session.entry_collection_key,\n\t\t\tsourceVersionType,\n\t\t\ttargetCollectionKey: data.collectionKey,\n\t\t\tfallbackVersionType: data.versionType,\n\t\t});\n\n\t\treturn {\n\t\t\tversionType,\n\t\t\t...(versionType === fallbackTarget.versionType &&\n\t\t\tfallbackTarget.versionId !== undefined\n\t\t\t\t? { versionId: fallbackTarget.versionId }\n\t\t\t\t: {}),\n\t\t};\n\t};\n\n\tif (session.mode === \"scoped\") {\n\t\tif (data.collectionKey === session.entry_collection_key) {\n\t\t\treturn {\n\t\t\t\terror: undefined,\n\t\t\t\tdata: {\n\t\t\t\t\tmode: \"scoped\",\n\t\t\t\t\ttarget: \"entry\",\n\t\t\t\t\tentry: {\n\t\t\t\t\t\tcollectionKey: session.entry_collection_key,\n\t\t\t\t\t\tdocumentId: session.entry_document_id,\n\t\t\t\t\t\tversionType: session.entry_version_type,\n\t\t\t\t\t\tversionId: session.entry_version_id ?? undefined,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tif (session.entry_version_type === \"revision\") {\n\t\t\treturn {\n\t\t\t\terror: undefined,\n\t\t\t\tdata: {\n\t\t\t\t\tmode: \"scoped\",\n\t\t\t\t\ttarget: \"auxiliary\",\n\t\t\t\t\tentry: {\n\t\t\t\t\t\tcollectionKey: session.entry_collection_key,\n\t\t\t\t\t\tdocumentId: session.entry_document_id,\n\t\t\t\t\t},\n\t\t\t\t\t...fallbackTarget,\n\t\t\t\t},\n\t\t\t};\n\t\t}\n\n\t\tconst relationVersionTypeRes = await resolveRelationVersionType(context, {\n\t\t\tcollectionKey: session.entry_collection_key,\n\t\t\tdocumentId: session.entry_document_id,\n\t\t\tversionType: session.entry_version_type,\n\t\t\tversionId: session.entry_version_id ?? undefined,\n\t\t});\n\t\tif (relationVersionTypeRes.error) return relationVersionTypeRes;\n\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: {\n\t\t\t\tmode: \"scoped\",\n\t\t\t\ttarget: \"auxiliary\",\n\t\t\t\tentry: {\n\t\t\t\t\tcollectionKey: session.entry_collection_key,\n\t\t\t\t\tdocumentId: session.entry_document_id,\n\t\t\t\t},\n\t\t\t\t...resolveCollectionTarget(relationVersionTypeRes.data.versionType),\n\t\t\t},\n\t\t};\n\t}\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: {\n\t\t\tmode: \"perspective\",\n\t\t\tentry: {\n\t\t\t\tcollectionKey: session.entry_collection_key,\n\t\t\t\tdocumentId: session.entry_document_id,\n\t\t\t},\n\t\t\t...resolveCollectionTarget(session.entry_version_type),\n\t\t},\n\t};\n};\n\nexport default authorize;\n"],"mappings":"6JAuBA,MAAM,EAUF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAa,MAAM,EAAe,EAAS,CAAE,MAAO,EAAK,KAAM,CAAC,EACtE,GAAI,EAAW,MAAO,OAAO,EAE7B,IAAM,EAAU,EAAW,KACrB,EAAiB,CACtB,YAAa,EAAK,YAClB,UAAW,EAAK,SACjB,EACM,EACL,GACI,CACJ,IAAM,EAAc,EAAoC,CACvD,OAAQ,EAAQ,OAChB,oBAAqB,EAAQ,qBAC7B,oBACA,oBAAqB,EAAK,cAC1B,oBAAqB,EAAK,WAC3B,CAAC,EAED,MAAO,CACN,cACA,GAAI,IAAgB,EAAe,aACnC,EAAe,YAAc,IAAA,GAC1B,CAAE,UAAW,EAAe,SAAU,EACtC,CAAC,CACL,CACD,EAEA,GAAI,EAAQ,OAAS,SAAU,CAC9B,GAAI,EAAK,gBAAkB,EAAQ,qBAClC,MAAO,CACN,MAAO,IAAA,GACP,KAAM,CACL,KAAM,SACN,OAAQ,QACR,MAAO,CACN,cAAe,EAAQ,qBACvB,WAAY,EAAQ,kBACpB,YAAa,EAAQ,mBACrB,UAAW,EAAQ,kBAAoB,IAAA,EACxC,CACD,CACD,EAGD,GAAI,EAAQ,qBAAuB,WAClC,MAAO,CACN,MAAO,IAAA,GACP,KAAM,CACL,KAAM,SACN,OAAQ,YACR,MAAO,CACN,cAAe,EAAQ,qBACvB,WAAY,EAAQ,iBACrB,EACA,GAAG,CACJ,CACD,EAGD,IAAM,EAAyB,MAAM,EAA2B,EAAS,CACxE,cAAe,EAAQ,qBACvB,WAAY,EAAQ,kBACpB,YAAa,EAAQ,mBACrB,UAAW,EAAQ,kBAAoB,IAAA,EACxC,CAAC,EAGD,OAFI,EAAuB,MAAc,EAElC,CACN,MAAO,IAAA,GACP,KAAM,CACL,KAAM,SACN,OAAQ,YACR,MAAO,CACN,cAAe,EAAQ,qBACvB,WAAY,EAAQ,iBACrB,EACA,GAAG,EAAwB,EAAuB,KAAK,WAAW,CACnE,CACD,CACD,CAEA,MAAO,CACN,MAAO,IAAA,GACP,KAAM,CACL,KAAM,cACN,MAAO,CACN,cAAe,EAAQ,qBACvB,WAAY,EAAQ,iBACrB,EACA,GAAG,EAAwB,EAAQ,kBAAkB,CACtD,CACD,CACD"}