Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
144 lines
4.3 KiB
JavaScript
144 lines
4.3 KiB
JavaScript
/**
|
|
* GitHub OAuth Bridge (main process)
|
|
*
|
|
* Renderer fetches to `github.com/login/*` are blocked by CORS.
|
|
* This bridge proxies GitHub Device Flow endpoints via the main process.
|
|
*/
|
|
|
|
const GITHUB_CLIENT_ID = process.env.VITE_SYNC_GITHUB_CLIENT_ID || "";
|
|
const GITHUB_DEVICE_CODE_URL = "https://github.com/login/device/code";
|
|
const GITHUB_ACCESS_TOKEN_URL = "https://github.com/login/oauth/access_token";
|
|
const GITHUB_GIST_RAW_ORIGIN = "https://gist.githubusercontent.com";
|
|
const pendingPollControllers = new Map();
|
|
|
|
function normalizeGistRawUrl(rawUrl) {
|
|
let url;
|
|
try {
|
|
url = new URL(rawUrl);
|
|
} catch {
|
|
throw new Error("Invalid GitHub Gist raw URL");
|
|
}
|
|
if (url.origin !== GITHUB_GIST_RAW_ORIGIN || url.protocol !== "https:") {
|
|
throw new Error("GitHub Gist raw URL must use gist.githubusercontent.com");
|
|
}
|
|
return url.href;
|
|
}
|
|
|
|
/**
|
|
* @param {Electron.IpcMain} ipcMain
|
|
* @param {import('electron')=} electronModule
|
|
*/
|
|
function registerHandlers(ipcMain, electronModule) {
|
|
const fetchImpl =
|
|
electronModule?.net?.fetch ? electronModule.net.fetch.bind(electronModule.net) : fetch;
|
|
|
|
ipcMain.handle("netcatty:github:deviceFlow:start", async (_event, payload) => {
|
|
const clientId = payload?.clientId || GITHUB_CLIENT_ID;
|
|
const scope = payload?.scope || "gist read:user";
|
|
|
|
const res = await fetchImpl(GITHUB_DEVICE_CODE_URL, {
|
|
method: "POST",
|
|
headers: {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
body: new URLSearchParams({
|
|
client_id: clientId,
|
|
scope,
|
|
}).toString(),
|
|
});
|
|
|
|
const text = await res.text();
|
|
if (!res.ok) {
|
|
throw new Error(`GitHub device flow failed: ${res.status} - ${text}`);
|
|
}
|
|
|
|
let data;
|
|
try {
|
|
data = JSON.parse(text);
|
|
} catch {
|
|
throw new Error(`GitHub device flow invalid JSON: ${text.slice(0, 200)}`);
|
|
}
|
|
|
|
return {
|
|
deviceCode: data.device_code,
|
|
userCode: data.user_code,
|
|
verificationUri: data.verification_uri,
|
|
expiresAt: Date.now() + (data.expires_in || 0) * 1000,
|
|
interval: data.interval || 5,
|
|
};
|
|
});
|
|
|
|
ipcMain.handle("netcatty:github:deviceFlow:poll", async (_event, payload) => {
|
|
const clientId = payload?.clientId || GITHUB_CLIENT_ID;
|
|
const deviceCode = payload?.deviceCode;
|
|
const pollId = payload?.pollId;
|
|
if (!deviceCode) throw new Error("Missing deviceCode");
|
|
|
|
const controller = new AbortController();
|
|
if (pollId) {
|
|
pendingPollControllers.set(pollId, controller);
|
|
}
|
|
|
|
try {
|
|
const res = await fetchImpl(GITHUB_ACCESS_TOKEN_URL, {
|
|
method: "POST",
|
|
signal: controller.signal,
|
|
headers: {
|
|
Accept: "application/json",
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
},
|
|
body: new URLSearchParams({
|
|
client_id: clientId,
|
|
device_code: deviceCode,
|
|
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
}).toString(),
|
|
});
|
|
|
|
const text = await res.text();
|
|
if (!res.ok) {
|
|
throw new Error(`GitHub token polling failed: ${res.status} - ${text}`);
|
|
}
|
|
|
|
try {
|
|
return JSON.parse(text);
|
|
} catch {
|
|
throw new Error(`GitHub token polling invalid JSON: ${text.slice(0, 200)}`);
|
|
}
|
|
} finally {
|
|
if (pollId) {
|
|
pendingPollControllers.delete(pollId);
|
|
}
|
|
}
|
|
});
|
|
|
|
ipcMain.handle("netcatty:github:deviceFlow:cancelPoll", async (_event, pollId) => {
|
|
if (!pollId) return;
|
|
const controller = pendingPollControllers.get(pollId);
|
|
if (!controller) return;
|
|
pendingPollControllers.delete(pollId);
|
|
controller.abort();
|
|
});
|
|
|
|
ipcMain.handle("netcatty:github:gistRawContent", async (_event, payload) => {
|
|
const accessToken = payload?.accessToken;
|
|
if (typeof accessToken !== "string" || !accessToken) {
|
|
throw new Error("Missing GitHub access token");
|
|
}
|
|
const rawUrl = normalizeGistRawUrl(payload?.rawUrl);
|
|
const res = await fetchImpl(rawUrl, {
|
|
headers: {
|
|
Authorization: `Bearer ${accessToken}`,
|
|
Accept: "application/vnd.github.raw",
|
|
},
|
|
});
|
|
const text = await res.text();
|
|
if (!res.ok) {
|
|
throw new Error(`Failed to download full gist content: ${res.status} - ${text.slice(0, 200)}`);
|
|
}
|
|
return text;
|
|
});
|
|
}
|
|
|
|
module.exports = { registerHandlers };
|