refactor(provider): migrate credentials storage from ~/.costrict to ~/.claude directory

This commit is contained in:
Askhz 2026-04-09 17:26:44 +08:00
parent 03b7cbf7de
commit 1cedb89db8

View File

@ -1,12 +1,12 @@
/** /**
* CoStrict * CoStrict
* ~/.costrict/share/auth.json CoStrict IDE * ~/.claude/csc-auth.json
*/ */
import { promises as fs } from 'node:fs' import { promises as fs } from 'node:fs'
import { join } from 'node:path' import { join } from 'node:path'
import { homedir } from 'node:os'
import { createHash } from 'node:crypto' import { createHash } from 'node:crypto'
import { getClaudeConfigHomeDir } from '../../utils/envUtils.js'
/** /**
* CoStrict ( IDE ) * CoStrict ( IDE )
@ -25,11 +25,10 @@ export interface CoStrictCredentials {
} }
/** /**
* CoStrict * ~/.claude/csc-auth.json
* @returns ~/.costrict/share/auth.json
*/ */
export function getCoStrictCredentialsPath(): string { export function getCoStrictCredentialsPath(): string {
return join(homedir(), '.costrict', 'share', 'auth.json') return join(getClaudeConfigHomeDir(), 'csc-auth.json')
} }
/** /**
@ -51,14 +50,9 @@ export function generateMachineId(): string {
*/ */
export async function loadCoStrictCredentials(): Promise<CoStrictCredentials | null> { export async function loadCoStrictCredentials(): Promise<CoStrictCredentials | null> {
try { try {
const filepath = getCoStrictCredentialsPath() const content = await fs.readFile(getCoStrictCredentialsPath(), 'utf-8')
const content = await fs.readFile(filepath, 'utf-8')
const credentials = JSON.parse(content) as CoStrictCredentials const credentials = JSON.parse(content) as CoStrictCredentials
if (!credentials.access_token || !credentials.base_url) return null
if (!credentials.access_token || !credentials.base_url) {
return null
}
return credentials return credentials
} catch (error: any) { } catch (error: any) {
if (error.code === 'ENOENT') return null if (error.code === 'ENOENT') return null
@ -68,16 +62,15 @@ export async function loadCoStrictCredentials(): Promise<CoStrictCredentials | n
} }
/** /**
* CoStrict * CoStrict ~/.claude/csc-auth.json
*/ */
export async function saveCoStrictCredentials( export async function saveCoStrictCredentials(credentials: CoStrictCredentials): Promise<void> {
credentials: CoStrictCredentials,
): Promise<void> {
const filepath = getCoStrictCredentialsPath() const filepath = getCoStrictCredentialsPath()
const dir = join(homedir(), '.costrict', 'share') await fs.mkdir(getClaudeConfigHomeDir(), { recursive: true })
await fs.mkdir(dir, { recursive: true, mode: 0o755 }) await fs.writeFile(filepath, JSON.stringify(credentials, null, 2) + '\n', {
const content = JSON.stringify(credentials, null, 2) encoding: 'utf-8',
await fs.writeFile(filepath, content, { encoding: 'utf-8', mode: 0o600 }) mode: 0o600,
})
} }
/** /**