feat(auth): add anthropic api key storage and cli auth

This commit is contained in:
William Valentin
2026-02-14 00:43:12 -08:00
parent 0493660e7d
commit 4bb8c88fbe
7 changed files with 211 additions and 2 deletions
+56
View File
@@ -0,0 +1,56 @@
import type { Command } from 'commander';
import readline from 'readline';
import { loadStoredAnthropicAuth, storeAnthropicAuth } from '../auth/index.js';
async function promptHidden(question: string): Promise<string> {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, terminal: true });
const rlAny = rl as unknown as { stdoutMuted?: boolean; _writeToOutput?: (s: string) => void };
rlAny.stdoutMuted = true;
rlAny._writeToOutput = (s: string) => {
if (!rlAny.stdoutMuted) {
process.stdout.write(s);
return;
}
if (s.includes('\n')) {
process.stdout.write('\n');
} else {
process.stdout.write('*');
}
};
const answer = await new Promise<string>((resolve) => rl.question(question, resolve));
rlAny.stdoutMuted = false;
rl.close();
process.stdout.write('\n');
return answer.trim();
}
export function registerAnthropicAuthCommand(program: Command): void {
program
.command('anthropic-auth')
.description('Store an Anthropic API key (auth.json)')
.action(async () => {
const existing = loadStoredAnthropicAuth();
if (existing) {
console.log('Anthropic credential already exists.');
console.log('Delete ~/.config/flynn/auth.json anthropic entry if you want to re-authenticate.');
process.exit(0);
}
console.log('Anthropic uses API keys for authentication.');
console.log('Create a key at: https://console.anthropic.com/settings/keys');
console.log('');
try {
const apiKey = await promptHidden('Enter Anthropic API key: ');
storeAnthropicAuth(apiKey);
console.log('');
console.log('Anthropic credential stored in ~/.config/flynn/auth.json');
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.error(`Anthropic auth failed: ${message}`);
process.exit(1);
}
});
}
+2
View File
@@ -20,6 +20,7 @@ import { registerGdriveAuthCommand } from './gdrive-auth.js';
import { registerGtasksAuthCommand } from './gtasks-auth.js';
import { registerOpenaiAuthCommand } from './openai-auth.js';
import { registerZaiAuthCommand } from './zai-auth.js';
import { registerAnthropicAuthCommand } from './anthropic-auth.js';
import { registerSkillsCommand } from './skills.js';
export function createProgram(): Command {
@@ -45,6 +46,7 @@ export function createProgram(): Command {
registerGtasksAuthCommand(program);
registerOpenaiAuthCommand(program);
registerZaiAuthCommand(program);
registerAnthropicAuthCommand(program);
registerSkillsCommand(program);
return program;