身份验证

Node-RED 管理 API 使用 settings.js 文件中的 adminAuth 属性进行安全保护。 安全性 部分描述了该属性的配置方式。

如果该属性未设置,Node-RED 管理 API 可供任何具有网络访问权限的用户使用。

第 0 步 - 检查身份验证方案

/auth/login 的 HTTP GET 请求返回当前活动的身份验证方案。

curl 示例
curl http://localhost:1880/auth/login

在当前版本的 API 中,有两种可能的结果:

没有活动身份验证
{}

所有 API 请求都可以在不提供任何进一步身份验证信息的情况下进行。

基于凭据的身份验证
{
  "type": "credentials",
  "prompts": [
    {
      "id": "username",
      "type": "text",
      "label": "用户名"
    },
    {
      "id": "password",
      "type": "password",
      "label": "密码"
    }
  ]
}

API 由访问令牌保护。

第 1 步 - 获取访问令牌

/auth/token 的 HTTP POST 请求用于以用户凭据换取访问令牌。

必须提供以下参数:

  • client_id - 标识客户端。目前必须是 node-red-adminnode-red-editor
  • grant_type - 必须是 password
  • scope - 请求权限的以空格分隔的列表。目前必须是 *read
  • username - 要进行身份验证的用户名
  • password - 要进行身份验证的密码
curl 示例
curl http://localhost:1880/auth/token --data 'client_id=node-red-admin&grant_type=password&scope=*&username=admin&password=password'

如果成功,响应将包含访问令牌:

{
  "access_token": "A_SECRET_TOKEN",
  "expires_in":604800,
  "token_type": "Bearer"
}

第 2 步 - 使用访问令牌

所有后续的 API 调用应该在 Authorization 头中提供此令牌。

curl 示例
curl -H "Authorization: Bearer A_SECRET_TOKEN" http://localhost:1880/settings

撤销令牌

要在不再需要令牌时撤销它,可以将其在 HTTP POST 请求中发送到 /auth/revoke

curl 示例
curl --data 'token=A_SECRET_TOKEN' -H "Authorization: Bearer A_SECRET_TOKEN" http://localhost:1880/auth/revoke