UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

80 lines (79 loc) • 3.84 kB
import { t as MAX_DATE_TIMESTAMP_MS } from "./number-coercion-D6u-4YD8.mjs"; import "./number-runtime-W4chuk8B.mjs"; import { i as normalizeGithubCopilotOAuthScope, n as isSupportedGithubCopilotDomain, r as normalizeGithubCopilotDomain, t as PUBLIC_GITHUB_COPILOT_DOMAIN } from "./domain-CmrCAfb-.mjs"; import { t as runGitHubCopilotDeviceFlow } from "./login-D0aYkXT8.mjs"; //#region extensions/github-copilot/oauth.ts const LEGACY_OAUTH_KEY_PREFIX = "openclaw-github-copilot-oauth:v1:"; function requireSupportedEnterpriseDomain(e) { const t = e.trim() ? normalizeGithubCopilotOAuthScope(e) : void 0; if (!t) throw new Error(`Unsupported GitHub Enterprise domain "${e.trim()}". Use github.com or a *.ghe.com data-residency tenant.`); return t; } async function loginGithubCopilotOAuth(e) { const t = await e.onPrompt({ message: "GitHub Enterprise URL/domain (blank for github.com)", placeholder: "company.ghe.com", allowEmpty: true }); if (e.signal?.aborted) throw new Error("GitHub Copilot login cancelled"); const n = t.trim() ? requireSupportedEnterpriseDomain(t) : void 0; const r = n ?? "github.com"; e.onProgress?.("Waiting for GitHub authorization..."); const i = await runGitHubCopilotDeviceFlow({ showCode: async ({ verificationUrl: t, userCode: n }) => { e.onAuth({ url: t, instructions: `Enter code: ${n}` }); }, ...e.signal ? { signal: e.signal } : {} }, r); if (i.status === "access_denied") throw new Error("GitHub Copilot login cancelled"); if (i.status === "expired") throw new Error("GitHub Copilot device code expired; retry login"); return { refresh: i.accessToken, access: i.accessToken, expires: MAX_DATE_TIMESTAMP_MS, ...n ? { enterpriseUrl: n } : {} }; } function refreshGithubCopilotOAuth(e) { if (e.enterpriseUrl && !isSupportedGithubCopilotDomain(e.enterpriseUrl)) throw new Error(`Refusing to refresh GitHub Copilot OAuth for unsupported enterprise domain "${e.enterpriseUrl}". Re-authenticate with github.com or a *.ghe.com tenant.`); return { ...e, access: e.refresh, expires: MAX_DATE_TIMESTAMP_MS }; } function formatGithubCopilotApiKey(e) { if (e.type !== "oauth" || typeof e.refresh !== "string") return ""; const t = e.refresh.trim(); if (!e.enterpriseUrl) return t; const n = requireSupportedEnterpriseDomain(e.enterpriseUrl); return `${LEGACY_OAUTH_KEY_PREFIX}${JSON.stringify({ token: t, githubDomain: n })}`; } function parseGithubCopilotApiKey(e) { if (!e.startsWith(LEGACY_OAUTH_KEY_PREFIX)) return { githubToken: e }; let t; try { t = JSON.parse(e.slice(33)); } catch { throw new Error("Invalid GitHub Copilot legacy OAuth credential metadata"); } if (!t || typeof t !== "object") throw new Error("Invalid GitHub Copilot legacy OAuth credential metadata"); const { token: n, githubDomain: r } = t; if (typeof n !== "string" || !n.trim() || typeof r !== "string" || !isSupportedGithubCopilotDomain(r)) throw new Error("Invalid GitHub Copilot legacy OAuth credential metadata"); return { githubToken: n, githubDomain: normalizeGithubCopilotDomain(r) }; } function buildGithubCopilotAuthDoctorHint(e) { if (!(e.profileId ? [e.store.profiles[e.profileId]] : Object.values(e.store.profiles)).some((e) => e?.type === "oauth" && e.provider.trim().toLowerCase() === "github-copilot" && !isSupportedGithubCopilotDomain(e.enterpriseUrl))) return; return "This GitHub Copilot OAuth profile has an unsupported enterprise domain and can no longer refresh. Remove the legacy profile before re-authenticating with a supported host (github.com or a *.ghe.com tenant): openclaw models auth login --provider github-copilot --force."; } //#endregion export { refreshGithubCopilotOAuth as a, parseGithubCopilotApiKey as i, formatGithubCopilotApiKey as n, loginGithubCopilotOAuth as r, buildGithubCopilotAuthDoctorHint as t };