/**
* Authentication Utility for IRCAM Amplify API
*/
import { MCPError } from '../types/mcp-tools.js';
/**
* Environment variable name for the API key
*/
export const API_KEY_ENV_VAR = 'IRCAM_AMPLIFY_API_KEY';
/**
* Get the IRCAM Amplify API key from environment
* @throws MCPError if the API key is not set
*/
export function getApiKey(): string {
const apiKey = process.env[API_KEY_ENV_VAR];
if (!apiKey || apiKey.trim() === '') {
const error: MCPError = {
code: 'MISSING_API_KEY',
message: `Missing API key. Set ${API_KEY_ENV_VAR} environment variable.`,
suggestion: `Get your API key from https://app.ircamamplify.io and set it with: export ${API_KEY_ENV_VAR}="your-key-here"`,
};
throw error;
}
return apiKey.trim();
}
/**
* Check if the API key is configured (without throwing)
*/
export function hasApiKey(): boolean {
const apiKey = process.env[API_KEY_ENV_VAR];
return !!apiKey && apiKey.trim() !== '';
}
/**
* Create authentication headers for IRCAM API requests
* @throws MCPError if the API key is not set
*/
export function createAuthHeaders(): Record<string, string> {
const apiKey = getApiKey();
return {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
};
}
/**
* Create headers with custom content type
* @throws MCPError if the API key is not set
*/
export function createAuthHeadersWithContentType(contentType: string): Record<string, string> {
const apiKey = getApiKey();
return {
Authorization: `Bearer ${apiKey}`,
'Content-Type': contentType,
};
}
/**
* Mask API key for logging (show only first and last 4 chars)
*/
export function maskApiKey(apiKey: string): string {
if (apiKey.length <= 8) {
return '***';
}
return `${apiKey.slice(0, 4)}...${apiKey.slice(-4)}`;
}