From 936000b32a46e445fe9227bf8b0308fd7e8a16f7 Mon Sep 17 00:00:00 2001 From: mouad bouras Date: Fri, 3 Apr 2026 15:17:00 -0400 Subject: [PATCH] add gh app --- functions/api/api.ts | 27 ++++++++++++++ functions/api/callback.ts | 74 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 functions/api/api.ts create mode 100644 functions/api/callback.ts diff --git a/functions/api/api.ts b/functions/api/api.ts new file mode 100644 index 0000000..8335ce8 --- /dev/null +++ b/functions/api/api.ts @@ -0,0 +1,27 @@ +export async function onRequest(context: any) { + const { + request, // same as existing Worker API + env, // same as existing Worker API + } = context; + + const client_id = (env as any).GITHUB_CLIENT_ID; + + try { + const url = new URL(request.url); + const redirectUrl = new URL('https://github.com/login/oauth/authorize'); + redirectUrl.searchParams.set('client_id', client_id); + redirectUrl.searchParams.set('redirect_uri', url.origin + '/api/callback'); + redirectUrl.searchParams.set('scope', 'repo user'); + redirectUrl.searchParams.set( + 'state', + crypto.getRandomValues(new Uint8Array(12)).join(''), + ); + return Response.redirect(redirectUrl.href, 301); + + } catch (error: any) { + console.error(error); + return new Response(error.message, { + status: 500, + }); + } +} \ No newline at end of file diff --git a/functions/api/callback.ts b/functions/api/callback.ts new file mode 100644 index 0000000..e116300 --- /dev/null +++ b/functions/api/callback.ts @@ -0,0 +1,74 @@ +function renderBody(status: string, content: any) { + const html = ` + + `; + const blob = new Blob([html]); + return blob; +} + +export async function onRequest(context: any) { + const { + request, // same as existing Worker API + env, // same as existing Worker API + } = context; + + const client_id = env.GITHUB_CLIENT_ID; + const client_secret = env.GITHUB_CLIENT_SECRET; + + try { + const url = new URL(request.url); + const code = url.searchParams.get('code'); + const response = await fetch( + 'https://github.com/login/oauth/access_token', + { + method: 'POST', + headers: { + 'content-type': 'application/json', + 'user-agent': 'cloudflare-functions-github-oauth-login-demo', + 'accept': 'application/json', + }, + body: JSON.stringify({ client_id, client_secret, code }), + }, + ); + const result = (await response.json()) as any; + if (result.error) { + return new Response(renderBody('error', result), { + headers: { + 'content-type': 'text/html;charset=UTF-8', + }, + status: 401 + }); + } + const token = result.access_token; + const provider = 'github'; + const responseBody = renderBody('success', { + token, + provider, + }); + return new Response(responseBody, { + headers: { + 'content-type': 'text/html;charset=UTF-8', + }, + status: 200 + }); + + } catch (error: any) { + console.error(error); + return new Response(error.message, { + headers: { + 'content-type': 'text/html;charset=UTF-8', + }, + status: 500, + }); + } +} \ No newline at end of file