onairos
Version:
The Onairos Library is a collection of functions that enable Applications to connect and communicate data with Onairos Identities via User Authorization. Integration for developers is seamless, simple and effective for all applications. LLM SDK capabiliti
749 lines (662 loc) ⢠27.4 kB
JavaScript
import express from 'express';
import { authenticateApiKey, smartAuth } from '../middleware/unifiedApiKeyAuth.js';
import { TokenManager } from '../utils/tokenManager.js';
import { ConnectionHealthMonitor } from '../utils/connectionHealth.js';
import { DatabaseUtils } from '../utils/databaseUtils.js';
import { sdkConfig } from '../config/sdk-config.js';
import { oauthConfig } from '../config/oauth-config.js';
const router = express.Router();
const tokenManager = new TokenManager();
const healthMonitor = new ConnectionHealthMonitor();
const dbUtils = new DatabaseUtils();
/**
* Cross-Platform Health Check
*
* Comprehensive health monitoring for all platform connections
* Returns detailed health status and recommendations for each platform
*/
router.get('/health-check/:username', authenticateApiKey, async (req, res) => {
const requestId = Date.now().toString(36) + Math.random().toString(36).substr(2, 9);
const { username } = req.params;
console.log(`\nš [VALIDATION-HEALTH-${requestId}] Starting comprehensive health check for: ${username}`);
try {
// Get user from database
const { user, userType } = await dbUtils.getUserWithSmartLookup(username);
if (!user) {
return res.status(404).json({
success: false,
error: 'User not found',
code: 'USER_NOT_FOUND',
requestId: requestId
});
}
console.log(`ā
[VALIDATION-HEALTH-${requestId}] Found user in ${userType} database`);
// Get user connections
const userConnections = dbUtils.getUserConnections(user, userType);
const enabledPlatforms = Object.keys(sdkConfig.platforms).filter(
platform => sdkConfig.platforms[platform].enabled
);
console.log(`š [VALIDATION-HEALTH-${requestId}] Checking ${enabledPlatforms.length} platforms`);
// Check health for each platform
const platformHealthResults = {};
const recommendations = [];
let overallScore = 0;
let connectedPlatforms = 0;
let healthyPlatforms = 0;
for (const platform of enabledPlatforms) {
console.log(`š [VALIDATION-HEALTH-${requestId}] Checking ${platform} health`);
try {
const healthResult = await healthMonitor.checkConnectionHealth(username, platform);
platformHealthResults[platform] = healthResult;
// Check if platform is connected
const isConnected = userConnections[platform] && userConnections[platform].accessToken;
if (isConnected) {
connectedPlatforms++;
// Calculate platform score
let platformScore = 0;
switch (healthResult.status) {
case 'healthy':
platformScore = 100;
healthyPlatforms++;
break;
case 'expired_refreshable':
platformScore = 75;
recommendations.push({
type: 'token_refresh_available',
platform: platform,
message: `${platform} token is expired but can be refreshed automatically`,
severity: 'warning',
actionRequired: false,
action: 'auto_refresh'
});
break;
case 'expired_no_refresh':
platformScore = 25;
recommendations.push({
type: 'reconnection_required',
platform: platform,
message: `${platform} token is expired and requires reconnection`,
severity: 'error',
actionRequired: true,
action: 'reconnect'
});
break;
case 'invalid_token':
platformScore = 10;
recommendations.push({
type: 'invalid_token',
platform: platform,
message: `${platform} token is invalid and requires reconnection`,
severity: 'error',
actionRequired: true,
action: 'reconnect'
});
break;
case 'error':
platformScore = 0;
recommendations.push({
type: 'connection_error',
platform: platform,
message: `${platform} connection has errors`,
severity: 'error',
actionRequired: true,
action: 'debug'
});
break;
}
overallScore += platformScore;
} else {
// Platform not connected
platformHealthResults[platform] = {
status: 'not_connected',
connected: false,
message: `${platform} is not connected`
};
recommendations.push({
type: 'platform_not_connected',
platform: platform,
message: `${platform} is not connected`,
severity: 'info',
actionRequired: false,
action: 'connect'
});
}
} catch (error) {
console.error(`ā [VALIDATION-HEALTH-${requestId}] Error checking ${platform}:`, error);
platformHealthResults[platform] = {
status: 'error',
connected: false,
error: error.message
};
recommendations.push({
type: 'platform_check_error',
platform: platform,
message: `Error checking ${platform} health: ${error.message}`,
severity: 'error',
actionRequired: true,
action: 'debug'
});
}
}
// Calculate overall score as percentage
const finalOverallScore = connectedPlatforms > 0 ? Math.round(overallScore / connectedPlatforms) : 0;
// Generate overall assessment
let overallStatus = 'unknown';
let overallMessage = '';
if (connectedPlatforms === 0) {
overallStatus = 'no_connections';
overallMessage = 'No platform connections found';
} else if (healthyPlatforms === connectedPlatforms) {
overallStatus = 'healthy';
overallMessage = 'All connected platforms are healthy';
} else if (healthyPlatforms > connectedPlatforms / 2) {
overallStatus = 'mostly_healthy';
overallMessage = 'Most platforms are healthy, some need attention';
} else {
overallStatus = 'needs_attention';
overallMessage = 'Multiple platforms require attention';
}
// Add general recommendations
if (connectedPlatforms < enabledPlatforms.length) {
recommendations.push({
type: 'expand_connections',
message: `Consider connecting to ${enabledPlatforms.length - connectedPlatforms} more platforms for better data coverage`,
severity: 'info',
actionRequired: false,
action: 'connect_more'
});
}
if (overallScore < 50) {
recommendations.push({
type: 'low_health_score',
message: 'Overall connection health is low - multiple platforms need attention',
severity: 'warning',
actionRequired: true,
action: 'repair_connections'
});
}
const response = {
success: true,
requestId: requestId,
username: username,
userType: userType,
timestamp: new Date(),
summary: {
overallStatus: overallStatus,
overallScore: finalOverallScore,
overallMessage: overallMessage,
connectedPlatforms: connectedPlatforms,
healthyPlatforms: healthyPlatforms,
totalPlatforms: enabledPlatforms.length,
needsAttention: connectedPlatforms - healthyPlatforms
},
platforms: platformHealthResults,
recommendations: recommendations,
nextActions: recommendations.filter(r => r.actionRequired).map(r => ({
platform: r.platform,
action: r.action,
message: r.message,
severity: r.severity
}))
};
console.log(`ā
[VALIDATION-HEALTH-${requestId}] Health check completed:`, {
overallScore: finalOverallScore,
connectedPlatforms: connectedPlatforms,
healthyPlatforms: healthyPlatforms,
recommendationCount: recommendations.length
});
return res.status(200).json(response);
} catch (error) {
console.error(`ā [VALIDATION-HEALTH-${requestId}] Error during health check:`, error);
return res.status(500).json({
success: false,
error: 'Failed to perform health check',
code: 'HEALTH_CHECK_ERROR',
requestId: requestId,
details: process.env.NODE_ENV === 'development' ? error.message : undefined
});
}
});
/**
* Connection Repair
*
* Automated connection repair for expired tokens and other issues
*/
router.post('/repair-connections/:username', authenticateApiKey, async (req, res) => {
const requestId = Date.now().toString(36) + Math.random().toString(36).substr(2, 9);
const { username } = req.params;
const { platforms: targetPlatforms } = req.body;
console.log(`\nš§ [VALIDATION-REPAIR-${requestId}] Starting connection repair for: ${username}`);
try {
// Get user from database
const { user, userType } = await dbUtils.getUserWithSmartLookup(username);
if (!user) {
return res.status(404).json({
success: false,
error: 'User not found',
code: 'USER_NOT_FOUND',
requestId: requestId
});
}
// Determine which platforms to repair
const userConnections = dbUtils.getUserConnections(user, userType);
const platformsToRepair = targetPlatforms || Object.keys(userConnections).filter(
platform => userConnections[platform] && userConnections[platform].accessToken
);
console.log(`š§ [VALIDATION-REPAIR-${requestId}] Repairing ${platformsToRepair.length} platforms`);
const repairResults = {};
const successfulRepairs = [];
const failedRepairs = [];
for (const platform of platformsToRepair) {
console.log(`š§ [VALIDATION-REPAIR-${requestId}] Repairing ${platform}`);
try {
// Check current health
const healthResult = await healthMonitor.checkConnectionHealth(username, platform);
repairResults[platform] = {
platform: platform,
beforeRepair: healthResult.status,
repairAttempted: false,
repairSuccess: false,
afterRepair: healthResult.status,
message: '',
error: null
};
// Attempt repair based on health status
switch (healthResult.status) {
case 'expired_refreshable':
console.log(`š§ [VALIDATION-REPAIR-${requestId}] Attempting token refresh for ${platform}`);
try {
const refreshResult = await tokenManager.refreshTokenIfNeeded(username, platform);
repairResults[platform].repairAttempted = true;
repairResults[platform].repairSuccess = refreshResult.success;
repairResults[platform].message = refreshResult.message;
if (refreshResult.success) {
// Re-check health after repair
const newHealthResult = await healthMonitor.checkConnectionHealth(username, platform);
repairResults[platform].afterRepair = newHealthResult.status;
successfulRepairs.push(platform);
} else {
repairResults[platform].error = refreshResult.error;
failedRepairs.push(platform);
}
} catch (refreshError) {
repairResults[platform].repairAttempted = true;
repairResults[platform].repairSuccess = false;
repairResults[platform].error = refreshError.message;
failedRepairs.push(platform);
}
break;
case 'healthy':
repairResults[platform].message = 'Platform is already healthy, no repair needed';
successfulRepairs.push(platform);
break;
case 'expired_no_refresh':
case 'invalid_token':
repairResults[platform].message = 'Manual reconnection required - cannot auto-repair';
repairResults[platform].repairAttempted = false;
failedRepairs.push(platform);
break;
case 'not_connected':
repairResults[platform].message = 'Platform is not connected - cannot repair';
repairResults[platform].repairAttempted = false;
failedRepairs.push(platform);
break;
default:
repairResults[platform].message = `Cannot repair platform with status: ${healthResult.status}`;
repairResults[platform].repairAttempted = false;
failedRepairs.push(platform);
}
} catch (error) {
console.error(`ā [VALIDATION-REPAIR-${requestId}] Error repairing ${platform}:`, error);
repairResults[platform] = {
platform: platform,
beforeRepair: 'error',
repairAttempted: false,
repairSuccess: false,
afterRepair: 'error',
message: `Error during repair: ${error.message}`,
error: error.message
};
failedRepairs.push(platform);
}
}
// Generate summary
const summary = {
totalPlatforms: platformsToRepair.length,
successfulRepairs: successfulRepairs.length,
failedRepairs: failedRepairs.length,
repairRate: platformsToRepair.length > 0 ?
Math.round((successfulRepairs.length / platformsToRepair.length) * 100) : 0
};
// Generate recommendations for failed repairs
const recommendations = failedRepairs.map(platform => {
const result = repairResults[platform];
if (result.beforeRepair === 'expired_no_refresh' || result.beforeRepair === 'invalid_token') {
return {
type: 'manual_reconnection_required',
platform: platform,
message: `${platform} requires manual reconnection - no refresh token available`,
severity: 'error',
actionRequired: true,
action: 'reconnect'
};
} else if (result.beforeRepair === 'not_connected') {
return {
type: 'platform_not_connected',
platform: platform,
message: `${platform} is not connected - establish connection first`,
severity: 'info',
actionRequired: false,
action: 'connect'
};
} else {
return {
type: 'repair_failed',
platform: platform,
message: `${platform} repair failed: ${result.error || 'Unknown error'}`,
severity: 'error',
actionRequired: true,
action: 'debug'
};
}
});
const response = {
success: true,
requestId: requestId,
username: username,
userType: userType,
timestamp: new Date(),
summary: summary,
repairResults: repairResults,
successfulRepairs: successfulRepairs,
failedRepairs: failedRepairs,
recommendations: recommendations
};
console.log(`ā
[VALIDATION-REPAIR-${requestId}] Repair completed:`, {
totalPlatforms: summary.totalPlatforms,
successful: summary.successfulRepairs,
failed: summary.failedRepairs,
repairRate: summary.repairRate
});
return res.status(200).json(response);
} catch (error) {
console.error(`ā [VALIDATION-REPAIR-${requestId}] Error during repair:`, error);
return res.status(500).json({
success: false,
error: 'Failed to repair connections',
code: 'REPAIR_ERROR',
requestId: requestId,
details: process.env.NODE_ENV === 'development' ? error.message : undefined
});
}
});
/**
* Platform Migration Status
*
* Check migration status and provide upgrade recommendations
*/
router.get('/migration-status/:username', authenticateApiKey, async (req, res) => {
const requestId = Date.now().toString(36) + Math.random().toString(36).substr(2, 9);
const { username } = req.params;
console.log(`\nš [VALIDATION-MIGRATION-${requestId}] Checking migration status for: ${username}`);
try {
// Get user from database
const { user, userType } = await dbUtils.getUserWithSmartLookup(username);
if (!user) {
return res.status(404).json({
success: false,
error: 'User not found',
code: 'USER_NOT_FOUND',
requestId: requestId
});
}
// Get user connections
const userConnections = dbUtils.getUserConnections(user, userType);
const platformStatuses = {};
const migrationNeeded = [];
const migrationRecommendations = [];
// Check each connected platform
for (const [platform, connection] of Object.entries(userConnections)) {
if (connection && connection.accessToken) {
const needsUpgrade = !connection.refreshToken;
const isLegacyConnection = !connection.lastValidated ||
(connection.lastValidated && new Date() - new Date(connection.lastValidated) > 30 * 24 * 60 * 60 * 1000);
platformStatuses[platform] = {
connected: true,
hasRefreshToken: !!connection.refreshToken,
needsUpgrade: needsUpgrade,
isLegacyConnection: isLegacyConnection,
lastValidated: connection.lastValidated,
connectedAt: connection.connectedAt,
migrationScore: needsUpgrade ? 25 : (isLegacyConnection ? 50 : 100)
};
if (needsUpgrade) {
migrationNeeded.push(platform);
migrationRecommendations.push({
type: 'refresh_token_upgrade',
platform: platform,
message: `${platform} connection needs upgrade to include refresh token`,
severity: 'warning',
actionRequired: false,
action: 'upgrade_connection',
benefits: [
'Automatic token refresh',
'Reduced connection failures',
'Better user experience'
]
});
}
if (isLegacyConnection) {
migrationRecommendations.push({
type: 'legacy_connection',
platform: platform,
message: `${platform} connection is legacy and should be refreshed`,
severity: 'info',
actionRequired: false,
action: 'refresh_connection'
});
}
}
}
// Calculate overall migration score
const connectedPlatforms = Object.keys(platformStatuses).length;
const totalScore = Object.values(platformStatuses).reduce((sum, status) => sum + status.migrationScore, 0);
const overallMigrationScore = connectedPlatforms > 0 ? Math.round(totalScore / connectedPlatforms) : 100;
// Determine migration status
let migrationStatus = 'completed';
let migrationMessage = 'All connections are up to date';
if (migrationNeeded.length > 0) {
migrationStatus = 'needed';
migrationMessage = `${migrationNeeded.length} platforms need migration`;
} else if (overallMigrationScore < 75) {
migrationStatus = 'recommended';
migrationMessage = 'Some connections could benefit from refresh';
}
const response = {
success: true,
requestId: requestId,
username: username,
userType: userType,
timestamp: new Date(),
migrationStatus: {
status: migrationStatus,
message: migrationMessage,
overallScore: overallMigrationScore,
connectedPlatforms: connectedPlatforms,
platformsNeedingMigration: migrationNeeded.length,
migrationProgress: connectedPlatforms > 0 ?
Math.round(((connectedPlatforms - migrationNeeded.length) / connectedPlatforms) * 100) : 100
},
platformStatuses: platformStatuses,
migrationNeeded: migrationNeeded,
recommendations: migrationRecommendations
};
console.log(`ā
[VALIDATION-MIGRATION-${requestId}] Migration status checked:`, {
migrationStatus: migrationStatus,
overallScore: overallMigrationScore,
platformsNeedingMigration: migrationNeeded.length
});
return res.status(200).json(response);
} catch (error) {
console.error(`ā [VALIDATION-MIGRATION-${requestId}] Error checking migration status:`, error);
return res.status(500).json({
success: false,
error: 'Failed to check migration status',
code: 'MIGRATION_STATUS_ERROR',
requestId: requestId,
details: process.env.NODE_ENV === 'development' ? error.message : undefined
});
}
});
/**
* System Health Check
*
* Check overall system health and configuration
*/
router.get('/system-health', authenticateApiKey, async (req, res) => {
const requestId = Date.now().toString(36) + Math.random().toString(36).substr(2, 9);
console.log(`\nš [VALIDATION-SYSTEM-${requestId}] Checking system health`);
try {
const healthChecks = {
configuration: { status: 'unknown', message: '', details: {} },
database: { status: 'unknown', message: '', details: {} },
platforms: { status: 'unknown', message: '', details: {} },
authentication: { status: 'unknown', message: '', details: {} }
};
// Check configuration
try {
const configValidation = sdkConfig.features ? { valid: true, errors: [], warnings: [] } : { valid: false, errors: ['Configuration not loaded'], warnings: [] };
healthChecks.configuration = {
status: configValidation.valid ? 'healthy' : 'error',
message: configValidation.valid ? 'Configuration is valid' : 'Configuration has errors',
details: {
valid: configValidation.valid,
errors: configValidation.errors || [],
warnings: configValidation.warnings || [],
enabledFeatures: Object.keys(sdkConfig.features || {}).filter(f => sdkConfig.features[f]),
enabledPlatforms: Object.keys(sdkConfig.platforms || {}).filter(p => sdkConfig.platforms[p]?.enabled)
}
};
} catch (error) {
healthChecks.configuration = {
status: 'error',
message: `Configuration error: ${error.message}`,
details: { error: error.message }
};
}
// Check database connections
try {
// This would normally test database connectivity
healthChecks.database = {
status: 'healthy',
message: 'Database connections are healthy',
details: {
primaryDatabase: 'connected',
secondaryDatabase: 'connected',
dualDatabaseSupport: sdkConfig.features?.crossDatabaseSync || false
}
};
} catch (error) {
healthChecks.database = {
status: 'error',
message: `Database error: ${error.message}`,
details: { error: error.message }
};
}
// Check platform configurations
try {
const platformHealths = {};
const enabledPlatforms = Object.keys(sdkConfig.platforms || {}).filter(p => sdkConfig.platforms[p]?.enabled);
for (const platform of enabledPlatforms) {
const platformConfig = oauthConfig[platform];
platformHealths[platform] = {
configured: !!platformConfig,
hasClientId: !!(platformConfig?.clientId),
hasClientSecret: !!(platformConfig?.clientSecret),
hasRequiredScopes: !!(platformConfig?.scopes && platformConfig.scopes.length > 0)
};
}
const healthyPlatforms = Object.values(platformHealths).filter(p => p.configured && p.hasClientId && p.hasClientSecret).length;
healthChecks.platforms = {
status: healthyPlatforms === enabledPlatforms.length ? 'healthy' : 'warning',
message: `${healthyPlatforms}/${enabledPlatforms.length} platforms configured correctly`,
details: {
enabledPlatforms: enabledPlatforms.length,
configuredPlatforms: healthyPlatforms,
platformHealths: platformHealths
}
};
} catch (error) {
healthChecks.platforms = {
status: 'error',
message: `Platform check error: ${error.message}`,
details: { error: error.message }
};
}
// Check authentication
try {
healthChecks.authentication = {
status: 'healthy',
message: 'Authentication system is functioning',
details: {
jwtEnabled: !!(sdkConfig.authentication?.jwt?.secretKey),
apiKeyEnabled: true,
dualAuthEnabled: sdkConfig.features?.dualAuthentication || false,
mfaEnabled: sdkConfig.authentication?.mfa?.enabled || false
}
};
} catch (error) {
healthChecks.authentication = {
status: 'error',
message: `Authentication error: ${error.message}`,
details: { error: error.message }
};
}
// Calculate overall health
const healthStatuses = Object.values(healthChecks).map(check => check.status);
const errorCount = healthStatuses.filter(s => s === 'error').length;
const warningCount = healthStatuses.filter(s => s === 'warning').length;
let overallStatus = 'healthy';
let overallMessage = 'All systems are healthy';
if (errorCount > 0) {
overallStatus = 'error';
overallMessage = `${errorCount} critical issues found`;
} else if (warningCount > 0) {
overallStatus = 'warning';
overallMessage = `${warningCount} warnings found`;
}
const response = {
success: true,
requestId: requestId,
timestamp: new Date(),
overallHealth: {
status: overallStatus,
message: overallMessage,
score: Math.round(((healthStatuses.length - errorCount - warningCount * 0.5) / healthStatuses.length) * 100)
},
healthChecks: healthChecks,
systemInfo: {
sdkVersion: sdkConfig.sdk?.version || 'unknown',
environment: process.env.NODE_ENV || 'development',
uptime: process.uptime(),
nodeVersion: process.version
}
};
console.log(`ā
[VALIDATION-SYSTEM-${requestId}] System health checked:`, {
overallStatus: overallStatus,
errors: errorCount,
warnings: warningCount
});
return res.status(200).json(response);
} catch (error) {
console.error(`ā [VALIDATION-SYSTEM-${requestId}] Error checking system health:`, error);
return res.status(500).json({
success: false,
error: 'Failed to check system health',
code: 'SYSTEM_HEALTH_ERROR',
requestId: requestId,
details: process.env.NODE_ENV === 'development' ? error.message : undefined
});
}
});
export default router;