Implementation of NodeRed base64 coding

Keywords: http PostMan

  Cause:

HTTP API usage of EMQ X Basic authentication (opens new window) Using the HTTP API of EMQ X Basic authentication (opens new window) Method, id and password must be filled in AppID and AppSecret respectively. The default AppID and AppSecret are admin/public. You can modify and add AppID/AppSecret by selecting "management" - > "application" in the left menu bar of Dashboard.

Link to WIKI

Basic access authentication

Wikipedia, the free encyclopedia
Jump to navigationJump to search

stay HTTP In the context of a transaction, basic access authentication is HTTP user agent (e.g Web browser )Provided on request user name and Password A method. In basic HTTP authentication, the request contains a header field authorization: basic < credentials > in the form of ID and password Base64 Encoding, connected by a single colon:.

It was launched in 2015 RFC  7617 medium Designated, it has been abandoned since 1999 RFC 2617

  The key of the above paragraph is "where the credentials are Base64 encoded ID and password, connected by a single colon:". Before Base64 coding, the data should be prepared as follows: admin:public

Test: POSTMAN connection test: obtain Broker basic information through HTTP GET request

EMQ detailed description

GET /api/v4/brokers/{node}

  Click Send to get the message returned by EMQ X server:

  It shows that the test is successful. But we can't stop here. We need to further explore what kind of data the GET request sends. Or use POSTMAN.

  The specific HTTP message is as follows:

  The underlined text is the specific data corresponding to the authorization: basic < credentials > format. Now "YWRtaW46cHVibGlj" is the result of admin:public encoded by Base64.

NodeRed authentication:

Use node management to install Base64 node in NodeRed, as shown in the figure:

  Generally, I won't use it after installation. I need an example to learn how to use it. Use Bing to search "nodered"   base64 ", as shown in the figure below:

 

Click the Base64 link

After entering, import the code of Sample Flow into NodeRed,

The following figure is obtained:

The specific content of the function node is shown in the figure: note that the string marked with a red line is the same as the data preparation mentioned at the beginning of the article.

 

  Results of post deployment operation:

 

The message result is the same as that of POSTMAN:

 

So far, the Base64 encoding problem of HTTP basic access authentication has been solved.

  Specific source code:

[
    {
        "id": "688fc49188120316",
        "type": "tab",
        "label": "Process 5",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "d2ccae00.2d335",
        "type": "inject",
        "z": "688fc49188120316",
        "name": "",
        "props": [
            {
                "p": "payload",
                "v": "",
                "vt": "str"
            },
            {
                "p": "topic",
                "v": "",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "topic": "",
        "payload": "",
        "payloadType": "str",
        "x": 230,
        "y": 360,
        "wires": [
            [
                "e03cae10.1fc35"
            ]
        ]
    },
    {
        "id": "b778ef09.48871",
        "type": "base64",
        "z": "688fc49188120316",
        "name": "",
        "action": "",
        "property": "payload",
        "x": 525.5,
        "y": 361,
        "wires": [
            [
                "6295d1b1.9d6a3",
                "46b597ba.b94a68"
            ]
        ]
    },
    {
        "id": "6295d1b1.9d6a3",
        "type": "debug",
        "z": "688fc49188120316",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 724,
        "y": 361,
        "wires": []
    },
    {
        "id": "ead9e7c9.152618",
        "type": "debug",
        "z": "688fc49188120316",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 724,
        "y": 441,
        "wires": []
    },
    {
        "id": "46b597ba.b94a68",
        "type": "base64",
        "z": "688fc49188120316",
        "name": "",
        "action": "",
        "property": "payload",
        "x": 525.5,
        "y": 441,
        "wires": [
            [
                "ead9e7c9.152618"
            ]
        ]
    },
    {
        "id": "1c9124e9.e36edb",
        "type": "inject",
        "z": "688fc49188120316",
        "name": "",
        "repeat": "",
        "crontab": "",
        "once": false,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 1889,
        "y": 314,
        "wires": [
            []
        ]
    },
    {
        "id": "48a892ea.b7576c",
        "type": "debug",
        "z": "688fc49188120316",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "false",
        "x": 2285,
        "y": 411,
        "wires": []
    },
    {
        "id": "e03cae10.1fc35",
        "type": "function",
        "z": "688fc49188120316",
        "name": "",
        "func": "msg.payload = new Buffer.from(\"admin:public\");\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 364,
        "y": 361,
        "wires": [
            [
                "b778ef09.48871"
            ]
        ]
    }
]

Posted by kwong on Sat, 20 Nov 2021 13:51:31 -0800