mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
fix minimax use (#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 #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 <chris@cstech.dev>
This commit is contained in:
1 parent
f1a7d36433
commit
c267bce42e
3 files changed
+391
-6
No files matched your search
@@ -10,11 +10,38 @@ type AuthCredentials = z.infer<typeof AuthCredentialsSchema>
|
||||
export class AuthService {
|
||||
private authPath = getAuthPath()
|
||||
|
||||
private migrateEntry(entry: Record<string, unknown>): Record<string, unknown> {
|
||||
if (entry.type === 'apiKey' && typeof entry.apiKey === 'string') {
|
||||
return {
|
||||
type: 'api',
|
||||
key: entry.apiKey,
|
||||
}
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
async getAll(): Promise<AuthCredentials> {
|
||||
try {
|
||||
const data = await fs.readFile(this.authPath, 'utf-8')
|
||||
const parsed = JSON.parse(data)
|
||||
return AuthCredentialsSchema.parse(parsed)
|
||||
const parsed = JSON.parse(data) as Record<string, Record<string, unknown>>
|
||||
|
||||
let needsMigration = false
|
||||
const migrated: Record<string, Record<string, unknown>> = {}
|
||||
|
||||
for (const [key, entry] of Object.entries(parsed)) {
|
||||
const migratedEntry = this.migrateEntry(entry)
|
||||
if (migratedEntry !== entry) {
|
||||
needsMigration = true
|
||||
}
|
||||
migrated[key] = migratedEntry
|
||||
}
|
||||
|
||||
if (needsMigration) {
|
||||
await fs.writeFile(this.authPath, JSON.stringify(migrated, null, 2), { mode: 0o600 })
|
||||
logger.info('Migrated auth.json to new schema format')
|
||||
}
|
||||
|
||||
return AuthCredentialsSchema.parse(migrated)
|
||||
} catch (error) {
|
||||
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
|
||||
return {}
|
||||
@@ -27,8 +54,8 @@ export class AuthService {
|
||||
async set(providerId: string, apiKey: string): Promise<void> {
|
||||
const auth = await this.getAll()
|
||||
auth[providerId] = {
|
||||
type: 'apiKey',
|
||||
apiKey,
|
||||
type: 'api',
|
||||
key: apiKey,
|
||||
}
|
||||
|
||||
await fs.mkdir(path.dirname(this.authPath), { recursive: true })
|
||||
|
||||
@@ -0,0 +1,358 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { unlink, mkdir } from 'fs/promises'
|
||||
import { AuthService } from '../../src/services/auth'
|
||||
|
||||
const readFile = vi.fn()
|
||||
const writeFile = vi.fn()
|
||||
const mkdirSpy = vi.fn()
|
||||
|
||||
vi.mock('fs', () => ({
|
||||
promises: {
|
||||
readFile,
|
||||
writeFile,
|
||||
mkdir: mkdirSpy,
|
||||
},
|
||||
}))
|
||||
|
||||
vi.mock('@opencode-manager/shared/config/env', () => ({
|
||||
getAuthPath: () => '/test/auth.json',
|
||||
ENV: {
|
||||
WORKSPACE: {
|
||||
BASE_PATH: '/test/workspace',
|
||||
REPOS_DIR: 'repos',
|
||||
CONFIG_DIR: '.config/opencode',
|
||||
AUTH_FILE: '.opencode/state/opencode/auth.json',
|
||||
},
|
||||
},
|
||||
getWorkspacePath: () => '/test/workspace',
|
||||
getReposPath: () => '/test/repos',
|
||||
getConfigPath: () => '/test/config',
|
||||
getOpenCodeConfigFilePath: () => '/test/config/opencode.json',
|
||||
getAgentsMdPath: () => '/test/config/AGENTS.md',
|
||||
getDatabasePath: () => '/test/database.db',
|
||||
}))
|
||||
|
||||
describe('AuthService', () => {
|
||||
let authService: AuthService
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
authService = new AuthService()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('migration', () => {
|
||||
it('migrates old apiKey format to new api format on read', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
minimax: {
|
||||
type: 'apiKey',
|
||||
apiKey: 'test-key-123',
|
||||
},
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2))
|
||||
|
||||
const result = await authService.getAll()
|
||||
|
||||
expect(result).toEqual({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key-123',
|
||||
},
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
})
|
||||
|
||||
expect(writeFile).toHaveBeenCalledWith(
|
||||
'/test/auth.json',
|
||||
JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key-123',
|
||||
},
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2),
|
||||
{ mode: 384 }
|
||||
)
|
||||
})
|
||||
|
||||
it('does not migrate when format is already correct', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key-123',
|
||||
},
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2))
|
||||
|
||||
const result = await authService.getAll()
|
||||
|
||||
expect(result).toEqual({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key-123',
|
||||
},
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
})
|
||||
|
||||
expect(writeFile).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('migrates multiple old format entries', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
minimax: {
|
||||
type: 'apiKey',
|
||||
apiKey: 'minimax-key',
|
||||
},
|
||||
openai: {
|
||||
type: 'apiKey',
|
||||
apiKey: 'openai-key',
|
||||
},
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2))
|
||||
|
||||
const result = await authService.getAll()
|
||||
|
||||
expect(result.minimax).toEqual({
|
||||
type: 'api',
|
||||
key: 'minimax-key',
|
||||
})
|
||||
expect(result.openai).toEqual({
|
||||
type: 'api',
|
||||
key: 'openai-key',
|
||||
})
|
||||
expect(result.anthropic).toEqual({
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
})
|
||||
|
||||
expect(writeFile).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('set', () => {
|
||||
it('writes credentials in new api format', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({}, null, 2))
|
||||
mkdirSpy.mockResolvedValue(undefined)
|
||||
|
||||
await authService.set('minimax', 'test-key-123')
|
||||
|
||||
const expectedData = JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key-123',
|
||||
},
|
||||
}, null, 2)
|
||||
|
||||
expect(writeFile).toHaveBeenCalledWith(
|
||||
'/test/auth.json',
|
||||
expectedData,
|
||||
{ mode: 384 }
|
||||
)
|
||||
})
|
||||
|
||||
it('appends new credentials to existing file', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2))
|
||||
mkdirSpy.mockResolvedValue(undefined)
|
||||
|
||||
await authService.set('minimax', 'test-key-123')
|
||||
|
||||
const expectedData = JSON.stringify({
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key-123',
|
||||
},
|
||||
}, null, 2)
|
||||
|
||||
expect(writeFile).toHaveBeenCalledWith(
|
||||
'/test/auth.json',
|
||||
expectedData,
|
||||
{ mode: 384 }
|
||||
)
|
||||
})
|
||||
|
||||
it('updates existing credentials in new format', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'old-key',
|
||||
},
|
||||
}, null, 2))
|
||||
mkdirSpy.mockResolvedValue(undefined)
|
||||
|
||||
await authService.set('minimax', 'new-key-456')
|
||||
|
||||
const expectedData = JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'new-key-456',
|
||||
},
|
||||
}, null, 2)
|
||||
|
||||
expect(writeFile).toHaveBeenCalledWith(
|
||||
'/test/auth.json',
|
||||
expectedData,
|
||||
{ mode: 384 }
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getAll', () => {
|
||||
it('returns empty object when file does not exist', async () => {
|
||||
readFile.mockRejectedValue({ code: 'ENOENT' })
|
||||
|
||||
const result = await authService.getAll()
|
||||
|
||||
expect(result).toEqual({})
|
||||
})
|
||||
|
||||
it('returns parsed credentials', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key',
|
||||
},
|
||||
}, null, 2))
|
||||
|
||||
const result = await authService.getAll()
|
||||
|
||||
expect(result).toEqual({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key',
|
||||
},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('delete', () => {
|
||||
it('removes provider credentials', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key',
|
||||
},
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2))
|
||||
|
||||
await authService.delete('minimax')
|
||||
|
||||
const expectedData = JSON.stringify({
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2)
|
||||
|
||||
expect(writeFile).toHaveBeenCalledWith(
|
||||
'/test/auth.json',
|
||||
expectedData,
|
||||
{ mode: 384 }
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('list', () => {
|
||||
it('returns list of provider IDs', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key',
|
||||
},
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2))
|
||||
|
||||
const result = await authService.list()
|
||||
|
||||
expect(result).toEqual(['minimax', 'anthropic'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('has', () => {
|
||||
it('returns true when provider has credentials', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
minimax: {
|
||||
type: 'api',
|
||||
key: 'test-key',
|
||||
},
|
||||
}, null, 2))
|
||||
|
||||
const result = await authService.has('minimax')
|
||||
|
||||
expect(result).toBe(true)
|
||||
})
|
||||
|
||||
it('returns false when provider does not have credentials', async () => {
|
||||
readFile.mockResolvedValue(JSON.stringify({
|
||||
anthropic: {
|
||||
type: 'oauth',
|
||||
refresh: 'refresh-token',
|
||||
access: 'access-token',
|
||||
expires: 1234567890,
|
||||
},
|
||||
}, null, 2))
|
||||
|
||||
const result = await authService.has('minimax')
|
||||
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,8 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const AuthEntrySchema = z.object({
|
||||
type: z.enum(["apiKey", "oauth"]),
|
||||
apiKey: z.string().optional(),
|
||||
type: z.enum(["api", "oauth"]),
|
||||
key: z.string().optional(),
|
||||
refresh: z.string().optional(),
|
||||
access: z.string().optional(),
|
||||
expires: z.number().optional(),
|
||||
|
||||
Reference in new issue
Block a user