-
Notifications
You must be signed in to change notification settings - Fork 0
fix(cli): serialize token refresh across processes #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "time" | ||
|
|
||
| "github.com/gofrs/flock" | ||
| ) | ||
|
|
||
| const lockRetryInterval = 100 * time.Millisecond | ||
|
|
||
| // tokenStoreLock wraps a process-level advisory lock acquired via flock. | ||
| type tokenStoreLock struct { | ||
| fl *flock.Flock | ||
| } | ||
|
|
||
| // Close releases the advisory lock. | ||
| func (l *tokenStoreLock) Close() error { | ||
| return l.fl.Unlock() | ||
| } | ||
|
|
||
| // lockTokenStore acquires a cross-process advisory lock scoped to | ||
| // (tokenFile, clientID). It serialises the "load → refresh → save" | ||
| // critical section so concurrent CLI invocations cannot spend the same | ||
| // refresh token twice (which would yield invalid_grant on rotation servers). | ||
| // | ||
| // The lock sits next to the token file regardless of the active backend — | ||
| // keyring-backed runs also need the coordination because the race is in the | ||
| // refresh flow, not the storage layer. | ||
| func lockTokenStore(ctx context.Context, tokenFile, clientID string) (io.Closer, error) { | ||
| if tokenFile == "" { | ||
| return nil, errors.New("lock: tokenFile is empty") | ||
| } | ||
| if clientID == "" { | ||
| return nil, errors.New("lock: clientID is empty") | ||
| } | ||
|
|
||
| dir := filepath.Dir(tokenFile) | ||
| if err := os.MkdirAll(dir, 0o700); err != nil { | ||
| return nil, fmt.Errorf("create lock directory %q: %w", dir, err) | ||
| } | ||
|
|
||
| lockPath := filepath.Join(dir, filepath.Base(tokenFile)+"."+clientID+".lock") | ||
| fl := flock.New(lockPath) | ||
| locked, err := fl.TryLockContext(ctx, lockRetryInterval) | ||
|
Comment on lines
+48
to
+50
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("acquire lock %s: %w", lockPath, err) | ||
| } | ||
| if !locked { | ||
| return nil, fmt.Errorf("could not acquire lock %s", lockPath) | ||
| } | ||
| return &tokenStoreLock{fl: fl}, nil | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lock placement currently depends on
cfg.TokenFile, whose default is a relative path (.authgate-tokens.json). Forkeyring/auto(keyring available) this introduces a new requirement that the current working directory be writable (so the lock file can be created), otherwise refresh will fail even though the keyring backend itself doesn’t need filesystem writes. Consider choosing a lock location that’s reliably writable (e.g., underos.UserCacheDir()/os.UserConfigDir()) when the token file path is relative or when using keyring storage.