feat: add GitHub OAuth proxy endpoint at /auth/github

This commit is contained in:
Julien Calixte
2026-03-21 10:35:49 +01:00
parent 32ee135099
commit 92e0dbe0d4

View File

@@ -11,11 +11,51 @@ import { log } from "./src/log.ts";
const router = new Router(); const router = new Router();
const PAGINATION = 20; const PAGINATION = 20;
const GITHUB_CLIENT_ID = Deno.env.get("GITHUB_CLIENT_ID") ?? "";
const GITHUB_CLIENT_SECRET = Deno.env.get("GITHUB_CLIENT_SECRET") ?? "";
router.get("/", (ctx) => { router.get("/", (ctx) => {
ctx.response.body = "Hello world"; ctx.response.body = "Hello world";
}); });
router.get("/auth/github", async (ctx) => {
const code = ctx.request.url.searchParams.get("code");
const type = ctx.request.url.searchParams.get("type");
if (!code) {
ctx.response.status = 400;
ctx.response.body = { error: "code is required" };
return;
}
const params: Record<string, string> = {
client_id: GITHUB_CLIENT_ID,
client_secret: GITHUB_CLIENT_SECRET,
};
if (type === "refresh") {
params.grant_type = "refresh_token";
params.refresh_token = code;
} else {
params.code = code;
}
const response = await fetch(
"https://github.com/login/oauth/access_token",
{
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(params),
},
);
ctx.response.status = response.status;
ctx.response.body = await response.json();
});
router.get("/health", (ctx) => { router.get("/health", (ctx) => {
ctx.response.body = { status: "ok" }; ctx.response.body = { status: "ok" };
}); });