# @abapify/adt-auth 中文文档

规范地址：https://npmcn.com/package/%40abapify/adt-auth/

版本：0\.4\.34
npm 元数据更新时间：2026-11-02T23:20:17.166Z
中文译文更新时间：2026-11-06T15:52:08.534Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40abapify/adt-auth
原文对照：https://npmcn.com/package/%40abapify/adt-auth/?lang=original
项目主页：https://github.com/abapify/adt-cli/tree/main/packages/adt-auth#readme
源码仓库：https://github.com/abapify/adt-cli

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：79.5 kB，25 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：3 个
- 首次发布：2026-04-20T21:11:51.999Z
- 最新版本发布：2026-11-02T23:20:16.278Z
- 近 30 天下载量：2826（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：30（近 12 个月 30 个）

## 中文简介

SAP ADT 系统认证软件包 - 支持多种认证方法

## 安装

```sh
npm install @abapify/adt-auth
```

也可以使用：`pnpm add @abapify/adt-auth`、`yarn add @abapify/adt-auth`、`bun add @abapify/adt-auth`

## README

# @abapify/adt-auth (英语)

[![version](https://img.shields.io/github/package-json/v/abapify/adt-cli?filename=packages/adt-auth/package.json)](https://github.com/abapify/adt-cli/pkgs/npm/%40abapify%2Fadt-auth)

SAP ADT系统支持多种认证方法的认证软件包.

## 特征

-   ✅ **多重认证方法**: Basic、SLC、OAuth(可扩展)
-   ✅ **安全存储**: 基于文件并有适当的权限
-   ✅ **类型安全**: 完整类型脚本支持
-   ✅ **可测试**:容易嘲笑和测试
-   ✅ **可重复使用**: 可用于任何节点.js工具

## 安装

```bash
npm install @abapify/adt-auth
# or
bun add @abapify/adt-auth
```

## 使用量

### 基本认证

```typescript
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)

```typescript
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!');
```

### 多个系统

```typescript
// 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()` - 列出所有可用的SID
-   `setDefaultSid(sid: string)` - 设定默认 SID
-   `getDefaultSid()` - 获取默认 SID

### 认证方法

#### 基本认证方法

用户名/ 密码认证 .

**配置 :**

```typescript
{
  method: 'basic',
  baseUrl: string,
  username: string,
  password: string,
  client?: string,
  language?: string,
  insecure?: boolean,
}
```

#### SlCAuth 方法

SAP 安全登录客户端网络适配器.

**配置 :**

```typescript
{
  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
```

### 会话格式

```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"
}
```

## 与客户合并

```typescript
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 :

1.  **导出默认值** - 插件必须是默认导出
2.  **执行 AuthPlugin 接口** - 一定是这样 `authenticate(options): Promise<AuthPluginResult>`
3.  **返回 AuthPlugin 结果** - 标准格式

```typescript
// 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 使用所存储的插件自动刷新 :

```typescript
// AuthManager loads plugin dynamically
const pluginModule = await import(session.auth.plugin);
const result = await pluginModule.default.authenticate(options);
```

## 警卫

### 证书存储

-   **文件权限**数字 : `0600` (所有者只读/写)
-   **目录权限**数字 : `0700` (仅拥有者)
-   **密码加密**: TODO( 目前为纯文本)

### SLC 警卫

-   没有存储证书( SLC 处理认证)
-   只有代理配置已存储
-   通过 SLC 基于证书的认证

## 发展

```bash
# Install dependencies
bun install

# Build
bun run build

# Test
bun test

# Type check
bun run typecheck
```

## 许可证

麻省理工学院

## 常见问题

### @abapify/adt-auth 是什么？

SAP ADT 系统认证软件包 - 支持多种认证方法

### 如何安装 @abapify/adt-auth？

在项目目录运行 npm install @abapify/adt-auth，或使用 pnpm add / yarn add / bun add。

### @abapify/adt-auth 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @abapify/adt-auth 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @abapify/adt-auth 可以商用吗？

@abapify/adt-auth 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @abapify/adt-auth 的下载量有多少？

根据 npm 官方下载统计，@abapify/adt-auth 在 2026/10/05 – 2026/11/03 这 30 天内被下载 2,826 次，日均约 94 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @abapify/adt-auth 还在维护吗？

最新版本 0\.4\.34 发布于 2026/11/03（4 天前）；至今共发布 30 个版本，其中过去 12 个月发布了 30 个；首次发布于 2026/04/21。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

