# @authbroker/authbroker 中文文档

规范地址：https://npmcn.com/package/%40authbroker/authbroker/

版本：0\.0\.12
npm 元数据更新时间：2022-04-04T16:31:02.559Z
中文译文更新时间：2026-11-07T05:57:38.454Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40authbroker/authbroker
原文对照：https://npmcn.com/package/%40authbroker/authbroker/?lang=original
项目主页：https://github.com/authbroker/authbroker#readme
源码仓库：https://github.com/authbroker/authbroker

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

- 包体积（解压后）：700 kB，18 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：5 个
- 首次发布：2019-06-14T10:34:30.717Z
- 最新版本发布：2022-03-30T11:10:03.412Z
- 近 30 天下载量：7568（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：8（近 12 个月 0 个）

## 中文简介

HTTP/MQTT/CoAP经纪公司基于IOT或物联网的NodeJS认证和授权模块.

## 安装

```sh
npm install @authbroker/authbroker
```

也可以使用：`pnpm add @authbroker/authbroker`、`yarn add @authbroker/authbroker`、`bun add @authbroker/authbroker`

## README

# 经纪人认证和授权模块

[![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badges/) [![Build Status](https://travis-ci.org/authbroker/authbroker.svg)](https://travis-ci.com/authbroker/authbroker)

![](https://github.com/authbroker/authbroker/blob/master/docs/asset/repository-open-graph.png)

HTTP/MQTT/CoAP经纪公司基于IOT或物联网的NodeJS认证和授权模块. 此还款正在开发中.

## 开始

-   安装 [键盘](https://www.keycloak.org/) 当地。 定能通达.
-   如果您想要在本地进行测试, 请复制此 repo .

```bash
git clone https://github.com/authbroker/authbroker
cd authbroker
npm install
bash run-test.sh
# visit http://localhost:8080
# with username 'admin' and password 'admin'
npm run test

# for stopping docker instance you use this
docker stop $(docker ps -a -q --filter ancestor=authbroker:test --format="{{.ID}}")
```

它通过多克运行Keycloak并导入演示数据到IOT Realm域. 一个使用 auhtBroker 的 Broker 运行的例子 :

```bash
node ./example/broker.js
```

下面这些命令将订阅和发布到经纪人.

```bash
mosquitto_sub -h localhost -p 1883 -t garden/fan -u admin -P admin

mosquitto_pub -h localhost -p 1883 -t garden/fan -m "hello world" -u admin -P admin
```

如果 Keycloak 中的用户名/ 密码或授权更改, AuthBroker 的授权将不会被允许代理 。 例如,将主题改为未经授权的主题,如花园/未拆卸,看看经纪人如何拒绝.

## 如何运用它

这个模块使用节点风格的回调,可以和不同的经纪人使用,比如: [亚得斯](https://github.com/mcollina/aedes)。 。 。 .

```js
const aedes = require("aedes")({
    persistence: new require("aedes-persistence")()
});
const server = require("net").createServer(aedes.handle);
const port = 1883;

const authBroker = require('@authbroker/authbroker');

const config = {
        "realm": "IOT_Realm",
        "authUrl": "http://localhost:8080/auth",
        "sslRequired": "external",
        "clientId": "authBroker",
        "verifyTokenAudience": true,
        "credentials": {
          "secret": "secret"
        },
        "confidentialPort": 0,
        "policyEnforcer": {},
        "mqttpubScope": "scopes:mqttpub", // scope text for pub permission
        "mqttsubScope": "scopes:mqttsub", // scope text for pub permission
        "mqttResPerfix": "res:" // prefix text for resource or topic
      };

const authbroker = new authBroker(config)

// hook it up
aedes.authenticate = authbroker.authenticate();
aedes.authorizeSubscribe = authbroker.authorizeSubscribe();
aedes.authorizePublish = authbroker.authorizePublish();

server.listen(port, function () {
    console.log("server listening on port", port);
});
```

有必要在凯克洛克的授权部分设置这些范围. 通过去fualt'scope:mqttpub'设定用于出版许可,而"scope:mqttsub"设定用于订阅许可.

![Keycloak Scopes](https://npmcn.com/package/%40authbroker/authbroker/images/keycloak-scope.png)

用户可以发布或订阅具有"范围:mqttpub"或"范围:mqttsub"的资源.

![Keycloak Resource](https://npmcn.com/package/%40authbroker/authbroker/images/keycloak-res.png)

run-test.sh 脚本运行一个预配置的 Keycloak Demo 版本,可以用作模板.

```bash
bash run-test.sh
```

然后访问 http://localhost:8080, 用户名“ admin” 和密码“ admin” 在管理控制台.

## 捐款

[![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/dwyl/esta/issues)

鼓励对下列技术有兴趣或有经验的任何人加入该项目. 如果你喜欢它,加入 [电报组](https://npmcn.com/package/%40authbroker/authbroker/t.me/joinchat/AuKmG05CNFTz0bsBny9igg) 给德夫斯打个招呼!

## 作者/贡献者

-   [哈迪·马赫达维](https://twitter.com/kamerdack)

## 信用/灵感

-   马特奥·科利纳代表莫斯卡、埃德斯、庞特(签名)[https://github.com/moscajs/aedes (中文(简体) )](https://github.com/moscajs/aedes)(中文(简体) )
-   Eugenio Pace for Auth0 Mosca 靈感 (英语:[https://github.com/eugeniop/auth0mosca 互联网档案馆的存檔,存档日期2013-12-02](https://github.com/eugeniop/auth0mosca)(中文(简体) )

## 版权

MIT - 版权(c) 2019-2022 ioKloud

## 常见问题

### @authbroker/authbroker 是什么？

HTTP/MQTT/CoAP经纪公司基于IOT或物联网的NodeJS认证和授权模块\.

### 如何安装 @authbroker/authbroker？

在项目目录运行 npm install @authbroker/authbroker，或使用 pnpm add / yarn add / bun add。

### @authbroker/authbroker 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/authbroker\_\_authbroker 是否存在。

### @authbroker/authbroker 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @authbroker/authbroker 可以商用吗？

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

### @authbroker/authbroker 的下载量有多少？

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

### @authbroker/authbroker 还在维护吗？

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

