Backup Worker Logins: GitHub App, Device Flow, OpenRouter and Linking
The backup worker (src/services/github-backup-worker) can log in instead of reading personal access tokens from .env:
| Login | Command | Stored as | Used for |
|---|---|---|---|
| GitHub, source | make login-github (github-backup-worker login github) | github:source | Listing and cloning the repositories to back up. With a GitHub App login, the repositories are exactly those granted to the app's installations, private ones included. |
| GitHub, destination | make login-github ARGS="--profile destination --from-gh" | github:destination | Pushing to an https:// BACKUP_REPO_PATH. SSH destinations do not need it. |
| OpenRouter | make login-openrouter (github-backup-worker login openrouter) | openrouter:default | An OpenRouter API key for scripts (github-backup-worker auth token openrouter); the worker itself does not call OpenRouter. |
| Control plane link | github-backup-worker link --control-plane URL | agent:<control plane URL> | An agent token of your account on a deployment: runs without DATABASE_URL report to it (linked mode, section 5). |
Without any stored login the worker keeps using GITHUB_TOKEN_PERSONAL and GITHUB_TOKEN_PRIVATE exactly as before. Once github:source is stored, those variables are no longer used.
1. Create the GitHub App (once per owner)
make github-app # the app belongs to your user account
make github-app ORG=my-org # or to an organization you administer
This runs github-backup-worker github-app create, which registers the app described by src/services/github-backup-worker/githubapp/manifest.json through GitHub's app manifest flow:
- A page on
http://127.0.0.1:<random port>/opens in the browser (the address is printed as well) and sends the manifest tohttps://github.com/settings/apps/new(or the organization's page) with a randomstate. - GitHub asks you to confirm. The name is prefilled as
github-backup-<6 hex digits>because app names are unique on GitHub; change it there if you like (--namesets it beforehand). - GitHub redirects back to the local page with a one-time code. A redirect whose
statedoes not match is refused. The code is exchanged (POST /app-manifests/{code}/conversions) for the app's id, slug, client id, client secret, private key and webhook secret. The id, slug, client id and client secret are saved togithub-app.jsonin the configuration directory (mode 0600, directory 0700) before the browser is told that the app exists, because GitHub shows the client secret only once. The private key and webhook secret are dropped, because the worker never uses them; if you ever need a private key, generate one in the app's settings. An existinggithub-app.jsonis only replaced with--force.
The app is read-only: repository permissions Contents: read and Metadata: read, no webhook (inactive, no events), public (so each of your accounts can install it), and "request user authorization during installation" is on.
Then do what a manifest cannot:
- Enable Device Flow. Open the settings URL the command printed (
https://github.com/settings/apps/<slug>, orhttps://github.com/organizations/<org>/settings/apps/<slug>), check Enable Device Flow under "Identifying and authorizing users", and save. - Install the app on every account whose repositories you want backed up:
https://github.com/apps/<slug>/installations/new. Choose "All repositories" or select them. Because the app requests user authorization on install, GitHub may then send the browser tohttp://127.0.0.1/callback, which does not load; close that tab.
The worker never uses the app's private key. The client secret is only sent if GitHub refuses a token refresh without it.
On other machines you do not need github-app.json: put the public client id into the repository's root .env (GITHUB_APP_CLIENT_ID=Iv23..., shown by github-backup-worker auth status on the first machine), or pass --client-id.
2. Log in to GitHub (each machine)
make login-github
The command prints https://github.com/login/device and a code such as ABCD-1234, opens the browser when there is a display, and waits while you enter the code and authorize the app (the code is valid for 15 minutes). It then records the account (GET /user), stores the login and lists the app's installations, so a missing installation shows up immediately.
- Lifetime. A GitHub App user token lasts 8 hours and is refreshed automatically before it expires; each refresh rotates the refresh token, which lasts about six months. A machine that does not run a backup for six months needs
make login-githubagain;auth statusshows the date. - Which repositories. With the default
GITHUB_DISCOVERY=auto, a GitHub App login listsGET /user/installationsand each installation's repositories (GET /user/installations/{id}/repositories) instead ofORG_ACCOUNT,PROJECT_ACCOUNTand the private listing. GitHub lists only what the logged-in account itself may access: installations on its own account and on organizations it belongs to, and within each installation the repositories it can read. The repositories of another user account are therefore visible only where this account is a collaborator; back those up with that account's own login on a machine, or withGITHUB_DISCOVERY=accountsand tokens.GITHUB_DISCOVERY=accountslistsORG_ACCOUNTandPROJECT_ACCOUNTagain (with the login's token, plus the private repositories it can see). Finding no installation fails the run instead of treating every repository as deleted. - Private destination check. Private repositories only go into a git destination that the GitHub API confirms is private, so some login must be able to read the destination repository. The worker asks with the destination login first, then the source login, then anonymously. When the destination belongs to another account (for example a separate backup account reached through an SSH alias), store that account's token as the destination login (
make login-github ARGS="--profile destination --from-gh"whileghis logged in as that account); with an SSHBACKUP_REPO_PATHit is used only for this check. - Without an app.
make login-github ARGS=--from-ghimports the GitHub CLI's token (gh auth token) as a non-expiring login. In accounts discovery it then also lists every private repository that account can see, likeGITHUB_TOKEN_PRIVATEdid.
Where the login is stored
The secret goes to the OS keychain (service github-backup, account github:source): the macOS Keychain, the Secret Service (GNOME Keyring, KWallet) on Linux, the Windows Credential Manager. credentials.json in the configuration directory indexes each login (kind, account, expiry, where the secret is) without keychain secrets, so auth status never touches the keychain. When no keychain is available (a headless Linux machine, a container), or with --store file, the secret is kept in credentials.json itself (mode 0600, directory 0700). Every change to credentials.json holds a lock on credentials.json.lock next to it, so a scheduled run that refreshes a token and a login running at the same time keep both changes.
| OS | Configuration directory |
|---|---|
| Linux | $XDG_CONFIG_HOME/github-backup, usually ~/.config/github-backup |
| macOS | ~/Library/Application Support/github-backup |
| Windows | %AppData%\github-backup |
Scheduled runs and the keychain. A scheduled run reads the login from the keychain, which must be unlocked at that moment. On Linux with lingering enabled (so the timer also runs while you are logged out, for example the catch-up run right after a boot), the login keyring is still locked and the run fails with a keychain error. On such machines log in with make login-github ARGS="--store file". The worker never falls back to the environment tokens when a stored login cannot be read, because that would list fewer repositories and mark the others missing.
3. HTTPS destination (optional)
When BACKUP_REPO_PATH is an https:// URL, the git sink pushes with the github:destination login, sent as an HTTP header scoped to that URL through GIT_CONFIG_*. The token is never in a command line, the remote URL or the clone's .git/config. Plain http:// is refused except to a loopback address.
The destination token needs write access to the destination repository. The backup app is read-only, so use the GitHub CLI's token of the account that owns the destination:
gh auth login # as the destination's owner, if needed
make login-github ARGS="--profile destination --from-gh --store file"
SSH destinations (including host aliases such as git@github.com-learning:owner/backups.git) keep using your SSH configuration and ignore this login.
4. OpenRouter (optional)
make login-openrouter # a local callback on http://localhost:<port>/callback/<nonce>
make login-openrouter ARGS=--no-browser # print the address, paste the code OpenRouter shows
~/.local/bin/github-backup-worker auth token openrouter # prints only the key
The callback path carries a random nonce, so only OpenRouter's redirect for this login reaches it (any other request gets 404). If exchanging the code fails, the page and the terminal say why and the login keeps waiting: open the printed address again to retry, or press Ctrl-C.
The login uses OpenRouter's PKCE flow (S256 challenge; the code only works with this login's verifier) and stores the new key as openrouter:default. The observatory still reads OPENROUTER_API_KEY from its own .env (possibly several comma-separated keys); to use the stored key there, export it yourself, for example OPENROUTER_API_KEY="$(~/.local/bin/github-backup-worker auth token openrouter)" make dev. make dev does not do this on its own, because a variable in the environment would replace the key list in the observatory's .env.
5. Link the worker to your account (optional)
On a deployment with several users, anyone with an account can run the worker on their own machine and see its runs in the dashboard, without a database connection: link the machine once, and every run without DATABASE_URL takes the account's run lease, reads its schedule and backup requests, and reports its logs, results and completion through the backup API's /v1 agent routes (the worker's README, section Modes, has the details).
github-backup-worker link --control-plane https://backup-api.example.com # or set GHB_CONTROL_PLANE_URL
github-backup-worker link --control-plane https://backup-api.example.com --no-browser --name nas
- The worker asks the API for a link request (
POST /v1/agent/linkwith the agent's name, by default the host name, the host name and the worker version) and prints a code such asBCDF-GHJKand the dashboard's/linkpage. It opens the page with the code filled in when there is a display and--no-browseris not given. - Sign in to the dashboard as the account this machine should back up for, check the agent name and host shown there, and approve. The code is valid for 10 minutes.
- The worker polls
POST /v1/agent/link/tokenat the interval the API names (5 s, slower afterslow_down) and receives the agent token (ghb_agt_...) once. It is stored like the logins above, underagent:<control plane URL>(keychain, or the 0600 file with--store file, which a machine that backs up while logged out needs), and never printed. A denied or expired request ends the command without storing anything.
Then run make update, so the schedule checks daily for a missed backup: setup asks the worker (auth status) whether its runs are linked, and GHB_CONTROL_PLANE_URL without a stored agent token does not count. A machine linked to exactly one control plane reports there without GHB_CONTROL_PLANE_URL; set it to the same address in the root .env to pin the control plane. DATABASE_URL wins over a link: a worker with a database stays self-hosted. github-backup-worker auth status shows which mode runs use (Runs: linked to ... as <login>).
The address must be https://; plain http:// is accepted only for localhost, 127.0.0.1 and [::1]. An agent token does not expire. Revoke it in the dashboard (Settings, Agents) when the machine is retired or lost; github-backup-worker unlink only removes the local copy.
6. Checking and removing logins
github-backup-worker auth status # logins, links, environment tokens, GitHub App, discovery and run mode; never a secret
github-backup-worker auth status --check # exit 0 when a GitHub login or token is configured
github-backup-worker logout github [--profile destination]
github-backup-worker logout openrouter
github-backup-worker unlink [--control-plane URL] # remove the agent token of a control plane
github-backup-worker logout all # every login and link, and github-app.json
make uninstall ARGS=--purge-credentials # the schedule, the binary, every login and link, and github-app.json
Removing a login does not revoke the token on GitHub: revoke GitHub App authorizations at https://github.com/settings/apps/authorizations and the GitHub CLI's token with gh auth logout. The GitHub App itself stays registered until you delete it in its settings. Removing a link does not revoke the agent either: revoke it in the dashboard.
Security notes
- Every login secret stays in the keychain or a 0600 file;
auth status, logs and errors never print it, and git errors are redacted. - The agent token travels only in the
Authorizationheader, over HTTPS (plain HTTP only to a loopback address); the worker follows no redirect from the control plane, bounds each call (15 s) and each answer (1 MiB), and never quotes an answer in an error. - The local pages of the browser logins listen on the loopback interface only and answer only requests addressed to
127.0.0.1,localhostor[::1]with their port, which blocks DNS rebinding. The manifest redirect needs the randomstate; the OpenRouter code needs the PKCE verifier. github-app.jsonholds the app's client secret (never its private key); keep it private. The worker needs only the client id.
Troubleshooting
| Message | Fix |
|---|---|
no GitHub App client id | Run make github-app once, or set GITHUB_APP_CLIENT_ID in the root .env, or use ARGS=--from-gh. |
device_flow_disabled | Enable Device Flow in the app's settings (step 1). |
incorrect_client_credentials during login | The client id is wrong (check GITHUB_APP_CLIENT_ID and github-app.json). |
the GitHub App is not installed on any account this login can access | Install the app (step 1), or set GITHUB_DISCOVERY=accounts. |
read github:source from the OS keychain: ... in a scheduled run | The keychain was locked; log in again with ARGS="--store file". |
refresh the GitHub login ... bad_refresh_token or expired on | The authorization was revoked or the refresh token expired: make login-github. |
credentials.json is not valid JSON | Move the file away and log in again. |
the link code expired before it was approved or the link request was denied | Run link again and approve the code in the dashboard within 10 minutes. |
uses plain http, which is allowed only for localhost | Use the control plane's https:// address. |
the control plane does not accept this machine's agent token | The agent was revoked or the account disabled: github-backup-worker link again. |