@abapify/adt-auth (英语)
SAP ADT系统支持多种认证方法的认证软件包.
特征
- ✅ 多重认证方法: Basic、SLC、OAuth(可扩展)
- ✅ 安全存储: 基于文件并有适当的权限
- ✅ 类型安全: 完整类型脚本支持
- ✅ 可测试:容易嘲笑和测试
- ✅ 可重复使用: 可用于任何节点.js工具
安装
npm install @abapify/adt-auth
# or
bun add @abapify/adt-auth
使用量
基本认证
import { AuthManager, BasicAuthMethod } from '@abapify/adt-auth';
const authManager = new AuthManager();
authManager.registerMethod(new BasicAuthMethod());
// Login
const session = await authManager.login(
{
method: 'basic',
baseUrl: 'https://sap.example.com',
username: 'user',
password: 'pass',
client: '100',
},
'TRL',
);
console.log('✅ Logged in!', session.sid);
// Later: get credentials
const credentials = authManager.getCredentials('TRL');
SAP 安全登录客户端( SLC)
import { AuthManager, SlcAuthMethod } from '@abapify/adt-auth';
const authManager = new AuthManager();
authManager.registerMethod(new SlcAuthMethod());
// Login (SLC handles authentication via proxy)
const session = await authManager.login(
{
method: 'slc',
baseUrl: 'https://sap.example.com',
slcProxy: {
host: 'localhost',
port: 3128,
},
client: '200',
},
'TRL',
);
console.log('✅ Logged in via SLC!');
多个系统
// Login to multiple systems
await authManager.login(config1, 'TRL');
await authManager.login(config2, 'QAS');
await authManager.login(config3, 'PRD');
// List all systems
const sids = authManager.listSids();
console.log('Available systems:', sids);
// Set default
authManager.setDefaultSid('TRL');
// Get default system credentials
const creds = authManager.getCredentials(); // Uses default SID
API 密码
认证管理器
主管弦乐以认证.
方法
registerMethod(method: AuthMethod)- 登记认证方法login(config: AuthConfig, sid: string)- 登录和创建会话getSession(sid?: string)- 获取会话( 如果未提供 sid 则默认使用)getCredentials(sid?: string)- 获得会议资格testSession(sid: string)- 测试会话是否仍然有效logout(sid: string)- 删除会话listSids()- 列出所有可用的SIDsetDefaultSid(sid: string)- 设定默认 SIDgetDefaultSid()- 获取默认 SID
认证方法
基本认证方法
用户名/ 密码认证 .
配置 :
{
method: 'basic',
baseUrl: string,
username: string,
password: string,
client?: string,
language?: string,
insecure?: boolean,
}
SlCAuth 方法
SAP 安全登录客户端网络适配器.
配置 :
{
method: 'slc',
baseUrl: string,
slcProxy: {
host: string,
port: number,
},
client?: string,
language?: string,
}
先决条件:
- SAP 安全登录客户端已安装并运行
- 配置并激活 Web 适配器配置
存储
会话存储于 ~/.adt/sessions/ 有文件权限 0600 (所有者只读/写).
文件结构
~/.adt/
└── sessions/
├── TRL.json
├── QAS.json
└── PRD.json
会话格式
{
"sid": "TRL",
"credentials": {
"method": "basic",
"baseUrl": "https://sap.example.com",
"username": "user",
"password": "encrypted",
"client": "100"
},
"createdAt": "2025-01-01T00:00:00.000Z",
"lastUsed": "2025-01-01T00:00:00.000Z"
}
与客户合并
import { createAdtClient } from '@abapify/adt-client';
import { AuthManager, BasicAuthMethod } from '@abapify/adt-auth';
// Get credentials
const authManager = new AuthManager();
authManager.registerMethod(new BasicAuthMethod());
const credentials = authManager.getCredentials('TRL');
if (!credentials) {
throw new Error('Not authenticated. Run: adt login');
}
// Create ADT client
const client = createAdtClient({
baseUrl: credentials.baseUrl,
client: credentials.client,
// Method-specific config
...(credentials.method === 'basic' && {
username: credentials.username,
password: credentials.password,
}),
...(credentials.method === 'slc' && {
proxy: credentials.slcProxy,
}),
});
// Use client
const info =
await client.adt.core.http.systeminformation.getSystemInformation();
console.log('System:', info);
插件结构
Auth插件提供认证方法. AuthManager 通用并代表插件.
插件合同
所有认证插件 MUST :
- 导出默认值 - 插件必须是默认导出
- 执行 AuthPlugin 接口 - 一定是这样
authenticate(options): Promise<AuthPluginResult> - 返回 AuthPlugin 结果 - 标准格式
// Plugin implementation
import type {
AuthPlugin,
AuthPluginResult,
AuthPluginOptions,
} from '@abapify/adt-auth';
const authPlugin: AuthPlugin = {
async authenticate(options: AuthPluginOptions): Promise<AuthPluginResult> {
// Your auth logic...
return {
method: 'cookie',
credentials: {
cookies: 'cookie-string',
expiresAt: new Date(Date.now() + 8 * 60 * 60 * 1000),
},
};
},
};
export default authPlugin;
可用的插件
- @abapify/adt-puppeteer (英语) - 基于浏览器的SSO认证
证书刷新
当会话到期后, AuthManager 使用所存储的插件自动刷新 :
// AuthManager loads plugin dynamically
const pluginModule = await import(session.auth.plugin);
const result = await pluginModule.default.authenticate(options);
警卫
证书存储
- 文件权限数字 :
0600(所有者只读/写) - 目录权限数字 :
0700(仅拥有者) - 密码加密: TODO( 目前为纯文本)
SLC 警卫
- 没有存储证书( SLC 处理认证)
- 只有代理配置已存储
- 通过 SLC 基于证书的认证
发展
# Install dependencies
bun install
# Build
bun run build
# Test
bun test
# Type check
bun run typecheck
许可证
麻省理工学院