Skip to content

qc 0.9.0: OAuth refresh tokens - #31

Merged
stooit merged 15 commits into
mainfrom
feat/oauth-refresh-0.8
Nov 6, 2026
Merged

stooit merged 15 commits into
mainfrom
feat/oauth-refresh-0.8

Conversation

@stooit

@stooit stooit commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Release 0.9.0. qc refreshes its OAuth login with the refresh token, so the portal can issue 90-day scoped tokens to qc instead of non-expiring legacy tokens.

Changes

  • Refresh the access token within 7 days of expiry, and once after a 401. Refresh at most once per process.
  • A file lock (with stale takeover) so parallel qc processes refresh once; atomic credential saves that work with symlinks and bind mounts.
  • Lost-rotation recovery that matches the portal rule: a "just rotated" answer or a network error is an unknown outcome; qc waits 45 s and retries with the same refresh token (at most 2 times, inside 4 min). A stderr notice explains the wait.
  • whoami shows expired / remaining days.
  • Do not log the authorization code or the PKCE verifier.
  • Require Node 18 (global fetch, AbortSignal.timeout). 0.8.0 already used global fetch.

Compatibility

  • Against today's production portal (legacy tokens, no refresh_token): login and all commands work; no refresh is attempted.
  • Existing 0.8.0 logins keep working after the upgrade.
  • Against the new portal (portal develop, scoped tokens): login, refresh, 401 retry, 3 parallel processes (1 refresh), lost refresh response (recovered), revoked token (clear qc login message, no loop).

Tests

npm test: 226 passed. Build OK.

Release

After merge: tag v0.9.0 on main; publish.yml publishes to npm. Then the portal switch for quant-cli (already on portal develop) can go to production.

stooit added 15 commits November 5, 2026 20:54
…tEnvironments and getEnvironmentMetrics

The quant-client SDK uses axios, which sets error.response.status, not
error.statusCode. These three methods checked only statusCode, so a 401
showed 'Failed to fetch ...' instead of the 'run qc login' message.
The portal will issue quant-cli logins with a 90-day access token and a
rotating refresh token. qc now:

- refreshes the access token when 7 days or less are left, before the
  command runs (grant_type=refresh_token, client_id=quant-cli);
- after a 401 from the v3 API (axios interceptor) or /api/oauth/user,
  refreshes once and repeats the request once, with no loop;
- holds a lock file next to the credentials file (fs.open 'wx', 30 s
  stale timeout) while it refreshes, and reloads the stored login first.
  If another qc process already rotated the token, qc uses that result
  and sends nothing, so one refresh token is never sent twice (the portal
  revokes the whole chain on reuse);
- keeps the stored refresh token when a response has none;
- on invalid_grant/invalid_client, tells the user to run `qc login` and
  keeps the credentials file;
- writes the credentials file atomically (temp file, mode 0600, rename),
  and takes the same lock for every read-modify-write of that file.

Legacy logins have no refresh token or expiry. They are never refreshed,
and their API clients keep the default axios with no retry.
With LOG_LEVEL=debug, qc login printed the token request body, which holds
the authorization code and the code_verifier. Log only the token URL.
…ed files

- Resolve the real path of the credentials file and write the temp file
  next to it, so a symlinked file (dotfiles manager) stays a symlink and
  its target is updated.
- Retry the rename on EBUSY, EXDEV, EPERM and EACCES with a short backoff
  (antivirus and open handles on Windows). If it still fails, write the
  file in place with mode 0600 (the caller holds the lock). This keeps a
  single-file bind mount (docker -v ~/.quant/credentials:...) working.
- Every read before a save (under the lock) now throws when the file
  cannot be read or parsed. Before, it got an empty config, and the save
  dropped every other platform.
- The single-platform migration write and qc logout take the lock.
- The lock file holds a random nonce and the holder's own timestamp.
  Staleness (90 s) comes from that timestamp, not from mtime.
- A stale lock is moved to a unique name first, checked to be the one
  that was inspected, and only then removed. If another process replaced
  it, it is put back. So two waiters cannot both take one stale lock.
- Release removes the file only if this holder's nonce is still in it.
- HeldLock.verify() lets a holder check ownership before it acts.
- While held: process.on('exit') removes the lock if this process owns
  it, and SIGINT/SIGTERM wait until the locked work is done.
- If the record cannot be written, the new lock file is closed and removed.
… per process

- If the save fails after the portal rotated the tokens, retry the save
  once, use the new pair for this command, and print where the save
  failed with "run qc login if later commands fail".
- refreshIfDue runs at most once per platform in a process, so
  getEffectiveAuth and ApiClient.create never send the same refresh
  token twice, even after a failed save.
- Check lock ownership just before the refresh request.
- Refresh timeout 60 s (the portal can stall and still commits the
  rotation); "Renewing Quant login..." on stderr after 5 s.
- The reload under the lock reads strictly: a broken file stops the
  refresh instead of looking like no login.
- An ApiClient stops renewing after a renewed token also gets 401, so a
  polling loop cannot rotate the token on every request.
- qc org list goes through ApiClient.getUserInfo(), so it gets the 401
  retry.
The portal now recovers a lost rotation: the previous refresh token, sent
again within 5 minutes and before the new access token was used, gets a
new pair once.

After an unknown outcome (timeout or abort, network error, 5xx, or a 200
body that cannot be read), qc keeps the lock, waits 20 s and sends the
same refresh token once more. A 200 is saved as usual. Any other answer
keeps the earlier behaviour. There is never more than one retry, and no
retry after a definite 4xx.

Both attempts and the wait can take up to 140 s, longer than the 90 s
stale limit, so HeldLock.touch() verifies ownership and writes a new
holder timestamp before and after the wait.
The portal answers the previous refresh token with "just rotated" for
30 s after a rotation, recovers a lost rotation once from 30 s to
5 minutes, and revokes after that.

- invalid_grant "just rotated" is an unknown outcome, not a refusal: the
  token is not marked as refused and no "run qc login" warning is shown.
- After an unknown outcome, qc waits 45 s after the previous attempt
  ended and sends the same refresh token again: at most 2 retries, and
  none when 4 minutes have passed since the first send.
- Before each retry, qc reloads the credentials under the lock. If the
  stored token changed, it uses the stored tokens and sends nothing.
- The lock is touched before and after each wait. The longest gap is one
  60 s attempt, inside the 90 s stale limit. A waiter now waits up to
  6 minutes, longer than the longest hold (about 5 minutes).
- While the lock is held, a second Ctrl-C stops at once and removes the
  lock first (a signal kill emits no 'exit').
Found in the local e2e run: a dropped refresh response made qc wait 46 s
with no output.
Review of 506faad: after the wait, the window may have passed or another
process may have rotated the token, so no retry is sent.
@stooit
stooit merged commit 22b5b22 into main Nov 6, 2026
12 checks 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