UNPKG

polyv-live-cli

Version:

CLI tool for managing PolyV live streaming services.

481 lines 18.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthenticationSourceManager = exports.ConfigAuthDetector = exports.EnvironmentAuthDetector = exports.DefaultAccountAuthDetector = exports.SessionAuthDetector = exports.CommandLineAuthDetector = exports.CommandLineAccountDetector = void 0; const auth_source_types_1 = require("../types/auth-source.types"); const session_state_1 = require("./session-state"); const account_config_1 = require("./account-config"); const auth_1 = require("../types/auth"); class CommandLineAccountDetector { constructor(accountManager) { this.type = 'command-line-account'; this.priority = auth_source_types_1.AUTH_PRIORITY.COMMAND_LINE_ACCOUNT; this.accountManager = accountManager || new account_config_1.AccountConfigManager(); } detect(options) { if (!options) { return null; } const accountName = options['account']; if (!accountName) { return null; } try { if (!this.accountManager.accountExists(accountName)) { return { type: this.type, priority: this.priority, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], accountName, setAt: new Date(), context: { error: 'account_not_found', message: `指定的账号 '${accountName}' 不存在`, }, }, }; } const account = this.accountManager.getAccount(accountName); if (!account) { return null; } return { type: this.type, priority: this.priority, appId: account.appId, appSecret: account.appSecret, userId: account.userId, baseUrl: account.baseUrl, environment: account.environment, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], accountName, setAt: new Date(), context: { providedAccountName: accountName, }, }, }; } catch (error) { return { type: this.type, priority: this.priority, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], accountName, setAt: new Date(), context: { error: 'detection_failed', message: error instanceof Error ? error.message : '账号检测失败', }, }, }; } } isAvailable(options) { const source = this.detect(options); return source !== null && !!source.appId && !!source.appSecret; } } exports.CommandLineAccountDetector = CommandLineAccountDetector; class CommandLineAuthDetector { constructor() { this.type = 'command-line'; this.priority = auth_source_types_1.AUTH_PRIORITY.COMMAND_LINE; } detect(options) { if (!options) { return null; } const appId = options[auth_1.AUTH_CLI_OPTIONS.APP_ID]; const appSecret = options[auth_1.AUTH_CLI_OPTIONS.APP_SECRET]; const userId = options[auth_1.AUTH_CLI_OPTIONS.USER_ID]; if (!appId && !appSecret && !userId) { return null; } return { type: this.type, priority: this.priority, appId, appSecret, userId, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], setAt: new Date(), context: { providedParams: { appId: !!appId, appSecret: !!appSecret, userId: !!userId, }, }, }, }; } isAvailable(options) { if (!options) { return false; } const appId = options[auth_1.AUTH_CLI_OPTIONS.APP_ID]; const appSecret = options[auth_1.AUTH_CLI_OPTIONS.APP_SECRET]; return !!(appId && appSecret); } } exports.CommandLineAuthDetector = CommandLineAuthDetector; class SessionAuthDetector { constructor(sessionManager, accountManager) { this.type = 'session'; this.priority = auth_source_types_1.AUTH_PRIORITY.SESSION; this.sessionManager = sessionManager || new session_state_1.SessionStateManager(); this.accountManager = accountManager || new account_config_1.AccountConfigManager(); } detect(_options) { try { const sessionAccount = this.sessionManager.getCurrentSessionAccount(); if (!sessionAccount) { return null; } if (!this.accountManager.accountExists(sessionAccount)) { return { type: this.type, priority: this.priority, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], accountName: sessionAccount, setAt: new Date(), context: { error: 'account_not_found', message: `会话账号 '${sessionAccount}' 不存在`, }, }, }; } const account = this.accountManager.getAccount(sessionAccount); if (!account) { return null; } return { type: this.type, priority: this.priority, appId: account.appId, appSecret: account.appSecret, userId: account.userId, baseUrl: account.baseUrl, environment: account.environment, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], accountName: sessionAccount, setAt: new Date(), context: { sessionState: this.sessionManager.getSessionState(), }, }, }; } catch (error) { return { type: this.type, priority: this.priority, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], setAt: new Date(), context: { error: 'detection_failed', message: error instanceof Error ? error.message : '会话账号检测失败', }, }, }; } } isAvailable(options) { const source = this.detect(options); return source !== null && !!source.appId && !!source.appSecret; } } exports.SessionAuthDetector = SessionAuthDetector; class DefaultAccountAuthDetector { constructor(accountManager) { this.type = 'default-account'; this.priority = auth_source_types_1.AUTH_PRIORITY.DEFAULT_ACCOUNT; this.accountManager = accountManager || new account_config_1.AccountConfigManager(); } detect(_options) { try { const defaultAccountName = this.accountManager.getDefaultAccount(); if (!defaultAccountName) { return null; } if (!this.accountManager.accountExists(defaultAccountName)) { return { type: this.type, priority: this.priority, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], accountName: defaultAccountName, setAt: new Date(), context: { error: 'default_account_not_found', message: `默认账号 '${defaultAccountName}' 不存在`, }, }, }; } const account = this.accountManager.getAccount(defaultAccountName); if (!account) { return null; } return { type: this.type, priority: this.priority, appId: account.appId, appSecret: account.appSecret, userId: account.userId, baseUrl: account.baseUrl, environment: account.environment, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], accountName: defaultAccountName, setAt: new Date(), context: { defaultAccount: defaultAccountName, }, }, }; } catch (error) { return { type: this.type, priority: this.priority, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], setAt: new Date(), context: { error: 'detection_failed', message: error instanceof Error ? error.message : '默认账号检测失败', }, }, }; } } isAvailable(options) { const source = this.detect(options); return source !== null && !!source.appId && !!source.appSecret; } } exports.DefaultAccountAuthDetector = DefaultAccountAuthDetector; class EnvironmentAuthDetector { constructor() { this.type = 'environment'; this.priority = auth_source_types_1.AUTH_PRIORITY.ENVIRONMENT; } detect(_options) { const appId = process.env[auth_1.AUTH_ENV_VARS.APP_ID]; const appSecret = process.env[auth_1.AUTH_ENV_VARS.APP_SECRET]; const userId = process.env[auth_1.AUTH_ENV_VARS.USER_ID]; if (!appId && !appSecret && !userId) { return null; } return { type: this.type, priority: this.priority, appId: appId?.trim(), appSecret: appSecret?.trim(), userId: userId?.trim(), metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], setAt: new Date(), context: { envVars: { [auth_1.AUTH_ENV_VARS.APP_ID]: !!appId, [auth_1.AUTH_ENV_VARS.APP_SECRET]: !!appSecret, [auth_1.AUTH_ENV_VARS.USER_ID]: !!userId, }, }, }, }; } isAvailable(options) { const source = this.detect(options); return source !== null && !!source.appId && !!source.appSecret; } } exports.EnvironmentAuthDetector = EnvironmentAuthDetector; class ConfigAuthDetector { constructor() { this.type = 'config'; this.priority = auth_source_types_1.AUTH_PRIORITY.CONFIG; } detect(_options) { try { const { globalConfig } = require('./global'); if (!globalConfig.exists()) { return null; } const config = globalConfig.load(); const appId = config.appId; const appSecret = config.appSecret; const userId = config.userId; if (!appId && !appSecret && !userId) { return null; } return { type: this.type, priority: this.priority, appId, appSecret, userId, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], setAt: new Date(), context: { configFile: globalConfig.getConfigPath(), configExists: globalConfig.exists(), validation: globalConfig.validate(config), }, }, }; } catch (error) { return { type: this.type, priority: this.priority, metadata: { source: auth_source_types_1.SOURCE_DESCRIPTIONS[this.type], setAt: new Date(), context: { error: 'detection_failed', message: error instanceof Error ? error.message : '全局配置检测失败', }, }, }; } } isAvailable(options) { const source = this.detect(options); return source !== null && !!source.appId && !!source.appSecret; } } exports.ConfigAuthDetector = ConfigAuthDetector; class AuthenticationSourceManager { constructor(sessionManager, accountManager) { this.detectors = [ new CommandLineAccountDetector(accountManager), new CommandLineAuthDetector(), new SessionAuthDetector(sessionManager, accountManager), new DefaultAccountAuthDetector(accountManager), new EnvironmentAuthDetector(), new ConfigAuthDetector(), ]; this.detectors.sort((a, b) => a.priority - b.priority); } getAvailableSources(options) { const sources = []; for (const detector of this.detectors) { try { const source = detector.detect(options); if (source) { sources.push(source); } } catch (error) { console.warn(`Warning: Authentication source detection failed for ${detector.type}: ${error}`); } } return sources; } getAuthenticationSource(options) { const sources = this.getAvailableSources(options); for (const source of sources) { if (this.isCompleteAuthentication(source)) { return source; } } return null; } isCompleteAuthentication(source) { return !!(source.appId && source.appSecret); } getCredentials(options) { const source = this.getAuthenticationSource(options); if (!source || !this.isCompleteAuthentication(source)) { return null; } return { appId: source.appId, appSecret: source.appSecret, ...(source.userId && { userId: source.userId }), }; } getDiagnostics(options) { const availableSources = this.getAvailableSources(options); const selectedSource = this.getAuthenticationSource(options); const errors = []; const warnings = []; const suggestions = []; for (const source of availableSources) { if (!this.isCompleteAuthentication(source)) { const missing = []; if (!source.appId) missing.push('appId'); if (!source.appSecret) missing.push('appSecret'); warnings.push(`${source.metadata.source} 缺少必需参数: ${missing.join(', ')}`); } if (source.metadata.context?.['error']) { errors.push(`${source.metadata.source}: ${source.metadata.context['message'] || source.metadata.context['error']}`); } } if (!selectedSource) { if (availableSources.length === 0) { suggestions.push('使用 \'polyv-cli account add <account-name>\' 添加账号'); suggestions.push('设置环境变量: POLYV_APP_ID, POLYV_APP_SECRET'); suggestions.push('使用命令行参数: --app-id, --app-secret'); } else { suggestions.push('检查认证参数是否完整 (需要 appId 和 appSecret)'); const sessionSource = availableSources.find(s => s.type === 'session'); if (sessionSource && sessionSource.metadata.context?.['error'] === 'account_not_found') { suggestions.push(`会话账号 '${sessionSource.metadata.accountName}' 不存在,使用 'polyv-cli use <valid-account>' 切换`); } } } const result = { availableSources, errors, warnings, suggestions, }; if (selectedSource) { result.selectedSource = selectedSource; } return result; } getStatusMessage(options) { const diagnostics = this.getDiagnostics(options); if (diagnostics.selectedSource) { const source = diagnostics.selectedSource; const sourceDesc = source.metadata.accountName ? `${source.metadata.source} (${source.metadata.accountName})` : source.metadata.source; return `使用认证来源: ${sourceDesc}`; } if (diagnostics.errors.length > 0) { return `Auth configuration is incomplete`; } if (diagnostics.warnings.length > 0) { return `Auth configuration is incomplete`; } return `Auth configuration is incomplete`; } getSourceByType(type, options) { const detector = this.detectors.find(d => d.type === type); return detector ? detector.detect(options) : null; } isSourceAvailable(type, options) { const detector = this.detectors.find(d => d.type === type); return detector ? detector.isAvailable(options) : false; } } exports.AuthenticationSourceManager = AuthenticationSourceManager; //# sourceMappingURL=auth-source-manager.js.map