Skip to content

Commit 24e3801

Browse files
fix minimax use (chriswritescode-dev#44)
* Fix provider API key storage to match OpenCode schema Backend was writing credentials with type 'apiKey' and field 'apiKey', but OpenCode expects type 'api' and field 'key'. This caused UI-set credentials to be silently ignored while environment variables worked. - Update AuthEntrySchema to use 'api' type and 'key' field - Update AuthService.set() to write new format - Add migration to convert existing entries on read Fixes chriswritescode-dev#40 * Add auth service tests to validate schema fix and migration Tests verify: - Migration from old 'apiKey' format to 'api' format - New credentials are written in correct format - getAll, set, delete, list, and has methods work correctly --------- Co-authored-by: Chris Scott <[email protected]>
1 parent b91cebf commit 24e3801

3 files changed

Lines changed: 391 additions & 6 deletions

File tree

‎backend/src/services/auth.ts‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,38 @@ type AuthCredentials = z.infer<typeof AuthCredentialsSchema>
1010
export class AuthService {
1111
private authPath = getAuthPath()
1212

13+
private migrateEntry(entry: Record<string, unknown>): Record<string, unknown> {
14+
if (entry.type === 'apiKey' && typeof entry.apiKey === 'string') {
15+
return {
16+
type: 'api',
17+
key: entry.apiKey,
18+
}
19+
}
20+
return entry
21+
}
22+
1323
async getAll(): Promise<AuthCredentials> {
1424
try {
1525
const data = await fs.readFile(this.authPath, 'utf-8')
16-
const parsed = JSON.parse(data)
17-
return AuthCredentialsSchema.parse(parsed)
26+
const parsed = JSON.parse(data) as Record<string, Record<string, unknown>>
27+
28+
let needsMigration = false
29+
const migrated: Record<string, Record<string, unknown>> = {}
30+
31+
for (const [key, entry] of Object.entries(parsed)) {
32+
const migratedEntry = this.migrateEntry(entry)
33+
if (migratedEntry !== entry) {
34+
needsMigration = true
35+
}
36+
migrated[key] = migratedEntry
37+
}
38+
39+
if (needsMigration) {
40+
await fs.writeFile(this.authPath, JSON.stringify(migrated, null, 2), { mode: 0o600 })
41+
logger.info('Migrated auth.json to new schema format')
42+
}
43+
44+
return AuthCredentialsSchema.parse(migrated)
1845
} catch (error) {
1946
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
2047
return {}
@@ -27,8 +54,8 @@ export class AuthService {
2754
async set(providerId: string, apiKey: string): Promise<void> {
2855
const auth = await this.getAll()
2956
auth[providerId] = {
30-
type: 'apiKey',
31-
apiKey,
57+
type: 'api',
58+
key: apiKey,
3259
}
3360

3461
await fs.mkdir(path.dirname(this.authPath), { recursive: true })

0 commit comments

Comments
 (0)