feat: add GitHub OAuth proxy endpoint at /auth/github
This commit is contained in:
40
server.ts
40
server.ts
@@ -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" };
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user