Harden managed auth token read-back
Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -187,13 +187,9 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
expect(readFileSync(runtimeCredentialsPath, 'utf-8')).toBe('{"token":"managed"}\n')
|
||||
})
|
||||
|
||||
it('falls back to atomic write when the unchanged check cannot read the target', async () => {
|
||||
if (process.platform === 'win32') {
|
||||
return
|
||||
}
|
||||
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
it('removes runtime credentials when deselecting with a missing system-default snapshot', async () => {
|
||||
const runtimeCredentialsPath = join(testState.fakeHomeDir, '.claude', '.credentials.json')
|
||||
writeFileSync(runtimeCredentialsPath, '{"token":"managed"}\n', 'utf-8')
|
||||
const managedAuthPath = createManagedClaudeAuth(
|
||||
testState.userDataDir,
|
||||
'account-1',
|
||||
@@ -207,6 +203,36 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
|
||||
settings.activeClaudeManagedAccountId = null
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
expect(existsSync(runtimeCredentialsPath)).toBe(false)
|
||||
if (process.platform === 'darwin') {
|
||||
expect(testState.activeKeychainCredentials).toBeNull()
|
||||
}
|
||||
})
|
||||
|
||||
it('falls back to atomic write when the unchanged check cannot read the target', async () => {
|
||||
if (process.platform === 'win32') {
|
||||
return
|
||||
}
|
||||
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
const runtimeCredentialsPath = join(testState.fakeHomeDir, '.claude', '.credentials.json')
|
||||
const managedAuthPath = createManagedClaudeAuth(
|
||||
testState.userDataDir,
|
||||
'account-1',
|
||||
'{"token":"managed"}\n'
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
testState.managedKeychainCredentials.set('account-1', '{"token":"rotated"}\n')
|
||||
@@ -236,13 +262,13 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
'{"token":"managed"}\n'
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
chmodSync(runtimeCredentialsPath, 0o644)
|
||||
await service.syncForCurrentSelection()
|
||||
@@ -259,6 +285,33 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
'account-1',
|
||||
originalCredentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, refreshedCredentials, 'utf-8')
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
expect(readManagedCredentialsForTest('account-1', managedAuthPath)).toBe(refreshedCredentials)
|
||||
expect(readFileSync(runtimeCredentialsPath, 'utf-8')).toBe(refreshedCredentials)
|
||||
})
|
||||
|
||||
it('reads back verified same-account credentials on first sync after restart', async () => {
|
||||
const runtimeCredentialsPath = join(testState.fakeHomeDir, '.claude', '.credentials.json')
|
||||
const originalCredentials = createClaudeCredentialsJson('user@example.com', 'original')
|
||||
const refreshedCredentials = createClaudeCredentialsJson('user@example.com', 'refreshed')
|
||||
writeFileSync(runtimeCredentialsPath, refreshedCredentials, 'utf-8')
|
||||
const managedAuthPath = createManagedClaudeAuth(
|
||||
testState.userDataDir,
|
||||
'account-1',
|
||||
originalCredentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
@@ -269,9 +322,6 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, refreshedCredentials, 'utf-8')
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
expect(readManagedCredentialsForTest('account-1', managedAuthPath)).toBe(refreshedCredentials)
|
||||
expect(readFileSync(runtimeCredentialsPath, 'utf-8')).toBe(refreshedCredentials)
|
||||
})
|
||||
@@ -286,13 +336,13 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
selectedCredentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, staleCredentials, 'utf-8')
|
||||
@@ -340,13 +390,13 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
selectedCredentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, staleCredentials, 'utf-8')
|
||||
@@ -450,13 +500,13 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
originalCredentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, refreshedCredentials, 'utf-8')
|
||||
@@ -484,13 +534,13 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
createClaudeAccount('account-1', managedAuthPath, {
|
||||
organizationUuid: 'org-1'
|
||||
})
|
||||
],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, externalCredentials, 'utf-8')
|
||||
@@ -515,13 +565,13 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [
|
||||
createClaudeAccount('account-1', managedAuthPath, { email: 'selected@example.com' })
|
||||
],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, staleCredentials, 'utf-8')
|
||||
@@ -569,6 +619,91 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
expect(readFileSync(runtimeCredentialsPath, 'utf-8')).toBe(account2Credentials)
|
||||
})
|
||||
|
||||
it('does not carry the reauth read-back skip across Claude account switches', async () => {
|
||||
const runtimeCredentialsPath = join(testState.fakeHomeDir, '.claude', '.credentials.json')
|
||||
const account1Credentials = createClaudeCredentialsJson('one@example.com', 'one')
|
||||
const account2Credentials = createClaudeCredentialsJson('two@example.com', 'two')
|
||||
const account2RefreshedCredentials = createClaudeCredentialsJson(
|
||||
'two@example.com',
|
||||
'two-refreshed'
|
||||
)
|
||||
const managedAuthPath1 = createManagedClaudeAuth(
|
||||
testState.userDataDir,
|
||||
'account-1',
|
||||
account1Credentials
|
||||
)
|
||||
const managedAuthPath2 = createManagedClaudeAuth(
|
||||
testState.userDataDir,
|
||||
'account-2',
|
||||
account2Credentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [
|
||||
createClaudeAccount('account-1', managedAuthPath1, { email: 'one@example.com' }),
|
||||
createClaudeAccount('account-2', managedAuthPath2, { email: 'two@example.com' })
|
||||
],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
service.clearLastWrittenCredentialsJson()
|
||||
settings.activeClaudeManagedAccountId = 'account-2'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, account2RefreshedCredentials, 'utf-8')
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
expect(readManagedCredentialsForTest('account-2', managedAuthPath2)).toBe(
|
||||
account2RefreshedCredentials
|
||||
)
|
||||
expect(readFileSync(runtimeCredentialsPath, 'utf-8')).toBe(account2RefreshedCredentials)
|
||||
})
|
||||
|
||||
it('does not apply inactive-account Claude reauth skip to the active account', async () => {
|
||||
const runtimeCredentialsPath = join(testState.fakeHomeDir, '.claude', '.credentials.json')
|
||||
const account1Credentials = createClaudeCredentialsJson('one@example.com', 'one')
|
||||
const account1RefreshedCredentials = createClaudeCredentialsJson(
|
||||
'one@example.com',
|
||||
'one-refreshed'
|
||||
)
|
||||
const account2Credentials = createClaudeCredentialsJson('two@example.com', 'two')
|
||||
const managedAuthPath1 = createManagedClaudeAuth(
|
||||
testState.userDataDir,
|
||||
'account-1',
|
||||
account1Credentials
|
||||
)
|
||||
const managedAuthPath2 = createManagedClaudeAuth(
|
||||
testState.userDataDir,
|
||||
'account-2',
|
||||
account2Credentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [
|
||||
createClaudeAccount('account-1', managedAuthPath1, { email: 'one@example.com' }),
|
||||
createClaudeAccount('account-2', managedAuthPath2, { email: 'two@example.com' })
|
||||
],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeCredentialsPath, account1RefreshedCredentials, 'utf-8')
|
||||
service.clearLastWrittenCredentialsJson('account-2')
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
expect(readManagedCredentialsForTest('account-1', managedAuthPath1)).toBe(
|
||||
account1RefreshedCredentials
|
||||
)
|
||||
expect(readFileSync(runtimeCredentialsPath, 'utf-8')).toBe(account1RefreshedCredentials)
|
||||
})
|
||||
|
||||
it('keeps external Claude logout when deselecting managed account', async () => {
|
||||
const runtimeCredentialsPath = join(testState.fakeHomeDir, '.claude', '.credentials.json')
|
||||
const systemCredentials = createClaudeCredentialsJson('system@example.com', 'system')
|
||||
@@ -580,13 +715,13 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
managedCredentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
rmSync(runtimeCredentialsPath, { force: true })
|
||||
@@ -647,13 +782,13 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
managedCredentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
settings.activeClaudeManagedAccountId = 'account-1'
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
testState.activeKeychainCredentials = externalKeychainCredentials
|
||||
@@ -747,4 +882,32 @@ describe('ClaudeRuntimeAuthService', () => {
|
||||
expect(preparation.stripAuthEnv).toBe(false)
|
||||
expect(preparation.provenance).toBe('system')
|
||||
})
|
||||
|
||||
it('does not clobber fresh Claude credentials after clearLastWrittenCredentialsJson', async () => {
|
||||
const runtimeCredentialsPath = join(testState.fakeHomeDir, '.claude', '.credentials.json')
|
||||
const originalCredentials = createClaudeCredentialsJson('user@example.com', 'original')
|
||||
const reauthedCredentials = createClaudeCredentialsJson('user@example.com', 'reauthed')
|
||||
const managedAuthPath = createManagedClaudeAuth(
|
||||
testState.userDataDir,
|
||||
'account-1',
|
||||
originalCredentials
|
||||
)
|
||||
const settings = createSettings({
|
||||
claudeManagedAccounts: [createClaudeAccount('account-1', managedAuthPath)],
|
||||
activeClaudeManagedAccountId: 'account-1'
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { ClaudeRuntimeAuthService } = await import('./runtime-auth-service')
|
||||
const service = new ClaudeRuntimeAuthService(store as never)
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
testState.managedKeychainCredentials.set('account-1', reauthedCredentials)
|
||||
writeFileSync(join(managedAuthPath, '.credentials.json'), reauthedCredentials, 'utf-8')
|
||||
service.clearLastWrittenCredentialsJson()
|
||||
await service.syncForCurrentSelection()
|
||||
|
||||
expect(readManagedCredentialsForTest('account-1', managedAuthPath)).toBe(reauthedCredentials)
|
||||
expect(readFileSync(runtimeCredentialsPath, 'utf-8')).toBe(reauthedCredentials)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -46,6 +46,7 @@ export class ClaudeRuntimeAuthService {
|
||||
// an external login (e.g. `claude auth login`) overwrote it — so Orca adopts
|
||||
// the file as the new system default instead of restoring a stale snapshot.
|
||||
private lastWrittenCredentialsJson: string | null = null
|
||||
private skipNextReadBackForAccountId: string | null = null
|
||||
|
||||
constructor(private readonly store: Store) {
|
||||
this.initializeLastSyncedState()
|
||||
@@ -130,8 +131,6 @@ export class ClaudeRuntimeAuthService {
|
||||
return
|
||||
}
|
||||
|
||||
await this.captureSystemDefaultSnapshot({ force: this.lastSyncedAccountId === null })
|
||||
|
||||
let credentialsJson = await this.readManagedCredentials(activeAccount)
|
||||
if (!credentialsJson) {
|
||||
console.warn(
|
||||
@@ -145,19 +144,30 @@ export class ClaudeRuntimeAuthService {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.lastSyncedAccountId === null) {
|
||||
await this.captureSystemDefaultSnapshot({ force: true })
|
||||
}
|
||||
|
||||
// Why: Claude CLI refreshes expired OAuth tokens and writes them back to
|
||||
// .credentials.json. If we detect the runtime file differs from what Orca
|
||||
// last wrote, the CLI must have refreshed — so we preserve those tokens
|
||||
// back to managed storage before overwriting runtime with managed state.
|
||||
if (this.lastSyncedAccountId === activeAccount.id) {
|
||||
const readBackResult = await this.readBackRefreshedTokens(activeAccount, credentialsJson, {
|
||||
updateLastWrittenCredentialsJson: true
|
||||
})
|
||||
if (readBackResult.status === 'persisted') {
|
||||
credentialsJson = (await this.readManagedCredentials(activeAccount)) ?? credentialsJson
|
||||
if (this.skipNextReadBackForAccountId === activeAccount.id) {
|
||||
this.skipNextReadBackForAccountId = null
|
||||
} else {
|
||||
const readBackResult = await this.readBackRefreshedTokens(activeAccount, credentialsJson, {
|
||||
updateLastWrittenCredentialsJson: true
|
||||
})
|
||||
if (readBackResult.status === 'persisted') {
|
||||
credentialsJson = (await this.readManagedCredentials(activeAccount)) ?? credentialsJson
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.lastSyncedAccountId !== activeAccount.id) {
|
||||
this.skipNextReadBackForAccountId = null
|
||||
}
|
||||
this.writeRuntimeCredentials(credentialsJson)
|
||||
if (process.platform === 'darwin') {
|
||||
await writeActiveClaudeKeychainCredentials(credentialsJson)
|
||||
@@ -170,8 +180,11 @@ export class ClaudeRuntimeAuthService {
|
||||
// re-auth or add-account. Those flows write fresh tokens to managed storage,
|
||||
// so the read-back must be skipped to avoid overwriting them with stale
|
||||
// runtime tokens.
|
||||
clearLastWrittenCredentialsJson(): void {
|
||||
clearLastWrittenCredentialsJson(
|
||||
accountId = this.store.getSettings().activeClaudeManagedAccountId
|
||||
): void {
|
||||
this.lastWrittenCredentialsJson = null
|
||||
this.skipNextReadBackForAccountId = accountId
|
||||
}
|
||||
|
||||
private async readBackRefreshedTokens(
|
||||
@@ -180,15 +193,14 @@ export class ClaudeRuntimeAuthService {
|
||||
options: { updateLastWrittenCredentialsJson: boolean }
|
||||
): Promise<ClaudeReadBackResult> {
|
||||
try {
|
||||
if (this.lastWrittenCredentialsJson === null) {
|
||||
return { status: 'unchanged' }
|
||||
}
|
||||
|
||||
const runtimeContents = await this.readRuntimeCredentialsForReadBack()
|
||||
const runtimeContents = await this.readRuntimeCredentialsForReadBack(managedCredentialsJson)
|
||||
if (!runtimeContents) {
|
||||
return { status: 'unchanged' }
|
||||
}
|
||||
if (runtimeContents === this.lastWrittenCredentialsJson) {
|
||||
if (
|
||||
this.lastWrittenCredentialsJson !== null &&
|
||||
runtimeContents === this.lastWrittenCredentialsJson
|
||||
) {
|
||||
return { status: 'unchanged' }
|
||||
}
|
||||
|
||||
@@ -223,18 +235,32 @@ export class ClaudeRuntimeAuthService {
|
||||
}
|
||||
}
|
||||
|
||||
private async readRuntimeCredentialsForReadBack(): Promise<string | null> {
|
||||
private async readRuntimeCredentialsForReadBack(
|
||||
managedCredentialsJson: string
|
||||
): Promise<string | null> {
|
||||
const paths = this.pathResolver.getRuntimePaths()
|
||||
const fileCredentials = existsSync(paths.credentialsPath)
|
||||
? readFileSync(paths.credentialsPath, 'utf-8')
|
||||
: null
|
||||
if (process.platform === 'darwin') {
|
||||
const keychainCredentials = await readActiveClaudeKeychainCredentials()
|
||||
if (this.lastWrittenCredentialsJson === null) {
|
||||
if (keychainCredentials && keychainCredentials !== managedCredentialsJson) {
|
||||
return keychainCredentials
|
||||
}
|
||||
if (fileCredentials && fileCredentials !== managedCredentialsJson) {
|
||||
return fileCredentials
|
||||
}
|
||||
return keychainCredentials ?? fileCredentials
|
||||
}
|
||||
if (keychainCredentials && keychainCredentials !== this.lastWrittenCredentialsJson) {
|
||||
return keychainCredentials
|
||||
}
|
||||
}
|
||||
const paths = this.pathResolver.getRuntimePaths()
|
||||
if (!existsSync(paths.credentialsPath)) {
|
||||
if (!fileCredentials) {
|
||||
return null
|
||||
}
|
||||
return readFileSync(paths.credentialsPath, 'utf-8')
|
||||
return fileCredentials
|
||||
}
|
||||
|
||||
private async readBackRefreshedTokensForAccount(
|
||||
@@ -425,6 +451,11 @@ export class ClaudeRuntimeAuthService {
|
||||
|
||||
const snapshotPath = this.getSystemDefaultSnapshotPath()
|
||||
if (!existsSync(snapshotPath)) {
|
||||
rmSync(this.pathResolver.getRuntimePaths().credentialsPath, { force: true })
|
||||
if (process.platform === 'darwin') {
|
||||
await deleteActiveClaudeKeychainCredentials()
|
||||
}
|
||||
this.lastWrittenCredentialsJson = null
|
||||
return
|
||||
}
|
||||
const snapshot = JSON.parse(readFileSync(snapshotPath, 'utf-8')) as ClaudeSystemDefaultSnapshot
|
||||
|
||||
@@ -115,7 +115,7 @@ export class ClaudeAccountService {
|
||||
claudeManagedAccounts: [...previousSettings.claudeManagedAccounts, account],
|
||||
activeClaudeManagedAccountId: account.id
|
||||
})
|
||||
this.runtimeAuth.clearLastWrittenCredentialsJson()
|
||||
this.runtimeAuth.clearLastWrittenCredentialsJson(accountId)
|
||||
await this.syncRuntimeAuthWithLivePtyGate()
|
||||
await this.rateLimits.refreshForClaudeAccountChange(outgoingAccountId)
|
||||
return this.getSnapshot()
|
||||
@@ -154,7 +154,7 @@ export class ClaudeAccountService {
|
||||
)
|
||||
})
|
||||
try {
|
||||
this.runtimeAuth.clearLastWrittenCredentialsJson()
|
||||
this.runtimeAuth.clearLastWrittenCredentialsJson(accountId)
|
||||
await this.syncRuntimeAuthWithLivePtyGate()
|
||||
await this.rateLimits.refreshForClaudeAccountChange()
|
||||
return this.getSnapshot()
|
||||
|
||||
@@ -182,13 +182,8 @@ describe('CodexRuntimeHomeService', () => {
|
||||
new CodexRuntimeHomeService(store as never)
|
||||
|
||||
expect(
|
||||
JSON.parse(
|
||||
readFileSync(
|
||||
join(testState.userDataDir, 'codex-runtime-home', 'system-default-auth.json'),
|
||||
'utf-8'
|
||||
)
|
||||
)
|
||||
).toEqual({ authJson: '{"account":"system"}\n' })
|
||||
existsSync(join(testState.userDataDir, 'codex-runtime-home', 'system-default-auth.json'))
|
||||
).toBe(true)
|
||||
if (process.platform !== 'win32') {
|
||||
expect(
|
||||
statSync(join(testState.userDataDir, 'codex-runtime-home', 'system-default-auth.json'))
|
||||
@@ -229,13 +224,8 @@ describe('CodexRuntimeHomeService', () => {
|
||||
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe('{"account":"managed"}\n')
|
||||
expect(
|
||||
JSON.parse(
|
||||
readFileSync(
|
||||
join(testState.userDataDir, 'codex-runtime-home', 'system-default-auth.json'),
|
||||
'utf-8'
|
||||
)
|
||||
)
|
||||
).toEqual({ authJson: '{"account":"system"}\n' })
|
||||
existsSync(join(testState.userDataDir, 'codex-runtime-home', 'system-default-auth.json'))
|
||||
).toBe(false)
|
||||
})
|
||||
|
||||
it('restores the system-default snapshot when no managed account is selected', async () => {
|
||||
@@ -260,12 +250,14 @@ describe('CodexRuntimeHomeService', () => {
|
||||
lastAuthenticatedAt: 1
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
activeCodexManagedAccountId: null
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
settings.activeCodexManagedAccountId = 'account-1'
|
||||
service.syncForCurrentSelection()
|
||||
settings.activeCodexManagedAccountId = null
|
||||
writeFileSync(runtimeAuthPath, '{"account":"managed"}\n', 'utf-8')
|
||||
|
||||
@@ -310,7 +302,39 @@ describe('CodexRuntimeHomeService', () => {
|
||||
expect(existsSync(runtimeAuthPath)).toBe(false)
|
||||
})
|
||||
|
||||
it('clears an invalid active account selection and restores the system default snapshot', async () => {
|
||||
it('removes runtime auth when deselecting with a missing system-default snapshot', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
const managedAuth = createCodexAuthJson('user@example.com', 'acct-1', 'managed')
|
||||
writeFileSync(runtimeAuthPath, managedAuth, 'utf-8')
|
||||
const managedHomePath = createManagedAuth(testState.userDataDir, 'account-1', managedAuth)
|
||||
const settings = createSettings({
|
||||
codexManagedAccounts: [
|
||||
{
|
||||
id: 'account-1',
|
||||
email: 'user@example.com',
|
||||
managedHomePath,
|
||||
providerAccountId: 'acct-1',
|
||||
workspaceLabel: null,
|
||||
workspaceAccountId: 'acct-1',
|
||||
createdAt: 1,
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
settings.activeCodexManagedAccountId = null
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
expect(existsSync(runtimeAuthPath)).toBe(false)
|
||||
})
|
||||
|
||||
it('clears an invalid active account selection and removes untrusted runtime auth', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
writeFileSync(runtimeAuthPath, '{"account":"system"}\n', 'utf-8')
|
||||
const missingManagedHomePath = join(
|
||||
@@ -342,10 +366,25 @@ describe('CodexRuntimeHomeService', () => {
|
||||
new CodexRuntimeHomeService(store as never)
|
||||
|
||||
expect(store.updateSettings).toHaveBeenCalledWith({ activeCodexManagedAccountId: null })
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe('{"account":"system"}\n')
|
||||
expect(existsSync(runtimeAuthPath)).toBe(false)
|
||||
expect(warnSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('clears an unknown active account id and removes untrusted runtime auth', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
writeFileSync(runtimeAuthPath, '{"account":"stale-managed"}\n', 'utf-8')
|
||||
const settings = createSettings({
|
||||
activeCodexManagedAccountId: 'missing-account'
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
new CodexRuntimeHomeService(store as never)
|
||||
|
||||
expect(store.updateSettings).toHaveBeenCalledWith({ activeCodexManagedAccountId: null })
|
||||
expect(existsSync(runtimeAuthPath)).toBe(false)
|
||||
})
|
||||
|
||||
it('returns ~/.codex for Codex launch and rate-limit preparation', async () => {
|
||||
const store = createStore(createSettings())
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
@@ -391,14 +430,16 @@ describe('CodexRuntimeHomeService', () => {
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
settings.activeCodexManagedAccountId = 'account-1'
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
// Deselect managed account — should restore system default once
|
||||
settings.activeCodexManagedAccountId = null
|
||||
service.syncForCurrentSelection()
|
||||
@@ -410,7 +451,7 @@ describe('CodexRuntimeHomeService', () => {
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe('{"account":"external-tool"}\n')
|
||||
})
|
||||
|
||||
it('restores system default on restart when persisted active account is invalid', async () => {
|
||||
it('removes untrusted runtime auth on restart when persisted active account is invalid', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
writeFileSync(runtimeAuthPath, '{"account":"system"}\n', 'utf-8')
|
||||
const settings = createSettings({
|
||||
@@ -435,10 +476,8 @@ describe('CodexRuntimeHomeService', () => {
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
new CodexRuntimeHomeService(store as never)
|
||||
|
||||
// Constructor initializes lastSyncedAccountId='account-1' from settings,
|
||||
// then syncForCurrentSelection finds missing auth.json and restores snapshot
|
||||
expect(store.updateSettings).toHaveBeenCalledWith({ activeCodexManagedAccountId: null })
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe('{"account":"system"}\n')
|
||||
expect(existsSync(runtimeAuthPath)).toBe(false)
|
||||
expect(warnSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
@@ -843,9 +882,8 @@ describe('CodexRuntimeHomeService', () => {
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe(refreshedAuth)
|
||||
})
|
||||
|
||||
it('skips read-back on first sync after restart (lastWrittenAuthJson is null)', async () => {
|
||||
it('rejects unverifiable Codex read-back on first sync after restart', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
// Pre-populate runtime with different content to simulate a CLI refresh while Orca was down
|
||||
writeFileSync(runtimeAuthPath, '{"tokens":"refreshed-while-down"}\n', 'utf-8')
|
||||
const managedHomePath = createManagedAuth(
|
||||
testState.userDataDir,
|
||||
@@ -875,13 +913,44 @@ describe('CodexRuntimeHomeService', () => {
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
new CodexRuntimeHomeService(store as never)
|
||||
|
||||
// After restart, managed storage should NOT be overwritten by unknown runtime state
|
||||
expect(readFileSync(managedAuthPath, 'utf-8')).toBe('{"tokens":"original"}\n')
|
||||
// Runtime should be overwritten with managed (conservative behavior)
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe('{"tokens":"original"}\n')
|
||||
})
|
||||
|
||||
it('skips read-back during account switch', async () => {
|
||||
it('reads back verified same-account refreshes on first sync after restart', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
const originalAuth = createCodexAuthJson('user@example.com', 'acct-1', 'original')
|
||||
const refreshedAuth = createCodexAuthJson('user@example.com', 'acct-1', 'refreshed')
|
||||
writeFileSync(runtimeAuthPath, refreshedAuth, 'utf-8')
|
||||
const managedHomePath = createManagedAuth(testState.userDataDir, 'account-1', originalAuth)
|
||||
const managedAuthPath = join(managedHomePath, 'auth.json')
|
||||
const store = createStore(
|
||||
createSettings({
|
||||
codexManagedAccounts: [
|
||||
{
|
||||
id: 'account-1',
|
||||
email: 'user@example.com',
|
||||
managedHomePath,
|
||||
providerAccountId: 'acct-1',
|
||||
workspaceLabel: null,
|
||||
workspaceAccountId: 'acct-1',
|
||||
createdAt: 1,
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
})
|
||||
)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
new CodexRuntimeHomeService(store as never)
|
||||
|
||||
expect(readFileSync(managedAuthPath, 'utf-8')).toBe(refreshedAuth)
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe(refreshedAuth)
|
||||
})
|
||||
|
||||
it('does not contaminate the incoming Codex account during account switch', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
writeFileSync(runtimeAuthPath, '{"account":"system"}\n', 'utf-8')
|
||||
const managedHomePath1 = createManagedAuth(
|
||||
@@ -927,15 +996,122 @@ describe('CodexRuntimeHomeService', () => {
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
// Switch to account-2 — runtime still has account-1 tokens
|
||||
settings.activeCodexManagedAccountId = 'account-2'
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
// Account-2's managed auth should NOT be contaminated with account-1's runtime tokens
|
||||
expect(readFileSync(managedAuthPath2, 'utf-8')).toBe('{"tokens":"account2"}\n')
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe('{"tokens":"account2"}\n')
|
||||
})
|
||||
|
||||
it('does not carry the reauth read-back skip across Codex account switches', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
writeFileSync(runtimeAuthPath, '{"account":"system"}\n', 'utf-8')
|
||||
const account1Auth = createCodexAuthJson('one@example.com', 'acct-one', 'one')
|
||||
const account2Auth = createCodexAuthJson('two@example.com', 'acct-two', 'two')
|
||||
const account2RefreshedAuth = createCodexAuthJson(
|
||||
'two@example.com',
|
||||
'acct-two',
|
||||
'two-refreshed'
|
||||
)
|
||||
const managedHomePath1 = createManagedAuth(testState.userDataDir, 'account-1', account1Auth)
|
||||
const managedHomePath2 = createManagedAuth(testState.userDataDir, 'account-2', account2Auth)
|
||||
const managedAuthPath2 = join(managedHomePath2, 'auth.json')
|
||||
const settings = createSettings({
|
||||
codexManagedAccounts: [
|
||||
{
|
||||
id: 'account-1',
|
||||
email: 'one@example.com',
|
||||
managedHomePath: managedHomePath1,
|
||||
providerAccountId: 'acct-one',
|
||||
workspaceLabel: null,
|
||||
workspaceAccountId: 'acct-one',
|
||||
createdAt: 1,
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
},
|
||||
{
|
||||
id: 'account-2',
|
||||
email: 'two@example.com',
|
||||
managedHomePath: managedHomePath2,
|
||||
providerAccountId: 'acct-two',
|
||||
workspaceLabel: null,
|
||||
workspaceAccountId: 'acct-two',
|
||||
createdAt: 2,
|
||||
updatedAt: 2,
|
||||
lastAuthenticatedAt: 2
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
service.clearLastWrittenAuthJson()
|
||||
settings.activeCodexManagedAccountId = 'account-2'
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeAuthPath, account2RefreshedAuth, 'utf-8')
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
expect(readFileSync(managedAuthPath2, 'utf-8')).toBe(account2RefreshedAuth)
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe(account2RefreshedAuth)
|
||||
})
|
||||
|
||||
it('does not apply inactive-account Codex reauth skip to the active account', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
writeFileSync(runtimeAuthPath, '{"account":"system"}\n', 'utf-8')
|
||||
const account1Auth = createCodexAuthJson('one@example.com', 'acct-one', 'one')
|
||||
const account1RefreshedAuth = createCodexAuthJson(
|
||||
'one@example.com',
|
||||
'acct-one',
|
||||
'one-refreshed'
|
||||
)
|
||||
const account2Auth = createCodexAuthJson('two@example.com', 'acct-two', 'two')
|
||||
const managedHomePath1 = createManagedAuth(testState.userDataDir, 'account-1', account1Auth)
|
||||
const managedHomePath2 = createManagedAuth(testState.userDataDir, 'account-2', account2Auth)
|
||||
const managedAuthPath1 = join(managedHomePath1, 'auth.json')
|
||||
const settings = createSettings({
|
||||
codexManagedAccounts: [
|
||||
{
|
||||
id: 'account-1',
|
||||
email: 'one@example.com',
|
||||
managedHomePath: managedHomePath1,
|
||||
providerAccountId: 'acct-one',
|
||||
workspaceLabel: null,
|
||||
workspaceAccountId: 'acct-one',
|
||||
createdAt: 1,
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
},
|
||||
{
|
||||
id: 'account-2',
|
||||
email: 'two@example.com',
|
||||
managedHomePath: managedHomePath2,
|
||||
providerAccountId: 'acct-two',
|
||||
workspaceLabel: null,
|
||||
workspaceAccountId: 'acct-two',
|
||||
createdAt: 2,
|
||||
updatedAt: 2,
|
||||
lastAuthenticatedAt: 2
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
writeFileSync(runtimeAuthPath, account1RefreshedAuth, 'utf-8')
|
||||
service.clearLastWrittenAuthJson('account-2')
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
expect(readFileSync(managedAuthPath1, 'utf-8')).toBe(account1RefreshedAuth)
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe(account1RefreshedAuth)
|
||||
})
|
||||
|
||||
it('restores system default when unverified runtime auth appears before deselect', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
writeFileSync(runtimeAuthPath, '{"account":"system"}\n', 'utf-8')
|
||||
@@ -957,14 +1133,16 @@ describe('CodexRuntimeHomeService', () => {
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
settings.activeCodexManagedAccountId = 'account-1'
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
// A stale or external process overwrites runtime with auth Orca cannot
|
||||
// verify against the outgoing managed account.
|
||||
writeFileSync(runtimeAuthPath, '{"account":"external-login"}\n', 'utf-8')
|
||||
@@ -999,14 +1177,16 @@ describe('CodexRuntimeHomeService', () => {
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
settings.activeCodexManagedAccountId = 'account-1'
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeAuthPath, externalAuth, 'utf-8')
|
||||
settings.activeCodexManagedAccountId = null
|
||||
service.syncForCurrentSelection()
|
||||
@@ -1035,14 +1215,16 @@ describe('CodexRuntimeHomeService', () => {
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
}
|
||||
],
|
||||
activeCodexManagedAccountId: 'account-1'
|
||||
]
|
||||
})
|
||||
const store = createStore(settings)
|
||||
|
||||
const { CodexRuntimeHomeService } = await import('./runtime-home-service')
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
settings.activeCodexManagedAccountId = 'account-1'
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
writeFileSync(runtimeAuthPath, staleLivePtyAuth, 'utf-8')
|
||||
settings.activeCodexManagedAccountId = null
|
||||
service.syncForCurrentSelection()
|
||||
@@ -1180,11 +1362,9 @@ describe('CodexRuntimeHomeService', () => {
|
||||
it('does not clobber fresh tokens after clearLastWrittenAuthJson', async () => {
|
||||
const runtimeAuthPath = join(testState.fakeHomeDir, '.codex', 'auth.json')
|
||||
writeFileSync(runtimeAuthPath, '{"account":"system"}\n', 'utf-8')
|
||||
const managedHomePath = createManagedAuth(
|
||||
testState.userDataDir,
|
||||
'account-1',
|
||||
'{"tokens":"original"}\n'
|
||||
)
|
||||
const originalAuth = createCodexAuthJson('user@example.com', 'acct-1', 'original')
|
||||
const reauthedAuth = createCodexAuthJson('user@example.com', 'acct-1', 'reauthed')
|
||||
const managedHomePath = createManagedAuth(testState.userDataDir, 'account-1', originalAuth)
|
||||
const managedAuthPath = join(managedHomePath, 'auth.json')
|
||||
const settings = createSettings({
|
||||
codexManagedAccounts: [
|
||||
@@ -1192,9 +1372,9 @@ describe('CodexRuntimeHomeService', () => {
|
||||
id: 'account-1',
|
||||
email: 'user@example.com',
|
||||
managedHomePath,
|
||||
providerAccountId: null,
|
||||
providerAccountId: 'acct-1',
|
||||
workspaceLabel: null,
|
||||
workspaceAccountId: null,
|
||||
workspaceAccountId: 'acct-1',
|
||||
createdAt: 1,
|
||||
updatedAt: 1,
|
||||
lastAuthenticatedAt: 1
|
||||
@@ -1208,15 +1388,15 @@ describe('CodexRuntimeHomeService', () => {
|
||||
const service = new CodexRuntimeHomeService(store as never)
|
||||
|
||||
// Simulate re-auth: managed storage gets fresh tokens
|
||||
writeFileSync(managedAuthPath, '{"tokens":"reauthed"}\n', 'utf-8')
|
||||
writeFileSync(managedAuthPath, reauthedAuth, 'utf-8')
|
||||
|
||||
// Clear tracking before sync (as CodexAccountService would)
|
||||
service.clearLastWrittenAuthJson()
|
||||
service.syncForCurrentSelection()
|
||||
|
||||
// Fresh re-auth tokens should survive — not be clobbered by stale runtime read-back
|
||||
expect(readFileSync(managedAuthPath, 'utf-8')).toBe('{"tokens":"reauthed"}\n')
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe('{"tokens":"reauthed"}\n')
|
||||
expect(readFileSync(managedAuthPath, 'utf-8')).toBe(reauthedAuth)
|
||||
expect(readFileSync(runtimeAuthPath, 'utf-8')).toBe(reauthedAuth)
|
||||
})
|
||||
|
||||
it('preserves conflicting legacy session files under deterministic names', async () => {
|
||||
|
||||
@@ -44,6 +44,7 @@ export class CodexRuntimeHomeService {
|
||||
// login (e.g. `codex auth login`) overwrote it — so Orca adopts the file as
|
||||
// the new system default instead of restoring a stale snapshot.
|
||||
private lastWrittenAuthJson: string | null = null
|
||||
private skipNextReadBackForAccountId: string | null = null
|
||||
|
||||
constructor(private readonly store: Store) {
|
||||
this.safeMigrateLegacyManagedState()
|
||||
@@ -57,12 +58,12 @@ export class CodexRuntimeHomeService {
|
||||
}
|
||||
|
||||
prepareForCodexLaunch(): string {
|
||||
this.safeSyncForCurrentSelection()
|
||||
this.syncForCurrentSelection()
|
||||
return this.getRuntimeHomePath()
|
||||
}
|
||||
|
||||
prepareForRateLimitFetch(): string {
|
||||
this.safeSyncForCurrentSelection()
|
||||
this.syncForCurrentSelection()
|
||||
return this.getRuntimeHomePath()
|
||||
}
|
||||
|
||||
@@ -103,8 +104,6 @@ export class CodexRuntimeHomeService {
|
||||
return
|
||||
}
|
||||
|
||||
this.captureSystemDefaultSnapshot({ force: this.lastSyncedAccountId === null })
|
||||
|
||||
const activeAuthPath = join(activeAccount.managedHomePath, 'auth.json')
|
||||
if (!existsSync(activeAuthPath)) {
|
||||
console.warn(
|
||||
@@ -118,16 +117,27 @@ export class CodexRuntimeHomeService {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.lastSyncedAccountId === null) {
|
||||
this.captureSystemDefaultSnapshot({ force: true })
|
||||
}
|
||||
|
||||
// Why: Codex CLI refreshes expired OAuth tokens and writes them back to
|
||||
// ~/.codex/auth.json. If we detect the runtime file differs from what Orca
|
||||
// last wrote, the CLI must have refreshed — so we preserve those tokens
|
||||
// back to managed storage before overwriting runtime with managed state.
|
||||
if (this.lastSyncedAccountId === activeAccount.id) {
|
||||
this.readBackRefreshedTokens(activeAccount, activeAuthPath, {
|
||||
updateLastWrittenAuthJson: true
|
||||
})
|
||||
if (this.skipNextReadBackForAccountId === activeAccount.id) {
|
||||
this.skipNextReadBackForAccountId = null
|
||||
} else {
|
||||
this.readBackRefreshedTokens(activeAccount, activeAuthPath, {
|
||||
updateLastWrittenAuthJson: true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (this.lastSyncedAccountId !== activeAccount.id) {
|
||||
this.skipNextReadBackForAccountId = null
|
||||
}
|
||||
this.lastSyncedAccountId = activeAccount.id
|
||||
this.writeRuntimeAuth(readFileSync(activeAuthPath, 'utf-8'))
|
||||
}
|
||||
@@ -136,8 +146,9 @@ export class CodexRuntimeHomeService {
|
||||
// re-auth or add-account. Those flows write fresh tokens to managed storage,
|
||||
// so the read-back must be skipped to avoid overwriting them with stale
|
||||
// runtime tokens.
|
||||
clearLastWrittenAuthJson(): void {
|
||||
clearLastWrittenAuthJson(accountId = this.store.getSettings().activeCodexManagedAccountId): void {
|
||||
this.lastWrittenAuthJson = null
|
||||
this.skipNextReadBackForAccountId = accountId
|
||||
}
|
||||
|
||||
private readBackRefreshedTokens(
|
||||
@@ -151,12 +162,8 @@ export class CodexRuntimeHomeService {
|
||||
return 'unchanged'
|
||||
}
|
||||
|
||||
if (this.lastWrittenAuthJson === null) {
|
||||
return 'unchanged'
|
||||
}
|
||||
|
||||
const runtimeContents = readFileSync(runtimeAuthPath, 'utf-8')
|
||||
if (runtimeContents === this.lastWrittenAuthJson) {
|
||||
if (this.lastWrittenAuthJson !== null && runtimeContents === this.lastWrittenAuthJson) {
|
||||
return 'unchanged'
|
||||
}
|
||||
|
||||
@@ -552,6 +559,8 @@ export class CodexRuntimeHomeService {
|
||||
|
||||
const snapshotPath = this.getSystemDefaultSnapshotPath()
|
||||
if (!existsSync(snapshotPath)) {
|
||||
rmSync(this.getRuntimeAuthPath(), { force: true })
|
||||
this.lastWrittenAuthJson = null
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ export class CodexAccountService {
|
||||
activeCodexManagedAccountId: account.id
|
||||
})
|
||||
this.safeSyncCanonicalConfigToManagedHomes()
|
||||
this.runtimeHome.clearLastWrittenAuthJson()
|
||||
this.runtimeHome.clearLastWrittenAuthJson(account.id)
|
||||
this.runtimeHome.syncForCurrentSelection()
|
||||
|
||||
// Why: the new account becomes active, so the previous active account is
|
||||
@@ -150,7 +150,7 @@ export class CodexAccountService {
|
||||
codexManagedAccounts: updatedAccounts
|
||||
})
|
||||
this.safeSyncCanonicalConfigToManagedHomes()
|
||||
this.runtimeHome.clearLastWrittenAuthJson()
|
||||
this.runtimeHome.clearLastWrittenAuthJson(accountId)
|
||||
this.runtimeHome.syncForCurrentSelection()
|
||||
|
||||
// Why: re-auth can change which actual Codex identity the managed home
|
||||
|
||||
Reference in New Issue
Block a user