NAzT
2/24/2017 - 1:36 PM

netpie.js

netpie.js

let hkey = Util.compute_hkey(access_token_secret, appsecret)
let mqttusername = `${appkey}%${Math.floor(Date.now() / 1000)}`;
let mqttpassword = Util.compute_mqtt_password(access_token, mqttusername, hkey)
let [input, protocol, host, port] = endpoint.match(/^([a-z]+):\/\/([^:\/]+):(\d+)/)
console.log(`Revoke = ${Util.compute_revoke_code(access_token, hkey)}`)
let ret = {
  username: mqttusername,
  password: mqttpassword,
  client_id: access_token,
  prefix: `/${appid}/gearname`,
  appid: appid,
  host: host,
  port: port,
  endpoint: endpoint
}
let CryptoJS = require("crypto-js");

export let Util = {
  debug: (...args) => {
    console.log.apply(console, args)
  },
  log: (...args) => {
    console.log.apply(console, args)
  },
  base64: (text) => text.toString(CryptoJS.enc.Base64),
  hmac: (text, key) => Util.base64(CryptoJS.HmacSHA1(text, key)),
  compute_hkey: (access_token_secret, app_secret) => `${access_token_secret}&${app_secret}`,
  compute_mqtt_password: (access_token, mqttusername, hkey) => Util.hmac(`${access_token}%${mqttusername}`, hkey),
  compute_revoke_code: (access_token, hkey) => Util.hmac(access_token, hkey).replace(/\//g, '_')
}