Skip to content

Share token.json safely with other tools: flock + atomic write - #10

Merged
jcasimir merged 2 commits into
mainfrom
token-file-lock
Nov 16, 2026
Merged

jcasimir merged 2 commits into
mainfrom
token-file-lock

Conversation

@jcasimir

Copy link
Copy Markdown
Member

The Beacon Mac app will sign in through work's token file, and Zitadel rotates the refresh token on every use. If both refresh at once, one ends up holding a dead refresh token and gets signed out. This makes the file safe to share.

The contract (Beacon matches it):

  • Lock: exclusive flock(2) on token.lock next to token.json ($XDG_CONFIG_HOME/work/, default ~/.config/work/), created 0600 if absent. Held across read-check-refresh-write, saveToken (login), and work logout's delete.
  • After taking the lock, re-read token.json. If it is now valid (more than a minute left), use it and skip the network refresh.
  • Atomic write: temp file .token.json.*.tmp in the same directory, mode 0600, fsync, rename over token.json, fsync the directory.
  • Readers take no lock. A file that reads empty or won't parse is retried once after 100ms before it counts as unreadable. A missing file still means signed out.
  • If token.json has a mode other than 0600, it is set back to 0600 and a warning naming the path and old mode goes to stderr. Token contents are never logged.

The JSON schema, commands, and flags stay the same. issuer is now a var so tests can stub the token endpoint.

Tests: four goroutines and, separately, two processes refresh an expired token at the same time against a stub endpoint that rotates the refresh token. Both cases make exactly one network refresh, every caller gets the new token, and the file holds r2. A reader polling during 50 writes never sees a partial file. An empty file that gets filled in during the retry is read, not treated as signed out. A 0644 file is set back to 0600. Logout waits for the lock. With the flock call removed, the refresh and logout tests fail (4 and 2 refreshes, and logout doesn't wait).

🤖 Generated with Claude Code

jcasimir and others added 2 commits November 16, 2026 10:35
Another tool (Beacon) will sign in through the same file, and Zitadel rotates
the refresh token on every use. Refresh, save and logout now hold an exclusive
flock on token.lock; a refresh re-reads the file after taking it and skips the
network when someone else already refreshed. Writes go through a same-dir temp
file (0600, fsync, rename). Readers retry a momentarily empty or partial file
once, and a token file with loose permissions is set back to 0600 with a
warning.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jcasimir
jcasimir merged commit f24fb6d into main Nov 16, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant