{"version":3,"sources":["../../src/server/session-auth.ts"],"sourcesContent":["import type { SessionAuthAdapter, SessionAuthIssueResult } from '@saraudio/core';\n\nexport interface SonioxSessionAdapterOptions {\n  /** Provide server API key directly (otherwise read from process.env.SONIOX_API_KEY). */\n  apiKey?: string;\n  /** Desired TTL in seconds (1..3600); provider may clamp. Default 30s. */\n  ttlSeconds?: number;\n}\n\nexport function sessionAuthAdapter(options: SonioxSessionAdapterOptions = {}): SessionAuthAdapter {\n  const id = 'soniox';\n  const envKey = process.env.SONIOX_API_KEY ?? process.env.NUXT_SONIOX_API_KEY; // common env names\n  const apiKey = options.apiKey ?? envKey ?? '';\n\n  interface TempKeyResponse {\n    api_key?: string;\n    expires_at?: string; // RFC3339\n    message?: string;\n  }\n\n  async function parseJson<T>(res: Response): Promise<T | null> {\n    try {\n      return (await res.json()) as T;\n    } catch {\n      return null;\n    }\n  }\n\n  return {\n    id,\n    async issue(input) {\n      const ttl = Math.max(1, Math.min(3600, input?.ttlSeconds ?? options.ttlSeconds ?? 30));\n      const url = 'https://api.soniox.com/v1/auth/temporary-api-key';\n      const headers: HeadersInit = {\n        Authorization: `Bearer ${apiKey}`,\n        'Content-Type': 'application/json',\n      };\n      const body = JSON.stringify({ usage_type: 'transcribe_websocket', expires_in_seconds: ttl });\n      const res = await fetch(url, { method: 'POST', headers, body });\n      const json = await parseJson<TempKeyResponse>(res);\n      if (!res.ok) {\n        const msg = json?.message ?? 'Soniox temporary key failed';\n        throw new Error(`${msg} (status ${res.status})`);\n      }\n      const token = json?.api_key;\n      const expiresAt = json?.expires_at;\n      if (typeof token !== 'string' || token.length === 0) throw new Error('Soniox temporary key is empty');\n      let expiresIn = 28; // safe default\n      if (typeof expiresAt === 'string') {\n        const ms = Date.parse(expiresAt) - Date.now();\n        if (Number.isFinite(ms) && ms > 0) expiresIn = Math.max(1, Math.floor(ms / 1000));\n      }\n      const result: SessionAuthIssueResult = { token, expiresIn, provider: id };\n      return result;\n    },\n  };\n}\n"],"mappings":";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