Files
openchamber/packages/web/server/lib/opencode/auth.js
T
Nelson Pires 073f44de2a refactor(server): split opencode config/auth/ui-auth into domain modules (#454)
* docs(agents): map opencode module documentation

* docs(opencode): document split module responsibilities

* refactor(opencode): centralize shared config and file helpers

* refactor(opencode): move auth storage helpers into domain module

* refactor(opencode): move UI auth implementation into domain module

* refactor(opencode): isolate agent scope and CRUD logic

* refactor(opencode): isolate command scope and CRUD logic

* refactor(opencode): isolate provider config helpers

* refactor(opencode): isolate skill discovery and CRUD logic

* refactor(opencode): define single public module entrypoint

* refactor(opencode): remove legacy opencode-config module

* refactor(opencode): remove obsolete opencode-config typings

* refactor(opencode): remove legacy opencode-auth module

* refactor(opencode): remove legacy ui-auth shim

* refactor(server): import opencode APIs from new module paths

* refactor(tts): consume auth helpers from opencode domain module

* refactor(quota): point claude provider auth import to opencode domain

* refactor(quota): point codex provider auth import to opencode domain

* refactor(quota): point copilot provider auth import to opencode domain

* refactor(quota): point google auth import to opencode domain

* refactor(quota): point kimi provider auth import to opencode domain

* refactor(quota): point nanogpt provider auth import to opencode domain

* refactor(quota): point openai provider auth import to opencode domain

* refactor(quota): point openrouter provider auth import to opencode domain

* refactor(quota): point zai provider auth import to opencode domain

* fix(opencode): restore provider source and disconnect semantics
2026-02-20 15:57:44 +02:00

82 lines
2.0 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import os from 'os';
const OPENCODE_DATA_DIR = path.join(os.homedir(), '.local', 'share', 'opencode');
const AUTH_FILE = path.join(OPENCODE_DATA_DIR, 'auth.json');
function readAuthFile() {
if (!fs.existsSync(AUTH_FILE)) {
return {};
}
try {
const content = fs.readFileSync(AUTH_FILE, 'utf8');
const trimmed = content.trim();
if (!trimmed) {
return {};
}
return JSON.parse(trimmed);
} catch (error) {
console.error('Failed to read auth file:', error);
throw new Error('Failed to read OpenCode auth configuration');
}
}
function writeAuthFile(auth) {
try {
if (!fs.existsSync(OPENCODE_DATA_DIR)) {
fs.mkdirSync(OPENCODE_DATA_DIR, { recursive: true });
}
if (fs.existsSync(AUTH_FILE)) {
const backupFile = `${AUTH_FILE}.openchamber.backup`;
fs.copyFileSync(AUTH_FILE, backupFile);
console.log(`Created auth backup: ${backupFile}`);
}
fs.writeFileSync(AUTH_FILE, JSON.stringify(auth, null, 2), 'utf8');
console.log('Successfully wrote auth file');
} catch (error) {
console.error('Failed to write auth file:', error);
throw new Error('Failed to write OpenCode auth configuration');
}
}
function removeProviderAuth(providerId) {
if (!providerId || typeof providerId !== 'string') {
throw new Error('Provider ID is required');
}
const auth = readAuthFile();
if (!auth[providerId]) {
console.log(`Provider ${providerId} not found in auth file, nothing to remove`);
return false;
}
delete auth[providerId];
writeAuthFile(auth);
console.log(`Removed provider auth: ${providerId}`);
return true;
}
function getProviderAuth(providerId) {
const auth = readAuthFile();
return auth[providerId] || null;
}
function listProviderAuths() {
const auth = readAuthFile();
return Object.keys(auth);
}
export {
readAuthFile,
writeAuthFile,
removeProviderAuth,
getProviderAuth,
listProviderAuths,
AUTH_FILE,
OPENCODE_DATA_DIR
};