@mcp-abap-adt/头部验证器

MCP ABAP ADT的标头验证器-验证身份验证标头并确定其优先级。
特性
- ✅ 标头验证:验证MCP ABAP ADT服务器的身份验证标头
- ✅ 优先级系统:自动确定身份验证方法的优先级
- ✅ 错误报告:详细的错误消息和警告
- ✅ 类型安全:完全支持TypeScript和类型定义
责任和设计原则
核心开发原则
仅接口通信该方案遵循一个基本的发展原则: 所有与外部依赖关系的交互都只能通过接口进行代码知道 没有超出接口中定义的内容.
这意味着:
- 不知道其他包中的具体实现类
- 不了解接口中未定义的内部数据结构或方法
- 不假设接口契约之外的实现行为
- 不访问接口中未明确定义的属性或方法
这一原则确保:
- 松散结合:验证器与其他包中的具体实现解耦
- 灵活性:可以添加新的实现,而无需修改验证器
- 可测试性:易于模拟测试依赖关系
- 可维护性:对实现的更改不会影响验证器
包装责任
该包负责:
- 标头验证:验证来自HTTP请求的身份验证标头
- 优先决议:根据标头存在和优先级规则确定使用哪种身份验证方法
- 配置提取:从标头中提取身份验证配置
- 错误报告:提供详细的验证错误和警告
这个包有什么作用
- 验证标头:检查身份验证标头的有效性和完整性
- 确定方法的优先级:确定身份验证方法优先级(SAP目标>MCP目标>JWT令牌>基本身份验证)
- 提取配置:提取物
SapConfig来自已验证的标头 - 报告错误:提供无效配置的详细错误消息和警告
- 类型安全性:返回带有配置对象的类型化验证结果
此软件包不做什么
- 不处理身份验证:身份验证由以下人员处理
@mcp-abap-adt/connection和@mcp-abap-adt/auth-broker - 不管理令牌:令牌管理由以下人员处理
@mcp-abap-adt/auth-broker - 不发出HTTP请求:HTTP请求由处理
@mcp-abap-adt/connection - 不存储配置:配置存储由消费者处理
- 不知道目的地:目标解析由处理
@mcp-abap-adt/auth-broker
外部依赖
此包与外部包交互 仅通过接口:
@mcp-abap-adt/connection:用途SapConfig配置类型-不知道具体的连接实现- 不直接依赖于其他包:所有交互都是通过定义良好的类型和接口进行的
安装
npm install @mcp-abap-adt/header-validator用法
import { validateAuthHeaders } from '@mcp-abap-adt/header-validator';
import { IncomingHttpHeaders } from 'http';
const headers: IncomingHttpHeaders = {
'x-sap-url': 'https://test.sap.com',
'x-mcp-destination': 'TRIAL',
// Note: x-sap-auth-type not needed - always uses JWT
};
const result = validateAuthHeaders(headers);
if (result.isValid && result.config) {
console.log('Auth method:', result.config.priority);
console.log('Destination:', result.config.destination);
} else {
console.error('Validation errors:', result.errors);
}身份验证方法和优先级
验证器支持四种身份验证方法,按优先级(从高到低)排序:
1.SAP基于目标的身份验证(最高优先级)
优先: AuthMethodPriority.SAP_DESTINATION (4)
所需标题:
x-sap-url-SAP系统URLx-sap-destination-目的地名称(例如,“S4HANA_E19”)
可选标头:
x-sap-client-SAP客户编号x-sap-login/x-sap-password-用户名/密码(边缘情况)
描述:最简单的配置-使用AuthBroker管理令牌。始终使用JWT身份验证。不 x-sap-auth-type 需要标题。
示例:
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-sap-destination': 'S4HANA_E19',
};备注:
- 不需要
x-sap-auth-type(总是JWT) - 如果
x-sap-auth-type如果提供,则将忽略(发出警告) - 如果
x-sap-jwt-token如果同时提供,则将被忽略(发出警告) - 需要在服务器中初始化AuthBroker
- 自动处理令牌刷新和验证
2.基于MCP目的地的身份验证
优先: AuthMethodPriority.MCP_DESTINATION (3)
所需标题:
x-sap-url-SAP系统URLx-mcp-destination-目标名称(例如,“试验”、“生产”)
可选标头:
x-sap-client-SAP客户编号
描述:使用AuthBroker根据目标管理令牌。令牌从以下位置加载 {destination}.env 文件,经过验证,并在需要时自动刷新。始终使用JWT身份验证。
示例:
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-mcp-destination': 'TRIAL',
// Note: x-sap-auth-type not needed - always uses JWT
};备注:
- 不需要
x-sap-auth-type(总是JWT) - 如果
x-sap-auth-type如果提供,则将忽略(发出警告) - 如果
x-sap-jwt-token如果同时提供,则将被忽略(发出警告) - 需要在服务器中初始化AuthBroker
- 自动处理令牌刷新和验证
3.直接JWT身份验证(中等优先级)
优先: AuthMethodPriority.DIRECT_JWT (2)
所需标题:
x-sap-url-SAP系统URLx-sap-auth-type-必须是jwt或xsuaax-sap-jwt-token-JWT访问令牌
可选标头:
x-sap-refresh-token-刷新令牌以自动续订令牌x-sap-uaa-url/uaa-url-UAA网址x-sap-uaa-client-id/uaa-client-id-UAA客户端IDx-sap-uaa-client-secret/uaa-client-secret-UAA客户端密码x-sap-client-SAP客户编号
描述:直接JWT令牌身份验证。令牌直接在标头中提供。
示例:
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'jwt',
'x-sap-jwt-token': 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...',
'x-sap-refresh-token': 'refresh_token_here', // optional
};备注:
- 令牌长度必须至少为10个字符
- 刷新令牌是可选的,但建议用于自动令牌续订
4.基本身份验证(最低优先级)
优先: AuthMethodPriority.BASIC (1)
所需标题:
x-sap-url-SAP系统URLx-sap-auth-type-必须是basicx-sap-login-用户名x-sap-password-密码
描述:使用用户名和密码进行基本HTTP身份验证。
示例:
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'basic',
'x-sap-login': 'username',
'x-sap-password': 'password',
};备注:
- 主要用于本地SAP系统
- 凭据以纯文本形式发送(在生产中使用HTTPS)
有效的标头组合
✅ 有效组合
1.仅限SAP目标(最简单-推荐)
{
'x-sap-url': 'https://test.sap.com',
'x-sap-destination': 'S4HANA_E19',
}
// No x-sap-auth-type needed - always JWT2.仅限MCP目的地
{
'x-sap-url': 'https://test.sap.com',
'x-mcp-destination': 'TRIAL',
// Note: x-sap-auth-type not needed - always uses JWT
}3.仅限智威汤逊
{
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'jwt',
'x-sap-jwt-token': 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...',
'x-sap-refresh-token': 'refresh_token', // optional
}4.仅限基本身份验证
{
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'basic',
'x-sap-login': 'username',
'x-sap-password': 'password',
}5.SAP目标+身份验证类型(发出警告)
{
'x-sap-url': 'https://test.sap.com',
'x-sap-destination': 'S4HANA_E19',
'x-sap-auth-type': 'jwt', // ignored (warning)
}结果:使用SAP目标身份验证,忽略身份验证类型(发出警告)
6.目的地+直达JWT(发出警告)
{
'x-sap-url': 'https://test.sap.com',
'x-sap-destination': 'S4HANA_E19',
'x-sap-jwt-token': 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...', // ignored
}结果:使用SAP目标身份验证(优先级4),忽略直接JWT令牌(发出警告)
7.多个目的地(SAP优先)
{
'x-sap-url': 'https://test.sap.com',
'x-sap-destination': 'S4HANA_E19', // Priority 4
'x-mcp-destination': 'TRIAL', // Priority 3 - ignored
}结果:使用SAP目标身份验证,忽略MCP目标
❌ 无效组合
1.缺少必需的标题
{
'x-sap-url': 'https://test.sap.com',
// Missing x-sap-auth-type
}错误: x-sap-auth-type header is required
2.身份验证类型无效
{
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'invalid',
}错误: x-sap-auth-type must be one of: jwt, xsuaa, basic
3.无令牌或目标的JWT身份验证
{
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'jwt',
// Missing x-sap-destination, x-mcp-destination, and x-sap-jwt-token
}错误: JWT authentication requires either x-sap-destination, x-mcp-destination, or x-sap-jwt-token header
4.无凭据的基本身份验证
{
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'basic',
// Missing x-sap-login and/or x-sap-password
}错误: Basic authentication requires x-sap-login and x-sap-password headers
5.空值
{
'x-sap-url': 'https://test.sap.com',
'x-sap-destination': ' ', // empty after trim
}错误: x-sap-destination header is empty
6.URL无效
{
'x-sap-url': 'not-a-valid-url',
'x-sap-auth-type': 'jwt',
'x-sap-jwt-token': 'token',
}错误: x-sap-url is not a valid URL
优先级解决方案
当检测到多种身份验证方法时,验证器会自动选择优先级最高的方法:
- SAP目标 (优先级4)-如果满足以下条件,则始终选择
x-sap-destination存在 - MCP目的地 (优先级3)-如果满足以下条件,则选择
x-mcp-destination存在(始终使用JWT,否x-sap-auth-type需要) - 直接JWT (优先级2)-如果提供JWT令牌,则选择此选项(需要
x-sap-auth-type: jwt) - 基础 (优先级1)-仅当存在基本身份验证标头时才选择(需要
x-sap-auth-type: basic)
示例:优先级解决方案
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-sap-destination': 'S4HANA_E19', // Priority 4 (selected)
'x-sap-auth-type': 'jwt', // Ignored (warning)
'x-mcp-destination': 'TRIAL', // Priority 3 (ignored)
'x-sap-jwt-token': 'token', // Priority 2 (ignored)
'x-sap-login': 'user', // Priority 1 (ignored)
'x-sap-password': 'pass', // Priority 1 (ignored)
};
const result = validateAuthHeaders(headers);
// result.config.priority === AuthMethodPriority.SAP_DESTINATION (4)
// result.warnings includes: "x-sap-auth-type is ignored when x-sap-destination is present"
// result.warnings includes: "x-sap-jwt-token is ignored when x-sap-destination is present"API 参考
validateAuthHeaders(headers?: IncomingHttpHeaders): HeaderValidationResult
验证身份验证标头并确定其优先级。
参数:
headers-HTTP标头对象(可选)
退货: HeaderValidationResult 对象具有:
isValid: boolean-配置是否有效config?: ValidatedAuthConfig-已验证的身份验证配置(如果有效)errors: string[]-验证错误列表warnings: string[]-警告列表(例如,忽略的标题)
ValidatedAuthConfig
interface ValidatedAuthConfig {
priority: AuthMethodPriority; // Authentication method priority
authType: AuthType; // 'jwt' | 'xsuaa' | 'basic'
sapUrl: string; // SAP system URL
destination?: string; // Destination name (for destination-based auth)
jwtToken?: string; // JWT token (for direct JWT auth)
refreshToken?: string; // Refresh token (optional, for JWT auth)
username?: string; // Username (for basic auth)
password?: string; // Password (for basic auth)
errors: string[]; // Validation errors
warnings: string[]; // Warnings
}AuthMethodPriority
身份验证方法优先级的枚举:
enum AuthMethodPriority {
DESTINATION_BASED = 3, // Highest priority
DIRECT_JWT = 2, // Medium priority
BASIC = 1, // Lowest priority
NONE = 0 // Invalid/No auth
}错误处理
验证器为常见问题提供详细的错误消息:
- 缺少必需的标题
- 标头值无效
- 空标头值(修剪空白后)
- URL格式无效
- 身份验证类型无效
- 缺少身份验证凭据
所有错误都收集在 errors 数组,验证结果包括 isValid: false 如果存在任何错误。
警告
验证器会发出以下警告:
- 冲突的标头(例如,目标和直接JWT令牌都存在)
- 具有相同优先级的多种身份验证方法(实践中不应发生)
警告不会阻止验证成功,但会指示潜在的配置问题。
例子
示例1:SAP目标身份验证(最简单)
import { validateAuthHeaders } from '@mcp-abap-adt/header-validator';
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-sap-destination': 'S4HANA_E19',
};
const result = validateAuthHeaders(headers);
if (result.isValid && result.config) {
console.log('Using SAP destination-based auth');
console.log('Destination:', result.config.destination);
console.log('Priority:', result.config.priority);
console.log('Auth Type:', result.config.authType); // Always 'jwt'
}示例2:MCP目标身份验证
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-mcp-destination': 'TRIAL',
// Note: x-sap-auth-type not needed - always uses JWT
};
const result = validateAuthHeaders(headers);
if (result.isValid && result.config) {
console.log('Using MCP destination-based auth');
console.log('Destination:', result.config.destination);
}示例3:直接JWT认证
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'jwt',
'x-sap-jwt-token': 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...',
'x-sap-refresh-token': 'refresh_token',
};
const result = validateAuthHeaders(headers);
if (result.isValid && result.config) {
console.log('Using direct JWT auth');
console.log('Token:', result.config.jwtToken);
}示例4:错误处理
const headers = {
'x-sap-url': 'https://test.sap.com',
'x-sap-auth-type': 'jwt',
// Missing required headers
};
const result = validateAuthHeaders(headers);
if (!result.isValid) {
console.error('Validation failed:');
result.errors.forEach(error => console.error(` - ${error}`));
}文档
完整的文档可在 docs/ 目录:
看 docs/README.md 查看完整的文档索引。
许可证
麻省理工学院
