From 3606ce65a6e24f455281e9fe68ec7969251f5d39 Mon Sep 17 00:00:00 2001 From: Ott Martens Date: Sun, 12 Nov 2023 10:00:26 +0200 Subject: [PATCH] initial setup --- .github/workflows/deploy.yml | 17 ++++++++++++ .gitignore | 4 +++ README.md | 3 ++ decap-cms-login-script.js | 23 ++++++++++++++++ index.ts | 53 ++++++++++++++++++++++++++++++++++++ package.json | 9 ++++++ wrangler.toml | 7 +++++ 7 files changed, 116 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 .gitignore create mode 100644 README.md create mode 100644 decap-cms-login-script.js create mode 100644 index.ts create mode 100644 package.json create mode 100644 wrangler.toml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..20de9e7 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,17 @@ +name: Deploy to cloudflare worker +"on": + push: + branches: + - main + workflow_dispatch: + +jobs: + deploy: + runs-on: ubuntu-latest + name: Deploy + steps: + - uses: actions/checkout@v3 + - name: Publish + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CF_API_TOKEN }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..403aa43 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.dev.vars +.wrangler + +/node_modules \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..954833c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# decap-cms-github-oauth-api-cloudflare-worker + +A github oauth authentication gateway for Decap CMS login, runnable as a cloudflare worker diff --git a/decap-cms-login-script.js b/decap-cms-login-script.js new file mode 100644 index 0000000..6bb5532 --- /dev/null +++ b/decap-cms-login-script.js @@ -0,0 +1,23 @@ +// Original version – https://github.com/vencax/netlify-cms-github-oauth-provider/blob/master/login_script.js +export default function (token) { + return ``; +} diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..6ec8ba2 --- /dev/null +++ b/index.ts @@ -0,0 +1,53 @@ +import decapCMSLoginScript from './decap-cms-login-script'; + +addEventListener('fetch', (event: any) => { + event.respondWith(handle(event.request)); +}); + +// Inserted as secrets to the worker +// @ts-ignore +const client_id = CLIENT_ID; +// @ts-ignore +const client_secret = CLIENT_SECRET; + +async function handle(request: Request) { + const { pathname, searchParams: params } = new URL(request.url); + + switch (pathname) { + case '/auth': + return redirectToAuthFlow(); + + case '/callback': + return await fetchAccessToken(params); + } +} + +async function fetchAccessToken(requestParams: URLSearchParams) { + const code = requestParams.get('code'); + + const response = await fetch('https://github.com/login/oauth/access_token', { + method: 'POST', + headers: { + 'content-type': 'application/json', + 'user-agent': 'decap-cms-github-oauth-api-cloudflare', + accept: 'application/json', + }, + body: JSON.stringify({ client_id, client_secret, code }), + }).then((res) => res.json()); + + const loginResponse = decapCMSLoginScript(response.access_token); + + return new Response(loginResponse, { + status: 201, + headers: { + 'Content-Type': 'text/html;charset=UTF-8', + }, + }); +} + +function redirectToAuthFlow() { + return Response.redirect( + `https://github.com/login/oauth/authorize?client_id=${client_id}`, + 302 + ); +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..14fa58f --- /dev/null +++ b/package.json @@ -0,0 +1,9 @@ +{ + "name": "decap-cms-github-oauth-api-cloudflare-worker", + "version": "1.0.0", + "private": true, + "description": "Github login gateway for use with Decap CMS, configured for Cloudflare Worker environment", + "main": "index.ts", + "author": "Ott Martens", + "repository": "github:ottmartens/decap-cms-github-oauth-api-cloudflare" +} \ No newline at end of file diff --git a/wrangler.toml b/wrangler.toml new file mode 100644 index 0000000..f26a307 --- /dev/null +++ b/wrangler.toml @@ -0,0 +1,7 @@ +name = "decap-cms-github-oauth-api" +main = "index.ts" + +compatibility_date = "2023-11-11" + +account_id = "c192ee4c911d964935f8f53ac8399ae9" +workers_dev = true