Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/core/cli-identity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,28 @@ export function clearSessionIdentity(
): void {
try { rmSync(sessionIdentityPath(sessionDataDir, sessionId, tool), { force: true }); }
catch { /* best-effort: absence is the desired state */ }
// Before #1543 identities lived directly under cli-identity/. Upgraded
// sessions no longer read or overwrite those files, so remove the exact
// legacy path as well instead of leaving a live token behind indefinitely.
try {
rmSync(
join(sessionIdentityDir(sessionDataDir), `${assertSafeSegment(sessionId)}.${tool}.env`),
{ force: true },
);
} catch { /* best-effort: absence is the desired state */ }
}

/** Drop every identity for a session (teardown). */
export function clearAllSessionIdentities(sessionDataDir: string, sessionId: string): void {
for (const tool of Object.keys(IDENTITY_ENV_KEYS) as TriggerUserAuthTool[]) {
clearSessionIdentity(sessionDataDir, sessionId, tool);
}
try {
rmSync(
join(sessionIdentityDir(sessionDataDir), `${assertSafeSegment(sessionId)}.turn`),
{ force: true },
);
} catch { /* best-effort: absence is the desired state */ }
}

/**
Expand Down
35 changes: 35 additions & 0 deletions test/cli-identity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,48 @@ describe('clearSessionIdentity', () => {
expect(existsSync(sessionIdentityPath(dir, SESSION, 'bytedcli'))).toBe(true);
});

it('removes the matching pre-#1543 identity without disturbing other legacy files', () => {
const identityDir = join(dir, 'cli-identity');
mkdirSync(identityDir, { recursive: true });
const stale = join(identityDir, `${SESSION}.lark-cli.env`);
const otherTool = join(identityDir, `${SESSION}.bytedcli.env`);
const otherSession = join(identityDir, 'sess-other.lark-cli.env');
writeFileSync(stale, 'live-token');
writeFileSync(otherTool, 'other-tool-token');
writeFileSync(otherSession, 'other-session-token');

clearSessionIdentity(dir, SESSION, 'lark-cli');

expect(existsSync(stale)).toBe(false);
expect(existsSync(otherTool)).toBe(true);
expect(existsSync(otherSession)).toBe(true);
});

it('clears every tool on teardown', () => {
writeSessionIdentity(dir, SESSION, { tool: 'lark-cli', appId: 'a', userAccessToken: 'tok' });
writeSessionIdentity(dir, SESSION, { tool: 'bytedcli', cloudJwt: 'jwt' });
clearAllSessionIdentities(dir, SESSION);
expect(existsSync(sessionIdentityPath(dir, SESSION, 'lark-cli'))).toBe(false);
expect(existsSync(sessionIdentityPath(dir, SESSION, 'bytedcli'))).toBe(false);
});

it('clears every pre-#1543 identity and turn marker on teardown', () => {
const identityDir = join(dir, 'cli-identity');
mkdirSync(identityDir, { recursive: true });
const legacyPaths = [
join(identityDir, `${SESSION}.lark-cli.env`),
join(identityDir, `${SESSION}.bytedcli.env`),
join(identityDir, `${SESSION}.turn`),
];
for (const path of legacyPaths) writeFileSync(path, 'stale');
const unrelated = join(identityDir, `${SESSION}.unknown.env`);
writeFileSync(unrelated, 'keep');

clearAllSessionIdentities(dir, SESSION);

for (const path of legacyPaths) expect(existsSync(path)).toBe(false);
expect(existsSync(unrelated)).toBe(true);
});
});

// The wrapper is what actually runs, on every CLI call. These tests execute it
Expand Down
Loading