research-cli
Version:
AI-powered research assistant with web search capabilities and beautiful terminal UI
102 lines • 3.51 kB
JavaScript
/**
* Secure credential management using OS keychain
* Legacy functions for backward compatibility
*/
import * as readline from "node:readline";
import { apiKeyManager } from "../auth/keyring-manager.js";
/**
* Get OpenAI API key from environment variable or keychain
* Updated to use SecureAPIKeyManager
*/
export async function getOpenAIApiKey() {
// First check environment variable (for CI/CD, advanced users)
const envKey = process.env["OPENAI_API_KEY"];
if (envKey) {
return envKey;
}
// Try to get from secure keychain using the new manager
try {
const storedKey = await apiKeyManager.getApiKey("openai");
if (storedKey) {
return storedKey;
}
}
catch (error) {
// If keychain access fails, continue to prompt
console.error("Warning: Could not access system keychain:", error instanceof Error ? error.message : "Unknown error");
console.error("Falling back to manual API key entry...");
}
// No key found, prompt user to enter it
return await promptForApiKey();
}
/**
* Prompt user for API key and optionally store it
*/
async function promptForApiKey() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
console.log("\n🔑 OpenAI API Key Required");
console.log("Get your API key from: https://platform.openai.com/api-keys\n");
const apiKey = await new Promise((resolve) => {
rl.question("Enter your OpenAI API key: ", resolve);
});
if (!apiKey || apiKey.trim().length === 0) {
throw new Error("API key is required to continue");
}
// Ask if they want to save it securely
const shouldSave = await new Promise((resolve) => {
rl.question("\nSave this key securely in your system keychain? (y/N): ", resolve);
});
if (shouldSave.toLowerCase().startsWith("y")) {
try {
const success = await apiKeyManager.storeApiKey("openai", apiKey);
if (success) {
console.log("✅ API key saved securely in system keychain");
}
else {
console.log("⚠️ Could not save to keychain");
console.log("You can set the OPENAI_API_KEY environment variable instead.");
}
}
catch (error) {
console.error("⚠️ Could not save to keychain:", error instanceof Error ? error.message : "Unknown error");
console.log("You can set the OPENAI_API_KEY environment variable instead.");
}
}
console.log(); // Add spacing
return apiKey.trim();
}
finally {
rl.close();
}
}
/**
* Remove stored API key from keychain
* Updated to use SecureAPIKeyManager
*/
export async function removeStoredApiKey() {
try {
return await apiKeyManager.removeApiKey("openai");
}
catch (error) {
console.error("Could not remove stored API key:", error instanceof Error ? error.message : "Unknown error");
return false;
}
}
/**
* Check if API key is stored in keychain
* Updated to use SecureAPIKeyManager
*/
export async function hasStoredApiKey() {
try {
const key = await apiKeyManager.getApiKey("openai");
return key !== null;
}
catch {
return false;
}
}
//# sourceMappingURL=credentials.js.map