Authentication was added to the WebSocket server in Streamer.bot v0.2.5.
SendMessage is the only request where authentication is required.Streamer.bot client will automatically handle authentication for you, if you have a password set in the WebSocket settings.
Simply add the password property to the StreamerbotClient constructor.
const client = new StreamerbotClient({
password: 'MySuperSecretPassword'
});
When connecting to Streamer.bot's WebSocket Server, you will receive a Hello message, detailed below.
This message always contains an info object with information about the instance you are connected to.
When authentication is enabled, there will also be an authentication object that contains a salt and a challenge string.
{
"timestamp": "<ISO8601_string>",
"session": "<string>",
"request": "Hello",
"info": {
"instanceId": "<string>",
"name": "<string>",
"version": "<string>",
"os": "<string>",
"osVersion": "<string>",
"mode": "ui",
"darkMode": true,
"source": "websocketServer"
},
"authentication": {
"salt": "<base64_string>",
"challenge": "<base64_string>"
}
}
Using the salt and challenge provided in the Hello request, you will need to generate an authentication string and send an Authenticate request back to Streamer.bot.
{
"request": "Authenticate",
"id": "<request_id>", // Any unique ID you want to use
"authentication": "<authentication_string>" // Your generated authentication string
}
You will need to send the above request back to Streamer.bot in order to authenticate. To build the authentication string, you will need to do the following:
(password + salt)SHA256 binary hash of the result and base64 encode it.base64 secret with the challenge sent by the server (base64_secret + challenge)SHA256 hash of that result and base64 encode it.import crypto from 'crypto';
const { salt, challenge } = data.authentication;
const password = 'your_websocket_password';
const secret = crypto.createHash('sha256').update(password + salt, 'utf8').digest('base64');
const authentication = crypto.createHash('sha256').update(secret + challenge, 'utf8').digest('base64');
const authRequest = {
id: '1',
request: 'Authenticate',
authentication
};
ws.send(JSON.stringify(authRequest));