Conversation
l2ysho
force-pushed
the
claude/auth-json-v2-1419
branch
from
September 17, 2026 11:46
7a941e1 to
cc85096
Compare
auth.json was a flat blob describing one account, holding the whole
user('me') response. It is now { version, activeProfile, profiles,
secretsBackend }, so it can hold N accounts. Nothing puts a second one
there yet, and users see no change.
New src/lib/auth-file.ts owns the file: reading, an atomic write, the
v1 to v2 migration, and the profile accessors. credentials.ts, login,
logout, getLocalUserInfo() and the rental notice all go through it.
The migration backs the old file up as auth.json.v1.bak, runs after
ensureMigrated() as a separate step, is idempotent and single-flight,
and never throws. Fields nothing reads are dropped: email, plan,
effectivePlatformFeatures, isPaying, createdAt and proxy.groups.
Closes #1419
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Both parsed auth.json by hand and asserted the v1 flat shape, so neither
could pass against a v2 file. log_in_out deep-equalled the file against
the whole user('me') response, which v2 deliberately no longer stores;
info read a top-level id that is now the profile key.
Both now read the active profile through the test helper, and
log_in_out checks the token through getToken() rather than the file.
Not run here — test:api needs a live token.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
copyFileSync inherits the source mode. An auth.json written before the CLI started passing mode 0600 is still 0644, and writeFileSync's mode applies only on create, so it stayed that way. The new atomic write fixes auth.json on the first v2 write, but the backup is copied before that and never rewritten — leaving a plaintext token at 0644. Also fixes two tests: apify info prints three rows since the token source line landed, and the idempotency check called the migration twice without resetting the memoised promise, so the second call never touched the file. Adds the missing cover for logout removing the backup, which is the only path that erases that token from disk. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The backup is written once and never refreshed, and only logout deletes it. So after `apify login` as a second account, auth.json holds the new token while auth.json.v1.bak still holds the previous one — for as long as the user never logs out. Nothing reads the backup, and a downgraded CLI finds its token through the keyring or auth.json rather than here, so the secrets are dropped when writing it. Also pins the two lines that make the migration run for users. Deleting `await ensureAuthFileCurrent()` from either resolveAuth() or getLocalUserInfo() left the whole suite green: every migration test called it by hand. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
l2ysho
force-pushed
the
claude/auth-json-v2-1419
branch
from
September 23, 2026 09:14
d903722 to
0452abf
Compare
Windows has no POSIX modes. Node reports 0o666 and chmod only moves the read-only bit, so the assertion read 438 where it wanted 384. The two other mode tests in the suite already skip on win32; this one now matches them. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Moving data between shapes later needed this module reworked: the
migration was one function gated on "no version field", so the next
format change had nowhere to go. It is now a table keyed by the version
each step upgrades from, and a file runs every step from its own version
upwards. Adding a step is an entry in the table.
A failed migration now says so once instead of only under APIFY_DEBUG.
It still never blocks a command, because the readers understand the old
shape, but failing on every run should be visible.
Other fixes from review:
- Reserve AuthProfile.secretsBackend. Once secrets are keyed per
profile, a keyring failure on one profile must not silently redirect
another profile's reads to the file backend.
- ensureMigrated() skips a file a newer CLI wrote. It runs before the
shape migration reports the version, and would otherwise rewrite it.
- Write the backup through the atomic writer, the one plain write left
in a module built around temp file plus rename.
- Treat a non-object JSON payload as unusable. JSON.parse('"abc"')
succeeds and Object.keys('abc') is ['0','1','2'], so it passed both
migration guards and got replaced.
- Drop the comment calling the backup a way back. Nothing reads it and
no procedure restores it; the comment beside it already said so.
Tests:
- getLocalUserInfo() returns organizationOwnerUserId. Deleting that line
left the suite green while demoting every organization login to a
personal account.
- A pre-existing 0644 auth.json is tightened to 0600. Only temp file
plus rename does that; writeFileSync's mode applies on create only.
- The three apify run tests read the token and proxy password from disk
again. They had come to assert getLocalUserInfo() against itself.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
setActiveProfile(userId, profile, backend) read as "mark this one active". It means "make this the only account, and drop the previous one's secrets". It is now replaceStoredAccount, and the docblock says why it replaces rather than adds. Replacing is deliberate twice over. Until each profile has its own secret, a second profile would name an account that cannot authenticate. And dropping the old secrets is what makes the write safe: loginWithToken writes the new token straight after, so a failure there leaves no token at all rather than the previous account's token sitting beside the new account's name. Two tests pin that second half, which nothing covered. Making the write preserve siblings and root secrets — the shape Stage-2 will need — fails both: the old profile survives, and so does the old token. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
AuthFile carried `[k: string]: unknown`, so it typed the v1 shape, the v2 shape and an empty object identically. Reading a field that no longer exists stayed legal, which matters because #1420 moves the token and proxy password off the top level and into the profile. Measured: remove `token` from the type and the old signature reported 2 errors. It now reports 12 — every reader, across auth-file.ts and credentials.ts. Ten sites would have gone unnamed. The v1 fields move to LegacyAuthFile, which extends AuthFile with the three the flat shape carried. Only v1Profile, toV2 and the fallback in lookUpActiveProfile take it; that fallback is the one cast left, and it is where a file this CLI cannot version lands. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The version guard ran before resolveAuth read APIFY_TOKEN, so a stored file written by a newer CLI stopped every command — including ones that never read that file. A platform run or a CI job has APIFY_TOKEN as its only credential and no interest in the stored login, and `apify run` calls resolveAuth uncaught while deliberately catching the account lookup on the next line. The guard now runs on the stored-login path only. Logout was refused by the same guard, which left no way out of the state: the error offered "Upgrade the CLI" and never mentioned the file. Logout exists to discard credentials, so it no longer checks the version. A shape this CLI cannot read is discarded whole rather than edited — the old code deleted fields from it and wrote it back, which left a mangled file when it held more than one profile. The error names `apify logout` as the escape, and drops the parenthetical aside the repo's copy style does not take. Removing the auth files also passes maxRetries again. rimrafPromised carried 10 retries against Windows EBUSY when an antivirus or a second process holds the file; the bare rmSync that replaced it had none. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Three kinds went: - Restating the signature. "The parsed file, or an empty object when it is missing or unreadable" above a function that returns exactly that. - Narrating the branch. "No index signature: removing one names every reader at compile time" justifies a commit, in a place that will rot once nobody remembers there was one. - Repeated verbatim. The same three-line rationale sat above all three apify run assertions; one earns its keep. Three fields carrying the same "unused until the device flow lands" share one line now, and two of the longer blocks say the same thing shorter. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
"Could not update auth.json to the current format, so it was left as it is" reads like something broke. Nothing did — the readers understand the old shape, so the command that triggered it carries on and the next one tries again. A user with a read-only ~/.apify saw an alarming line on every command with no action to take. The message now leads with what matters to them and names the debug variable for the part that does not. Adds the failure-path test. The whole migration sits in one try/catch and nothing covered it: removing the warning left the suite green. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Two were wrong. ensureAuthFileCurrent said it brings the file "to the v2 profile shape", which predates the step chain, and that migrating "never throws" — the function does, through the version assert one line below. lookUpActiveProfile described the pre-profile read path as something that covers commands running before the migration, which reads like scaffolding; useRentalSunsetNotice calls it without migrating on purpose, so that path is permanent. The rest were restating the signature, saying the same thing in two docblocks, or taking five lines for one idea. Comment-only: the diff has no non-comment lines. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
l2ysho
marked this pull request as ready for review
September 24, 2026 10:52
auth.json.v1.bak described the account it was taken from and is never refreshed, so only logout removed it. Log in as someone else and one user's details sat on disk under another user's login. A login now discards it. Profiles carry loggedInAt. Nothing reads it — `auth list` will order by it, and a logout will fall back to the most recent profile left — but neither can backfill a time nobody recorded, so it has to be written from the start. A profile migrated from the pre-profile file gets null, because that file never held one. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Four lines for a field whose name and type say it. The reason it exists belongs in the commit that added it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This was referenced Sep 24, 2026
This branch has not been deployed
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.
Note
TL;DR
profilesshape (userIDkeys)profilesstructure)tokenattribute will be migrate in followup with token in keyring (same for proxy password)auth.json.v1.bak, removed after logoutPart of #1383 (Stage-1). Closes #1419.
What changed
New
src/lib/auth-file.tsowns the file — reading, an atomic write, the migration, and the profile accessors.credentials.ts,login,logout,getLocalUserInfo()and the rental-sunset notice all go through it. No caller parsesauth.jsonby hand any more.{ "version": 2, "activeProfile": "<userId>", "profiles": { "<userId>": { "username": "moria", "name": null, ... } }, "secretsBackend": "keyring" }token/proxy.passwordon the file backend. Secret storage v2 #1420 moves both backends together, becausedowngradeBackendToFile()can flip mid-process and a half-moved scheme loses a token.name,authMethod,expiresAt,hasRefreshToken,loggedInAt, per-profilesecretsBackend) are written and never read, so the device flow and Secret storage v2 #1420 need no extra format change.loggedInAtis recorded from the start becauseauth listwill order by it and a logout will fall back to the most recent profile left — neither can backfill a time nobody wrote down.email,plan,effectivePlatformFeatures,isPaying,createdAt,proxy.groups. Checked all 19 call sites — no readers.emailalso leaves theAuthJSONtype.getLocalUserInfo()keeps its return shape, so its call sites are untouched.0600. That also repairs a legacy0644auth.json, which the old in-place write never did.Migration
A version-step chain, so moving data between shapes later means adding a table entry rather than reworking the module. Runs after
ensureMigrated(), is single-flight, and never throws — a failure leaves the old shape, which the readers still understand, and says so once.keyringfileauth.json.v1.bak, without the secrets — it is written once and never refreshed, so a rotated token copied there would outlive the account it belongs to. A login or a logout removes it, for the same reason.idhas no key to store a profile under. The secrets stay and the next command asks for a re-login; no key is invented.apify logoutas the way out.Behavior worth calling out
apify logoutroutes through the store instead ofrimraf. It drops the active profile and its secrets, removes the backup, and never refuses — it is the only escape from a file this CLI cannot read, so a shape it does not understand is discarded whole rather than edited.APIFY_TOKENas its only credential and no interest in the stored file; checking it first would stopapify runover a file it never reads.apify-cli@1.10.0: it finds the token and its per-command refresh re-adds the flatid/username, so the file carries both shapes and both CLIs read it. No downgrade release note needed.Verification
pnpm run test:local— 665 passed, 4 skipped, up 12 from master.pnpm run lint,pnpm run format,pnpm run build— clean.pnpm run update-docs— no diff; no flag, arg, description or registration moved.36a7004d, including API Tests and Windows on Node 22 and 24.apify runinjecting the right identity,actors lsagainst the API, the installed CLI reading the migrated file, and logout clearing both files.test/local/lib/auth-file.test.tscovers states A/B/C on both backends, idempotency, the backup contents and mode, a corrupt file, a v1 file with noid, a danglingactiveProfile, aversion: 3file surviving a login and discarded by a logout, and the migration's failure path.Left out, deliberately
--profile, noauth switch/list. The file can hold N profiles; Stage-2 (Stage-2: Login - token multi account support #1386) puts a second one there, Stage-3 (Stage-3: Multi-account UX #1384) adds the commands.🤖 Generated with Claude Code