diff --git a/server.ts b/server.ts index 856801c..a3d3f4e 100644 --- a/server.ts +++ b/server.ts @@ -11,11 +11,51 @@ import { log } from "./src/log.ts"; const router = new Router(); 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) => { 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 = { + 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) => { ctx.response.body = { status: "ok" }; });