polyv-live-cli
Version:
CLI tool for managing PolyV live streaming services.
124 lines • 4.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadAuthFromCLI = loadAuthFromCLI;
exports.loadAuthFromEnv = loadAuthFromEnv;
exports.mergeAuthOptions = mergeAuthOptions;
exports.validateAuthConfig = validateAuthConfig;
exports.createAuthErrorMessage = createAuthErrorMessage;
exports.loadAuthConfig = loadAuthConfig;
const auth_1 = require("../types/auth");
const errors_1 = require("../utils/errors");
function loadAuthFromCLI(cliOptions) {
const authOptions = {};
if (typeof cliOptions[auth_1.AUTH_CLI_OPTIONS.APP_ID] === 'string') {
authOptions.appId = cliOptions[auth_1.AUTH_CLI_OPTIONS.APP_ID];
}
if (typeof cliOptions[auth_1.AUTH_CLI_OPTIONS.APP_SECRET] === 'string') {
authOptions.appSecret = cliOptions[auth_1.AUTH_CLI_OPTIONS.APP_SECRET];
}
if (typeof cliOptions[auth_1.AUTH_CLI_OPTIONS.USER_ID] === 'string') {
authOptions.userId = cliOptions[auth_1.AUTH_CLI_OPTIONS.USER_ID];
}
return authOptions;
}
function loadAuthFromEnv() {
const authOptions = {};
const appId = process.env[auth_1.AUTH_ENV_VARS.APP_ID];
if (appId !== undefined) {
authOptions.appId = appId.trim();
}
const appSecret = process.env[auth_1.AUTH_ENV_VARS.APP_SECRET];
if (appSecret !== undefined) {
authOptions.appSecret = appSecret.trim();
}
const userId = process.env[auth_1.AUTH_ENV_VARS.USER_ID];
if (userId !== undefined) {
authOptions.userId = userId.trim();
}
return authOptions;
}
function mergeAuthOptions(cliOptions, envOptions) {
const mergedOptions = {};
const sources = {};
if (cliOptions.appId !== undefined) {
mergedOptions.appId = cliOptions.appId;
sources.appIdSource = 'cli';
}
else if (envOptions.appId !== undefined) {
mergedOptions.appId = envOptions.appId;
sources.appIdSource = 'env';
}
if (cliOptions.appSecret !== undefined) {
mergedOptions.appSecret = cliOptions.appSecret;
sources.appSecretSource = 'cli';
}
else if (envOptions.appSecret !== undefined) {
mergedOptions.appSecret = envOptions.appSecret;
sources.appSecretSource = 'env';
}
if (cliOptions.userId !== undefined) {
mergedOptions.userId = cliOptions.userId;
sources.userIdSource = 'cli';
}
else if (envOptions.userId !== undefined) {
mergedOptions.userId = envOptions.userId;
sources.userIdSource = 'env';
}
return { options: mergedOptions, sources };
}
function validateAuthConfig(authOptions) {
return !!(authOptions.appId && authOptions.appSecret);
}
function createAuthErrorMessage(authOptions, sources) {
const missing = [];
if (!authOptions.appId) {
missing.push('appId');
}
if (!authOptions.appSecret) {
missing.push('appSecret');
}
if (missing.length === 0) {
return 'Authentication configuration is valid';
}
const missingStr = missing.join(', ');
const hasCliSource = Object.values(sources).some(source => source === 'cli');
const hasEnvSource = Object.values(sources).some(source => source === 'env');
let message = `Missing required authentication parameters: ${missingStr}\n\n`;
message += 'You can provide authentication in two ways:\n';
message += '1. Command line: --appId <ID> --appSecret <SECRET> [--userId <USER_ID>]\n';
message += '2. Environment variables: POLYV_APP_ID, POLYV_APP_SECRET, POLYV_USER_ID\n\n';
if (hasCliSource || hasEnvSource) {
message += 'Current configuration sources:\n';
if (sources.appIdSource) {
message += ` appId: ${sources.appIdSource}\n`;
}
if (sources.appSecretSource) {
message += ` appSecret: ${sources.appSecretSource}\n`;
}
if (sources.userIdSource) {
message += ` userId: ${sources.userIdSource}\n`;
}
}
return message.trim();
}
function loadAuthConfig(cliOptions = {}) {
const cliAuth = loadAuthFromCLI(cliOptions);
const envAuth = loadAuthFromEnv();
const { options: mergedOptions, sources } = mergeAuthOptions(cliAuth, envAuth);
const isValid = validateAuthConfig(mergedOptions);
if (!isValid) {
const errorMessage = createAuthErrorMessage(mergedOptions, sources);
throw new errors_1.ConfigurationError(errorMessage);
}
const config = {
appId: mergedOptions.appId,
appSecret: mergedOptions.appSecret,
...(mergedOptions.userId && { userId: mergedOptions.userId }),
};
return {
config,
sources,
isValid: true,
};
}
//# sourceMappingURL=auth.js.map