auth: add Anthropic auth token storage

This commit is contained in:
William Valentin
2026-02-15 10:27:32 -08:00
parent bcf6c377d5
commit 6375f56f67
3 changed files with 75 additions and 3 deletions
+30 -1
View File
@@ -6,6 +6,7 @@ import { join } from 'path';
describe('auth/anthropic', () => {
const originalHome = process.env.HOME;
const originalEnvKey = process.env.ANTHROPIC_API_KEY;
const originalEnvToken = process.env.ANTHROPIC_AUTH_TOKEN;
let homeDir: string;
@@ -13,6 +14,7 @@ describe('auth/anthropic', () => {
homeDir = mkdtempSync(join(tmpdir(), 'flynn-auth-anthropic-'));
process.env.HOME = homeDir;
delete process.env.ANTHROPIC_API_KEY;
delete process.env.ANTHROPIC_AUTH_TOKEN;
vi.resetModules();
});
@@ -23,6 +25,12 @@ describe('auth/anthropic', () => {
} else {
delete process.env.ANTHROPIC_API_KEY;
}
if (originalEnvToken) {
process.env.ANTHROPIC_AUTH_TOKEN = originalEnvToken;
} else {
delete process.env.ANTHROPIC_AUTH_TOKEN;
}
});
it('stores, loads, and clears Anthropic API key', async () => {
@@ -41,10 +49,31 @@ describe('auth/anthropic', () => {
expect(mod.loadStoredAnthropicAuth()).toBeNull();
});
it('stores, loads, and clears Anthropic auth token', async () => {
const mod = await import('./anthropic.js');
expect(mod.loadStoredAnthropicAuthToken()).toBeNull();
mod.storeAnthropicAuthToken('tok-test');
expect(mod.loadStoredAnthropicAuthToken()).toBe('tok-test');
const authFile = join(homeDir, '.config/flynn/auth.json');
const mode = statSync(authFile).mode & 0o777;
expect(mode).toBe(0o600);
mod.clearAnthropicAuthToken();
expect(mod.loadStoredAnthropicAuthToken()).toBeNull();
});
it('getAnthropicApiKey prefers environment variable', async () => {
process.env.ANTHROPIC_API_KEY = 'sk-env';
const mod = await import('./anthropic.js');
expect(mod.getAnthropicApiKey()).toBe('sk-env');
});
});
it('getAnthropicAuthToken prefers environment variable', async () => {
process.env.ANTHROPIC_AUTH_TOKEN = 'tok-env';
const mod = await import('./anthropic.js');
expect(mod.getAnthropicAuthToken()).toBe('tok-env');
});
});