mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
* feat(ocm-cli): add file and keychain credential backends with auth commands * feat(ocm-cli): refactor credentials into async TokenStore architecture Replace the credential-backend module with a TokenStore interface and file/keychain implementations behind an internal facade. Convert token get/set/delete to async, add env-token precedence warnings on login and logout, and surface store availability in status. Add token-store tests and helpers; remove the old credential modules and tests. Bump to 0.2.6. * fix(ocm-cli): keep tokens out of security argv and stop masking config read errors Route every macOS Keychain call through `security -i`, sending the quoted command on stdin so the token is never visible in process arguments. Reject arguments containing line breaks, which would otherwise inject additional security commands. Let unexpected state file read failures propagate so a broken config dir is reported instead of being reduced to "no manager configured", and stop consuming the install notice when reading it fails unexpectedly. --------- Co-authored-by: CS <chris@chriswritescode.dev>
70 lines
2.4 KiB
TypeScript
70 lines
2.4 KiB
TypeScript
import { vi } from 'vitest'
|
|
import type { OcmState } from '../../src/state.js'
|
|
import type { TokenStoreDescription } from '../../src/token-store.js'
|
|
|
|
export class MockTokenStoreError extends Error {
|
|
constructor(message: string, public kind = 'file') {
|
|
super(message)
|
|
this.name = 'TokenStoreError'
|
|
}
|
|
}
|
|
|
|
const DEFAULT_STORE: TokenStoreDescription = { kind: 'file', location: '/tmp/credentials.json' }
|
|
|
|
export interface TokenStoreMockOptions {
|
|
token?: string | null
|
|
getTokenImpl?: (account: string) => Promise<string | null>
|
|
setTokenImpl?: (account: string, token: string) => Promise<void>
|
|
deleteTokenImpl?: (account: string) => Promise<boolean>
|
|
store?: TokenStoreDescription
|
|
writeTarget?: TokenStoreDescription
|
|
env?: string | null
|
|
}
|
|
|
|
export function mockStateModule(state: OcmState | null = { managerUrl: 'https://manager.example.com' }) {
|
|
const clearState = vi.fn()
|
|
vi.doMock('../../src/state.js', () => ({
|
|
readState: () => state,
|
|
writeState: () => {},
|
|
clearState,
|
|
getStatePath: () => '/tmp/state.json',
|
|
getConfigDir: () => '/tmp',
|
|
}))
|
|
return { clearState }
|
|
}
|
|
|
|
export function mockTokenStoreModule(options: TokenStoreMockOptions = {}) {
|
|
const envValue = options.env ?? null
|
|
const getToken = vi.fn<(account: string) => Promise<string | null>>(
|
|
options.getTokenImpl ?? (() => Promise.resolve(options.token ?? null)),
|
|
)
|
|
const setToken = vi.fn<(account: string, token: string) => Promise<void>>(
|
|
options.setTokenImpl ?? (() => Promise.resolve()),
|
|
)
|
|
const deleteToken = vi.fn<(account: string) => Promise<boolean>>(
|
|
options.deleteTokenImpl ?? (() => Promise.resolve(true)),
|
|
)
|
|
const store = options.store ?? DEFAULT_STORE
|
|
vi.doMock('../../src/internal-token-store.js', () => ({
|
|
TOKEN_ENV: 'OCM_TOKEN',
|
|
envToken: () => envValue,
|
|
getToken,
|
|
setToken,
|
|
deleteToken,
|
|
hasStoredToken: async (account: string | undefined) => {
|
|
if (envValue) return true
|
|
if (!account) return false
|
|
return (await getToken(account)) !== null
|
|
},
|
|
describeTokenStore: () => (envValue ? { kind: 'env', location: 'OCM_TOKEN' } : store),
|
|
describeTokenWriteTarget: () => options.writeTarget ?? store,
|
|
TokenStoreError: MockTokenStoreError,
|
|
}))
|
|
return { getToken, setToken, deleteToken }
|
|
}
|
|
|
|
export function unmockTokenStoreModules() {
|
|
vi.doUnmock('../../src/state.js')
|
|
vi.doUnmock('../../src/internal-token-store.js')
|
|
}
|