Node-RED api Authentication


验证

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

如果未设置该属性,则对 Node-RED 具有网络访问权限的任何人都可以访问 Node-RED 管理 API。

Step 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 由访问令牌保护。

Step 1 - 获取访问令牌

HTTP POST/auth/token用于交换用户凭据以获取访问令牌。

必须提供以下参数:

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

Step 2 - 使用访问令牌

然后,所有后续 API 调用都应在标头中提供此令牌Authorization

curl example

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

撤销令牌

要在不再需要令牌时撤销令牌,应通过 HTTP POST 将其发送到/auth/revoke

卷曲示例

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

文章作者: Kevin
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Kevin !
评论
  目录