身份验证

Node-RED的管理API通过settings.js文件中的adminAuth属性进行安全保护。security部分描述了如何配置该属性。

如果未设置该属性,任何能够通过网络访问Node-RED的人都可以访问Node-RED管理API。

步骤0 - 检查认证方案

/auth/login发送HTTP GET请求将返回当前有效的认证方案。

curl example:
curl http://localhost:1880/auth/login

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

无活动认证
{}

所有API请求都可在不提供额外认证信息的情况下发起。

基于凭证的身份验证
{
  "type": "credentials",
  "prompts": [
    {
      "id": "username",
      "type": "text",
      "label": "Username"
    },
    {
      "id": "password",
      "type": "password",
      "label": "Password"
    }
  ]
}

该API通过访问令牌进行安全保护。

第一步 - 获取访问令牌

/auth/token发送HTTP POST请求,用于将用户凭证交换为访问令牌。

必须提供以下参数:

  • client_id - 标识客户端。当前必须为node-red-adminnode-red-editor
  • grant_type - 必须为 password
  • scope - 请求的权限列表,以空格分隔。当前必须为*read
  • username - 用于身份验证的用户名
  • password - 用于身份验证的密码
curl example:
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 example:
curl -H "Authorization: Bearer A_SECRET_TOKEN" http://localhost:1880/settings

撤销令牌

当不再需要该令牌时,应通过HTTP POST请求发送至/auth/revoke来撤销:

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