Repository navigation
Share token.json safely with other tools: flock + atomic write - #10
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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):
flock(2)ontoken.locknext totoken.json($XDG_CONFIG_HOME/work/, default~/.config/work/), created 0600 if absent. Held across read-check-refresh-write,saveToken(login), andwork logout's delete.token.json. If it is now valid (more than a minute left), use it and skip the network refresh..token.json.*.tmpin the same directory, mode 0600, fsync, rename overtoken.json, fsync the directory.token.jsonhas 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.
issueris now avarso 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 theflockcall removed, the refresh and logout tests fail (4 and 2 refreshes, and logout doesn't wait).🤖 Generated with Claude Code