feat(ocm-cli): add file and keychain credential backends with auth commands (#327)

* 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>
This commit is contained in:
Chris ScottandCS authored and GitHub committed 2026-07-28 16:38:12 -04:00
1 parent 29f154a9c3
commit 6cc50a02ed
19 files changed
+1560 -117

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
import { accessSync, chmodSync, constants, mkdirSync, readFileSync, renameSync, unlinkSync, writeFileSync } from 'fs'
import { randomBytes } from 'crypto'
import { dirname } from 'path'
const DIR_MODE = 0o700
const FILE_MODE = 0o600
function isPermissionError(err: unknown): boolean {
const code = (err as NodeJS.ErrnoException).code
return code === 'EACCES' || code === 'EPERM'
}
function removeQuietly(filePath: string): void {
try {
unlinkSync(filePath)
} catch {
return
}
}
export function ensurePrivateDir(dir: string): void {
try {
mkdirSync(dir, { recursive: true, mode: DIR_MODE })
} catch (err) {
if (!isPermissionError(err)) throw err
try {
accessSync(dir, constants.X_OK)
} catch {
throw err
}
}
try {
chmodSync(dir, DIR_MODE)
} catch {
return
}
}
export function readJsonFile<T>(filePath: string): T | null {
let contents: string
try {
contents = readFileSync(filePath, 'utf-8')
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return null
throw err
}
if (contents.trim() === '') return null
try {
return JSON.parse(contents) as T
} catch {
return null
}
}
export function writeJsonFileAtomic(filePath: string, data: unknown): void {
ensurePrivateDir(dirname(filePath))
const tmp = `${filePath}.${process.pid}.${randomBytes(4).toString('hex')}.tmp`
try {
writeFileSync(tmp, JSON.stringify(data, null, 2), { encoding: 'utf-8', mode: FILE_MODE, flag: 'wx' })
chmodSync(tmp, FILE_MODE)
renameSync(tmp, filePath)
} catch (err) {
removeQuietly(tmp)
throw err
}
}