A Cloudflare Worker that lets Progressive Web Office sign in to GitHub with the device flow of its GitHub App (requirement GIT-022), from the browser, with no secret anywhere.
GitHub's device flow needs no client secret, but its two endpoints send no CORS headers: a web page cannot call them. The relay forwards exactly those two requests and adds the headers that let the application read the answer:
| Relay | GitHub | Fields forwarded |
|---|---|---|
POST /device/code |
https://github.com/login/device/code |
client_id, scope |
POST /device/token |
https://github.com/login/oauth/access_token |
client_id, device_code, grant_type (the device grant only) |
It refuses any other path, method, origin (ALLOWED_ORIGINS), application
(CLIENT_ID) or grant, and bodies over 4 KB. It holds no secret, stores
nothing, and logs nothing (observability off). The token GitHub gives passes
through it once, to the browser that asked for it.
- Progressive Web Office asks the relay for a code and shows it, with a link to https://github.com/login/device.
- The user types the code on github.com and approves the application, then chooses on GitHub the repositories it may use (by installing the App).
- Progressive Web Office polls the relay until GitHub gives the token, then keeps it in the browser, as for an access token typed by hand.
-
The GitHub App (in the organisation's settings, Developer settings → GitHub Apps → New GitHub App):
- name Progressive Web Office, home page the application's address;
- Callback URL: none needed; Enable Device Flow: ticked;
- Expire user authorization tokens: unticked (a token can then be used until it is revoked: refreshing one would need the App's secret, which the relay does not hold);
- Webhook: inactive;
- permissions (Repository): Contents read and write, Pull requests read and write, Metadata read (added by GitHub);
- Where can this GitHub App be installed?: any account.
Copy its Client ID into
CLIENT_IDinwrangler.toml. -
Cloudflare: a free account; create an API token with the Edit Cloudflare Workers template, and note the account id.
-
This repository's secrets (Settings → Secrets and variables → Actions):
CLOUDFLARE_API_TOKENandCLOUDFLARE_ACCOUNT_ID. -
Push to
main: the Deploy workflow tests, then deploys the Worker tohttps://pwo-github-relay.<account>.workers.dev. Give that address to Progressive Web Office.
The free plan of Cloudflare Workers allows 100,000 requests a day; a sign-in takes a few requests (one code, then one poll every five seconds until the user approves).
just test # unit tests (Vitest)
just typecheck # TypeScript
just dev # the Worker on http://localhost:8787 (wrangler dev)AGPL-3.0-or-later, like Progressive Web Office. See REUSE.toml.