qapinterface
Version:
Comprehensive API utilities for Node.js applications including authentication, security, request processing, and response handling with zero external dependencies
53 lines (46 loc) • 1.62 kB
JavaScript
/**
* Environment Variable Validator
* Single Responsibility: Validate environment variables for authentication ONLY
*/
const localVars = require('../../config/localVars');
/**
* Validates that required API key environment variable is set
* @param {string} envKeyName - Name of the environment variable to check
* @returns {object} Validation result with status and details
*/
function validateApiKeyEnvironment(envKeyName) {
const apiKey = process.env[envKeyName];
if (!apiKey) {
return {
valid: false,
error: `Environment variable ${envKeyName} is not set`,
critical: true,
suggestion: `Set ${envKeyName} environment variable with a secure API key`
};
}
if (apiKey.length < localVars.DEFAULT_API_KEY_MIN_LENGTH) {
return {
valid: false,
error: `API key in ${envKeyName} is too short (minimum ${localVars.DEFAULT_API_KEY_MIN_LENGTH} characters)`,
critical: false,
suggestion: 'Use a longer, more secure API key'
};
}
return {
valid: true,
message: `API key environment variable ${envKeyName} is properly configured`
};
}
/**
* Checks if current environment should skip authentication
* @param {string} [environment] - Environment to check (defaults to NODE_ENV)
* @returns {boolean} True if authentication should be skipped
*/
function shouldSkipAuthentication(environment = localVars.NODE_ENV) {
const skipEnvironments = ['development', 'dev', 'test', 'testing'];
return skipEnvironments.includes(environment?.toLowerCase());
}
module.exports = {
validateApiKeyEnvironment,
shouldSkipAuthentication
};