direct-api-config.jsā¢7.71 kB
#!/usr/bin/env node
/**
* Direct API Configuration v1.0.0
* Standalone API client configuration for EuConquisto Composer
*
* Purpose: Enable direct API authentication without browser automation
* Status: EXPERIMENTAL - Alternative to JWT redirect server approach
*
* @version 1.0.0 (January 15, 2025)
*/
/**
* Authentication configuration for direct API access
*
* This configuration can be populated from:
* 1. Environment variables (most secure)
* 2. MCP server configuration
* 3. One-time extraction from active Composer session
* 4. Cached session data
*/
export const DirectAPIConfig = {
// API Base Configuration
baseURL: 'https://api.digitalpages.com.br',
projectKey: 'e3894d14dbb743d78a7efc5819edc52e',
apiEnv: 'prd',
// Authentication Data (to be populated)
authentication: {
accessToken: null, // JWT token from localStorage['rdp-composer-user-data'].access_token
tokenType: 'Bearer', // Usually 'Bearer'
projectUid: null, // From localStorage['rdp-composer-active-project'].uid
connectors: [] // From localStorage['rdp-composer-active-project'].connectors
},
// API Endpoints
endpoints: {
userProfile: '/auth/v1.0/user/me',
projectInfo: '/auth/v1.0/project/uid/{projectUid}',
connectorsList: '/auth/v1.1/connector',
uploadComposition: '/storage/v1.0/upload/connector/uid/{connectorUid}?manual_project_uid={projectUid}',
getComposition: '/storage/v1.0/content?uid={compositionUid}&access_token={accessToken}&project_key={projectKey}&api_env={apiEnv}'
},
// Standard Headers
getHeaders() {
return {
'Authorization': `${this.authentication.tokenType} ${this.authentication.accessToken}`,
'Project-Key': this.projectKey,
'Api-Env': this.apiEnv,
'Content-Type': 'application/json',
'Accept': 'application/json'
};
},
// Get upload headers (no Content-Type for FormData)
getUploadHeaders() {
return {
'Authorization': `${this.authentication.tokenType} ${this.authentication.accessToken}`,
'Project-Key': this.projectKey,
'Api-Env': this.apiEnv
};
},
// Build full URL
buildURL(endpoint, params = {}) {
let url = this.baseURL + endpoint;
// Replace URL parameters
Object.keys(params).forEach(key => {
url = url.replace(`{${key}}`, params[key]);
});
return url;
},
// Load authentication from environment variables
loadFromEnvironment() {
this.authentication.accessToken = process.env.EUCONQUISTO_ACCESS_TOKEN;
this.authentication.tokenType = process.env.EUCONQUISTO_TOKEN_TYPE || 'Bearer';
this.authentication.projectUid = process.env.EUCONQUISTO_PROJECT_UID;
// Load connectors from JSON string
if (process.env.EUCONQUISTO_CONNECTORS) {
try {
this.authentication.connectors = JSON.parse(process.env.EUCONQUISTO_CONNECTORS);
} catch (e) {
console.error('Failed to parse EUCONQUISTO_CONNECTORS:', e.message);
this.authentication.connectors = [];
}
}
return this.isValid();
},
// Load authentication from config object
loadFromConfig(config) {
this.authentication.accessToken = config.accessToken;
this.authentication.tokenType = config.tokenType || 'Bearer';
this.authentication.projectUid = config.projectUid;
this.authentication.connectors = config.connectors || [];
return this.isValid();
},
// Validate configuration
isValid() {
return !!(
this.authentication.accessToken &&
this.authentication.projectUid &&
this.authentication.connectors.length > 0
);
},
// Get validation status
getValidationStatus() {
return {
valid: this.isValid(),
issues: {
missingAccessToken: !this.authentication.accessToken,
missingProjectUid: !this.authentication.projectUid,
missingConnectors: this.authentication.connectors.length === 0,
tokenLength: this.authentication.accessToken?.length || 0,
connectorsCount: this.authentication.connectors.length
}
};
},
// Select optimal connector (same logic as current implementation)
selectOptimalConnector() {
const connectors = this.authentication.connectors;
// Priority 1: ContentManager connector
const contentManager = connectors.find(c =>
c.name && c.name.toLowerCase().includes('contentmanager')
);
if (contentManager) return contentManager;
// Priority 2: Composer-specific connector
const composer = connectors.find(c =>
c.name && c.name.toLowerCase().includes('composer')
);
if (composer) return composer;
// Priority 3: First connector with upload permissions
const uploadCapable = connectors.find(c =>
c.permissions && c.permissions.includes && c.permissions.includes('upload')
);
if (uploadCapable) return uploadCapable;
// Priority 4: Any available connector
return connectors[0] || null;
},
// Get configuration summary for debugging
getSummary() {
return {
baseURL: this.baseURL,
projectKey: this.projectKey,
apiEnv: this.apiEnv,
authentication: {
hasAccessToken: !!this.authentication.accessToken,
tokenLength: this.authentication.accessToken?.length || 0,
tokenType: this.authentication.tokenType,
hasProjectUid: !!this.authentication.projectUid,
connectorsCount: this.authentication.connectors.length,
selectedConnector: this.selectOptimalConnector()?.name || 'None'
},
validation: this.getValidationStatus()
};
}
};
/**
* Environment variable template for direct API authentication
*/
export const EnvironmentTemplate = {
// Add these to your environment or MCP configuration:
EUCONQUISTO_ACCESS_TOKEN: "your_jwt_access_token_here",
EUCONQUISTO_TOKEN_TYPE: "Bearer",
EUCONQUISTO_PROJECT_UID: "your_project_uid_here",
EUCONQUISTO_CONNECTORS: '[{"uid":"connector_uid","name":"ContentManager","permissions":["upload"]}]'
};
/**
* MCP Configuration template for Claude Desktop
*/
export const MCPConfigTemplate = {
"mcpServers": {
"euconquisto-composer": {
"command": "node",
"args": [
"--max-old-space-size=4096",
"/path/to/euconquisto-composer-mcp-poc/dist/index.js"
],
"env": {
"NODE_ENV": "production",
"EUCONQUISTO_ACCESS_TOKEN": "your_jwt_access_token_here",
"EUCONQUISTO_TOKEN_TYPE": "Bearer",
"EUCONQUISTO_PROJECT_UID": "your_project_uid_here",
"EUCONQUISTO_CONNECTORS": "[{\"uid\":\"connector_uid\",\"name\":\"ContentManager\",\"permissions\":[\"upload\"]}]"
}
}
}
};
// CLI testing utility
if (import.meta.url === `file://${process.argv[1]}`) {
console.log('š§ Direct API Configuration v1.0.0');
console.log('=====================================\n');
// Test environment loading
console.log('š Environment Variables Status:');
const envStatus = DirectAPIConfig.loadFromEnvironment();
console.log(` Valid Configuration: ${envStatus ? 'ā
' : 'ā'}`);
// Show configuration summary
console.log('\nš Configuration Summary:');
const summary = DirectAPIConfig.getSummary();
console.log(JSON.stringify(summary, null, 2));
// Show templates
console.log('\nš Environment Template:');
console.log('Add these environment variables:');
Object.keys(EnvironmentTemplate).forEach(key => {
console.log(`export ${key}="${EnvironmentTemplate[key]}"`);
});
console.log('\nš MCP Configuration Template:');
console.log('Add to Claude Desktop config:');
console.log(JSON.stringify(MCPConfigTemplate, null, 2));
}