node-redにおけるユーザ認証に関する研究

freeFree Technical Resource

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

node-redにおけるユーザ認証に関する研究

前の記事

デフォルトでは、node-redエディタは、ノードの変更、データのストリーミング、ストリームの再デプロイなど、アクセスしたすべてのユーザーがOperationできます。
このデフォルトのデプロイメント方法は、信頼できるネットワーク上でのみ動作します。以下では、パブリックネットワーク上にnode-redを展開した後、セキュリティ強化と権限検証を行う方法を紹介します。
主に3部構成

  • HTTPS権限の有効化
  • エディタとadmin APIの保護
  • httpノードとnode-redを保護するダッシュボード

HTTPSを開く

node-redはデフォルトでアクセスにhttpを使用します。httpsアクセスを設定するには、node-redの設定ファイルが必要です。settingファイルに設定しますhttps部分的な内容
設定ファイル内のhttps設定項目は、JSON静的データまたは関数のいずれかです。nodejsのHTTPモジュールの完全な設定パラメータ リンクをクリックすると完全な構成が表示されます。

この設定項目のうち、少なくとも2つは返すか設定する必要があります。

  • key PEMフォーマットされたキー。型はStringまたはBufferです。
  • Cert PEMフォーマットされたCertチェーン(StringまたはBuffer型)

構成の例

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

関数として設定する場合は、Promiseにkeyとcertを入れてください。

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もできます。node-redは11以上のnodejsが必要で、httpsは機能として設定する必要があります。 httpsRefreshInterval数時間にわたって自動更新を繰り返すことを示す数値です。

エディタとAdmin APIの認証

node-redのエディタとAdmin APIは2種類の認証をサポートします。

  • ユーザ名パスワード証明書を使用した認証
  • 認証には、Twitter Live GitHubなどのOAuth/Open IDプロバイダを使用してください。

ユーザー名とパスワードに基づく認証は、設定設定ファイルで設定するだけです。
以下は構成例です。

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

管理者は複数のユーザーを設定できます。これらのユーザーのログインと権限データはすべて書き込み済みです。ログインアカウント、パスワード、権限が含まれます。ここで、パスワードはbcryptアルゴリズムツールを使用してハッシュ暗号化されます。
ハッシュパスワードを使用できます。 node-red admin hash-pwnode-red 1.1.0以降

OAuth/Open IDを使用した認証

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の2つの大きなモジュールの権限制限に加えて、管理者は設定することもできます。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 existresolve(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"});
       });
   }
}

最後に、設定ファイルの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 *.