node - red 에서의 사용자 로그인 인증에 관한 연구

freeFree Technical Resource

This content is free to read, suitable for basic learning and search traffic.

node - red 에서의 사용자 로그인 인증에 관한 연구

前言

デフォルト地,node-red編集器可以被任何アクセス的用户操作,包括修改节点,流データ,重新部署流。
这种デフォルト的部署方式只適用される运行在可靠的网络中。下面我就给大家介绍一下,在公网上部署node-red后,如何对其进行セキュリティ強化和权限验证。
主要分为三部分

  • Openhttps权限
  • 保护編集器和admin api
  • 保护http节点和node-red的仪表盘

Openhttps

node-redデフォルト是使用http来进行アクセス,もし要設定httpsアクセス,你需要在node-red的設定文件setting文件中設定一下https部分的内容
在設定文件中 https設定项可以是一个json静态データ,也可以是一个函数。完全的設定参数事nodejs中httpモジュール的設定 Click链接可以表示到完全的設定

在该設定项中,至少要有二项需要戻る或設定。

  • key PEM格式化后的密钥,タイプ可以是String 或Buffer
  • cert PEM格式化后的 Cert chain,タイプ可以是String 或Buffer

設定的例子

https: {
    key: require("fs").readFileSync('privkey.pem'),
    cert: require("fs").readFileSync('cert.pem')
},

もし你要設定成一个function的话,请记得将key与cert放到promise中,如下

https: function() {
    return new Promise((resolve, reject) => {
        var key, cert;
        // Do some work to obtain valid certificates
        // ...
        resolve({
            key: key
            cert: cert
        })
    });
}

もし你要在設定https后,自动リフレッシュ证书,并且不用再起動node-red,node-red也是能做到的,需要nodejs在11之上,并且https必须設定成function,接着設定 httpsRefreshInterval,タイプ为一个数字,表示几小时内循环自动リフレッシュ。

編集器与Admin API的鉴权

node-red的編集器与Admin apiサポート两种タイプ的鉴权

  • 使用ユーザー名 パスワード证书来进行鉴权
  • 使用任何OAuth/OpenID提供商来进行鉴权,如Twitter活GitHub。

基于ユーザー名和パスワード的鉴权只需要在setting設定文件中設定即可
如下是一个設定サンプル

adminAuth: {
    type: "credentials",
    users: [
        {
            username: "admin",
            password: "$2a$08$zZWtXTja0fB1pzD4sHCMyOCMYz2Z6dNbM6tl8sJogENOMcxWV9DN.",
            permissions: "*"
        },
        {
            username: "george",
            password: "$2b$08$wuAqPiKJlVN27eF5qJp.RuQYuy6ZYONW7a/UWYxDTtwKFCdB8F19y",
            permissions: "read"
        }
    ]
}

管理者のこと可以設定多个用户。这些用户的ログイン及权限データ都是写死的。包括ログイン的账号,パスワード,和权限。其中パスワード是使用bcrypt 算法工具进行hash加密。
生成hashパスワード可以使用 node-red admin hash-pw(在node-red 1.1.0 之后)

使用OAuth/OpenID来进行認証

dminAuth: {
    type:"strategy",
    strategy: {
        name: "twitter",
        label: 'Sign in with Twitter',
        icon:"fa-twitter",
        strategy: require("passport-twitter").Strategy,
        options: {
            consumerKey: TWITTER_APP_CONSUMER_KEY,
            consumerSecret: TWITTER_APP_CONSUMER_SECRET,
            callbackURL: "http://example.com/auth/strategy/callback",
            verify: function(token, tokenSecret, profile, done) {
                done(null, profile);
            }
        },
    },
    users: [
       { username: "knolleary",permissions: ["*"]}
    ]
}

設定项详解:
name: 策略名称
lable/icon:在ログイン页表示的信息
strategy:需要使用到的库
options:根据strategy来进行設定,需要使用的参数
verify:验证函数,最后使用done函数将用户资料向下传递

設定デフォルト用户

もし你想要限定ないログイン时,node-red的一些权限,可以使用設定デフォルト用户的方式来実装
設定一个デフォルト用户,并設定其对于的权限。权限可以是すべて,也可以是読み取り専用。
如下是一个例子

adminAuth: {
    type: "credentials",
    users: [ /* list of users */ ],
    default: {
        permissions: "read"
    }
}

除来 * 和 read 两种大モジュール的权限限定外,管理者のこと还可以設定Admin API
例如为了取得当前流的信息,用户将会要求 flow.read权限。もし要更新流信息,他们需要使用flows.write权限。

自定义用户鉴权

目前介绍的用户鉴权都是硬编码的鉴权,写死用户データ及对于的权限。这种方式不方便扩展,所以node-red又提供了另一种方式,自定义用户鉴权。以下是実装步骤
作成 <node-red>/user-authentication.js 按照以下模版编写認証代码

module.exports = {
   type: "credentials",
   users: function(username) {
       return new Promise(function(resolve) {
           // Do whatever work is needed to check username is a valid
           // user.
           if (valid) {
               // Resolve with the user object. It must contain
               // properties 'username' and 'permissions'
               var user = { username: "admin", permissions: "*" };
               resolve(user);
           } else {
               // Resolve with null to indicate this user does not exist
               resolve(null);
           }
       });
   },
   authenticate: function(username,password) {
       return new Promise(function(resolve) {
           // Do whatever work is needed to validate the username/password
           // combination.
           if (valid) {
               // Resolve with the user object. Equivalent to having
               // called users(username);
               var user = { username: "admin", permissions: "*" };
               resolve(user);
           } else {
               // Resolve with null to indicate the username/password pair
               // were not valid.
               resolve(null);
           }
       });
   },
   default: function() {
       return new Promise(function(resolve) {
           // Resolve with the user object for the default user.
           // If no default user exists, resolve with null.
           resolve({anonymous: true, permissions:"read"});
       });
   }
}

最后在setting文件的adminAuth中設定
adminAuth: require("./user-authentication")

结语

以上就是本篇的すべて内容,这些内容对于想要精细化控制node-red的权限尤为重要,特别是要将其改造为一个多租户系统时。 谢谢观看。

Related Tags
Put this resource to use in a real project?

Go to the Tool Center for message parsing, CRC verification and device debugging, or submit your requirements for selection and integration advice.

Engineer Membership

Turn this article into actionable debugging resources

After activation, you can use advanced message parsing, resource pack downloads, code examples, engineering cases and priority technical support, suitable for real project delivery.

Unlimited Advanced Tools
Resource & Code Packs
Complete Engineering Case Library
Priority Technical Support

Leave a Reply

Your email address will not be published. Required fields are marked *.