Skip to content

Instantly share code, notes, and snippets.

@dzt
Last active October 25, 2019 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dzt/3440fa1b1b76bfcfcc18c0e10858b731 to your computer and use it in GitHub Desktop.
Save dzt/3440fa1b1b76bfcfcc18c0e10858b731 to your computer and use it in GitHub Desktop.
Kleidi Bot Verification Endpoints
import request from 'request';
import { machineIdSync } from 'node-machine-id';
import CryptoJS, { AES } from 'crypto-js';
const encryptData = (key, data) => {
if (data == null || data === '') return null;
return AES.encrypt(data, key).toString();
};
const decryptData = (key, data) => {
if (data == null || data === '') return null;
const decrypted = AES.decrypt(data, key);
const text = decrypted.toString(CryptoJS.enc.Utf8);
return text;
};
const ENCRYPTION_KEY = 'YOUR ENCRYPTION KEY GOES HERE';
const deviceID = machineIdSync();
const key = 'XXXXX-XXXXX-XXXXX-XXXXX';
let token = null;
const authReq = {
deviceID: deviceID,
key: key, /* Users assigned key */
timestamp: Math.floor((new Date()).getTime() / 1000)
}
request({
url: `https://domain.com/auth/bot`,
method: 'post',
json: true,
form: {
payload: encryptData(ENCRYPTION_KEY, JSON.stringify(authReq))
}
}, (err, res, body) => {
if (err) {
return console.log('Request Error (Auth): ' + err);
} else {
if (res.statusCode == 200) {
let responseDecrypted = null;
try {
responseDecrypted = JSON.parse(decryptData(ENCRYPTION_KEY, body));
token = responseDecrypted.token;
console.log(`Successful Response (Auth): ${JSON.stringify(responseDecrypted)}`);
execValidateReq()
} catch (e) {
console.dir(e);
}
} else {
console.error('Unsuccessful Request (Auth): ' + JSON.stringify(body));
}
}
});
let execValidateReq = () => {
let validateReq = {
token: token,
deviceID: deviceID,
timestamp: Math.floor((new Date()).getTime() / 1000)
}
request({
url: `https://domain.com/auth/bot/validate`,
method: 'post',
json: true,
form: {
payload: encryptData(ENCRYPTION_KEY, JSON.stringify(validateReq))
}
}, (err, res, body) => {
if (err) {
return console.log('Request Error (Validate): ' + err);
} else {
if (res.statusCode == 200) {
console.log(`Successful Response (Validate): ${JSON.stringify(body)}`);
execUnlinkReq()
} else {
console.error('Unsuccessful Request (Validate): ' + JSON.stringify(body));
}
}
});
}
let execUnlinkReq = () => {
request({
url: `https://domain.com/auth/bot/unlink`,
method: 'post',
json: true,
form: {
token: token
}
}, (err, res, body) => {
if (err) {
return console.log('Request Error (Unlink): ' + err);
} else {
if (res.statusCode == 200) {
console.log(`Successful Response (Unlink): ${JSON.stringify(body)}`);
} else {
console.error('Unsuccessful Request (Unlink): ' + JSON.stringify(body));
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment