Files
ewelink-api/src/helpers/ewelink.js
Martin M b87d092a71 Release v3.1.0 (#93)
* set APP_ID and APP_SECRET from main class

* add APP_ID and APP_SECRET as class constructor parameters

* updated test case

* updated test case

* added new test case

* docs updated

* Release v3.1.0 - "setWSDevicePowerState" (#96)

* new mixing to control devices using websocket

* switch status on single channel devices

* working on deviceControl mixin

* better error handling

* working on fix for shared devices

* refactor/cleanup

* added helper function

* added docs for new method

* return device new status

* added test cases

* properly close websocket connection and clean used properties

* added test cases

* error detection enhancements

* added test cases

* error detection enhancements

* added new test file to jest setup

* method renamed

* fix for closing websocket connection

* new getWSDevicePowerState method

* added test cases

* re-arrange tests

* added new test cases

* extract helpers methods

* added test case

* close WebSocket connection on auth error

* updated docs

* updated dependencies

* fix for "forbidden" error

* updated dependencies
2020-10-12 19:01:57 -03:00

69 lines
1.9 KiB
JavaScript

const crypto = require('crypto');
const CryptoJS = require('crypto-js');
const random = require('random');
const DEVICE_TYPE_UUID = require('../data/devices-type-uuid.json');
const DEVICE_CHANNEL_LENGTH = require('../data/devices-channel-length.json');
const makeAuthorizationSign = (APP_SECRET, body) =>
crypto
.createHmac('sha256', APP_SECRET)
.update(JSON.stringify(body))
.digest('base64');
const getDeviceTypeByUiid = uiid => DEVICE_TYPE_UUID[uiid] || '';
const getDeviceChannelCountByType = deviceType =>
DEVICE_CHANNEL_LENGTH[deviceType] || 0;
const getDeviceChannelCount = deviceUUID => {
const deviceType = getDeviceTypeByUiid(deviceUUID);
return getDeviceChannelCountByType(deviceType);
};
const create16Uiid = () => {
let result = '';
for (let i = 0; i < 16; i += 1) {
result += random.int(0, 9);
}
return result;
};
const encryptionBase64 = t =>
CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(t));
const decryptionBase64 = t =>
CryptoJS.enc.Base64.parse(t).toString(CryptoJS.enc.Utf8);
const encryptationData = (data, key) => {
const encryptedMessage = {};
const uid = create16Uiid();
const iv = encryptionBase64(uid);
const code = CryptoJS.AES.encrypt(data, CryptoJS.MD5(key), {
iv: CryptoJS.enc.Utf8.parse(uid),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
encryptedMessage.uid = uid;
encryptedMessage.iv = iv;
encryptedMessage.data = code.ciphertext.toString(CryptoJS.enc.Base64);
return encryptedMessage;
};
const decryptionData = (data, key, iv) => {
const iv64 = decryptionBase64(iv);
const code = CryptoJS.AES.decrypt(data, CryptoJS.MD5(key), {
iv: CryptoJS.enc.Utf8.parse(iv64),
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7,
});
return code.toString(CryptoJS.enc.Utf8);
};
module.exports = {
makeAuthorizationSign,
getDeviceChannelCount,
encryptationData,
decryptionData,
};