mcp-swagger-parser
Version:
Enterprise-grade OpenAPI/Swagger specification parser for Model Context Protocol (MCP) projects
148 lines • 5.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BearerAuthManager = void 0;
const types_1 = require("./types");
/**
* Bearer Token 认证管理器
* 负责处理Bearer Token认证的所有逻辑
*/
class BearerAuthManager {
constructor(config) {
this.config = config;
if (!config.bearer) {
throw new types_1.AuthError(types_1.AuthErrorType.CONFIG_INVALID, 'Bearer configuration is required for Bearer authentication');
}
this.bearerConfig = config.bearer;
this.validateConfig();
}
/**
* 获取认证头
*/
async getAuthHeaders(context) {
try {
const token = await this.getToken();
const headers = {
'Authorization': `Bearer ${token}`
};
// 调试模式下记录认证信息(不记录完整token)
if (this.config.debug) {
console.debug(`[BearerAuth] Added Authorization header for ${context?.method || 'unknown'} ${context?.path || 'unknown'}`);
console.debug(`[BearerAuth] Token source: ${this.bearerConfig.source}`);
console.debug(`[BearerAuth] Token preview: ${token.substring(0, 10)}...`);
}
return headers;
}
catch (error) {
throw new types_1.AuthError(types_1.AuthErrorType.TOKEN_INVALID, `Failed to get Bearer token: ${error instanceof Error ? error.message : 'Unknown error'}`, { context }, error instanceof Error ? error : undefined);
}
}
/**
* 获取Token
*/
async getToken() {
switch (this.bearerConfig.source) {
case 'static':
return this.getStaticToken();
case 'env':
return this.getEnvToken();
case 'function':
return this.getFunctionToken();
default:
throw new types_1.AuthError(types_1.AuthErrorType.CONFIG_INVALID, `Unsupported Bearer token source: ${this.bearerConfig.source}`);
}
}
/**
* 验证配置
*/
validateConfig() {
const { bearer } = this.config;
if (!bearer) {
throw new types_1.AuthError(types_1.AuthErrorType.CONFIG_INVALID, 'Bearer configuration is required');
}
switch (bearer.source) {
case 'static':
if (!bearer.token || bearer.token.trim() === '') {
throw new types_1.AuthError(types_1.AuthErrorType.TOKEN_MISSING, 'Bearer token is required for static source');
}
break;
case 'env':
if (!bearer.envName || bearer.envName.trim() === '') {
throw new types_1.AuthError(types_1.AuthErrorType.CONFIG_INVALID, 'Environment variable name is required for env source');
}
break;
case 'function':
if (!bearer.provider || typeof bearer.provider !== 'function') {
throw new types_1.AuthError(types_1.AuthErrorType.CONFIG_INVALID, 'Provider function is required for function source');
}
break;
default:
throw new types_1.AuthError(types_1.AuthErrorType.CONFIG_INVALID, `Unsupported Bearer token source: ${bearer.source}`);
}
return true;
}
/**
* 处理认证错误
*/
async handleAuthError(error) {
if (this.config.debug) {
console.error('[BearerAuth] Authentication error:', {
type: error.type,
message: error.message,
details: error.details
});
}
// 可以在这里添加重试逻辑、刷新token等处理
switch (error.type) {
case types_1.AuthErrorType.TOKEN_EXPIRED:
// 未来可以添加token刷新逻辑
break;
case types_1.AuthErrorType.TOKEN_INVALID:
// 未来可以添加token重新获取逻辑
break;
default:
// 其他错误直接抛出
break;
}
}
/**
* 获取静态Token
*/
getStaticToken() {
const token = this.bearerConfig.token;
if (!token || token.trim() === '') {
throw new types_1.AuthError(types_1.AuthErrorType.TOKEN_MISSING, 'Static Bearer token is empty');
}
return token.trim();
}
/**
* 从环境变量获取Token
*/
getEnvToken() {
const envName = this.bearerConfig.envName || 'API_TOKEN';
const token = process.env[envName];
if (!token || token.trim() === '') {
throw new types_1.AuthError(types_1.AuthErrorType.ENV_VAR_MISSING, `Environment variable '${envName}' is not set or empty`, { envName });
}
return token.trim();
}
/**
* 通过函数获取Token
*/
async getFunctionToken() {
if (!this.bearerConfig.provider) {
throw new types_1.AuthError(types_1.AuthErrorType.CONFIG_INVALID, 'Token provider function is not configured');
}
try {
const token = await this.bearerConfig.provider();
if (!token || token.trim() === '') {
throw new types_1.AuthError(types_1.AuthErrorType.TOKEN_MISSING, 'Provider function returned empty token');
}
return token.trim();
}
catch (error) {
throw new types_1.AuthError(types_1.AuthErrorType.TOKEN_INVALID, `Failed to get token from provider: ${error instanceof Error ? error.message : 'Unknown error'}`, undefined, error instanceof Error ? error : undefined);
}
}
}
exports.BearerAuthManager = BearerAuthManager;
//# sourceMappingURL=bearer-auth.js.map