Skip to content

Commit

Permalink
initial setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ottmartens committed Nov 12, 2023
0 parents commit 3606ce6
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -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 }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.dev.vars
.wrangler

/node_modules
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions decap-cms-login-script.js
Original file line number Diff line number Diff line change
@@ -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 `<script>
(function() {
function receiveMessage(e) {
console.log("receiveMessage %o", e)
// send message to the main window
window.opener.postMessage(
'authorization:github:success:${JSON.stringify({
provider: 'github',
token,
})}',
e.origin
)
}
window.addEventListener("message", receiveMessage, false)
// Start handshake with parent
console.log("Sending message: %o", "github")
window.opener.postMessage("authorizing:github", "*")
})()
</script>`;
}
53 changes: 53 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -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
);
}
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 7 additions & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name = "decap-cms-github-oauth-api"
main = "index.ts"

compatibility_date = "2023-11-11"

account_id = "c192ee4c911d964935f8f53ac8399ae9"
workers_dev = true

0 comments on commit 3606ce6

Please sign in to comment.