{"version":3,"file":"composed-policy.mjs","names":[],"sources":["../src/browser-integrity-challenge.ts"],"sourcesContent":["import {\n  buildDelegationChallengeBody,\n  deriveDelegationPickupUri,\n  generateDelegationSid,\n  type ChallengeWireInput,\n} from '@kya-os/checkpoint-shared/host-adapter';\nimport type { VerifyResult } from './engine/types';\nimport { browserIntegrityChallengeScopes } from './browser-posture';\n\nexport interface BrowserIntegrityChallengeOptions {\n  /** Trusted registration API origin, usually the Checkpoint dashboard. */\n  registrationOrigin: string;\n  /** Trusted origin serving this project's consent and credential pickup. */\n  authorizationOrigin: string;\n  resourceOrigin: string;\n  resourceOrgDid: string;\n  projectId: string;\n  apiKey: string;\n  timeoutMs?: number;\n}\n\n/** Register the default browser step-up through the existing consent/mint lane.\n * No organization signing key belongs in the customer middleware. Unknown\n * principals stay explicitly anonymous until the consent flow establishes them.\n * Other policy challenges remain the responsibility of the caller's resolver.\n */\nexport function createBrowserIntegrityChallengeResolver(options: BrowserIntegrityChallengeOptions) {\n  for (const value of [\n    options.registrationOrigin,\n    options.authorizationOrigin,\n    options.resourceOrigin,\n  ]) {\n    const url = new URL(value);\n    if (url.protocol !== 'https:' || url.origin !== value)\n      throw new Error('Challenge origins must be exact HTTPS origins');\n  }\n  if (!options.apiKey || !options.projectId || !/^did:(web|key):.+$/.test(options.resourceOrgDid)) {\n    throw new Error(\n      'Challenge registration requires a project API key and resource organization DID'\n    );\n  }\n  const timeoutMs = options.timeoutMs ?? 2500;\n  if (!Number.isFinite(timeoutMs) || timeoutMs <= 0)\n    throw new Error('Invalid challenge registration timeout');\n  return async ({ result }: { result: VerifyResult }): Promise<ChallengeWireInput | undefined> => {\n    const scopes = browserIntegrityChallengeScopes(result);\n    if (result.decision.kind !== 'Challenge' || !scopes?.length) return undefined;\n    const sid = generateDelegationSid();\n    const abort = new AbortController();\n    const timer = setTimeout(() => abort.abort(), timeoutMs);\n    try {\n      const response = await fetch(`${options.registrationOrigin}/api/v1/bouncer/sessions`, {\n        method: 'POST',\n        redirect: 'manual',\n        signal: abort.signal,\n        headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${options.apiKey}` },\n        body: JSON.stringify({\n          session_id: sid,\n          project_id: options.projectId,\n          created_at: Date.now(),\n          // A protocol placeholder, never a claimed or engine-verified identity.\n          agent_did: 'did:web:kya.vouched.id:principals:anonymous',\n          client_info: { name: 'checkpoint-browser-integrity' },\n          challenge: {\n            required_scopes: scopes,\n            audience: options.resourceOrgDid,\n            pep_surface: 'sdk',\n            verdict: 'challenge',\n            policy_id: result.decision.params.policyId,\n            detected_agent_name: result.detectionDetail.detectedAgent?.name,\n          },\n        }),\n      });\n      if (!response.ok) return undefined;\n      const body = (await response.json()) as {\n        data?: {\n          session_id?: string;\n          challenge?: {\n            consent_uri?: string;\n            challenge_proof?: unknown;\n          };\n        };\n      };\n      const data = body.data;\n      const consentUri = data?.challenge?.consent_uri;\n      if (\n        data?.session_id !== sid ||\n        !consentUri ||\n        new URL(consentUri).origin !== options.authorizationOrigin\n      )\n        return undefined;\n      const context: ChallengeWireInput = {\n        realm: new URL(options.resourceOrigin).host,\n        sid,\n        consentUri,\n        pickupUri: deriveDelegationPickupUri(consentUri, sid),\n        resourceMetadataUri: `${options.resourceOrigin}/.well-known/oauth-protected-resource`,\n        requiredScopes: scopes,\n        minAssurance: 'attested-bearer',\n        challengeProof: data.challenge?.challenge_proof,\n      };\n      // Validate every wire field before returning; never invent a missing proof.\n      buildDelegationChallengeBody(context);\n      return context;\n    } catch {\n      return undefined;\n    } finally {\n      clearTimeout(timer);\n    }\n  };\n}\n"],"mappings":";;;;;;;;;;AA0BA,SAAgB,wCAAwC,SAA2C;CACjG,KAAK,MAAM,SAAS;EAClB,QAAQ;EACR,QAAQ;EACR,QAAQ;CACV,GAAG;EACD,MAAM,MAAM,IAAI,IAAI,KAAK;EACzB,IAAI,IAAI,aAAa,YAAY,IAAI,WAAW,OAC9C,MAAM,IAAI,MAAM,+CAA+C;CACnE;CACA,IAAI,CAAC,QAAQ,UAAU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