diff --git a/lib/ble/index.js b/lib/ble/index.js index 65f52c9..2be0f0c 100644 --- a/lib/ble/index.js +++ b/lib/ble/index.js @@ -65,16 +65,23 @@ function onWifiWrite (data, offset, withoutResponse, callback) { ssid = obj.ssid pwd = obj.pwd log.info(`connecting to AP`, { ssid : ssid }) - return wifi.setNetwork(ssid, pwd, (err, data) => { + return wifi.createPSK(ssid, pwd, (err, hash, plaintext) => { if (err) { - log.error('Error configuring wifi', err) - result = bleno.Characteristic.RESULT_UNLIKELY_ERROR + log.error('Error hashing wifi password', err) + result = bleno.Characteristic.RESULT_UNLIKELY_ERROR return callback(result) } - currentWifi = ssid - log.info(`Connected to AP`, { ssid : ssid }) - result = bleno.Characteristic.RESULT_SUCCESS - return callback(result) + return wifi.setNetwork(ssid, plaintext, hash, (err, data) => { + if (err) { + log.error('Error configuring wifi', err) + result = bleno.Characteristic.RESULT_UNLIKELY_ERROR + return callback(result) + } + currentWifi = ssid + log.info(`Connected to AP`, { ssid : ssid }) + result = bleno.Characteristic.RESULT_SUCCESS + return callback(result) + }) }) } diff --git a/lib/wifi/index.js b/lib/wifi/index.js index a07a4c6..dc70d1d 100644 --- a/lib/wifi/index.js +++ b/lib/wifi/index.js @@ -111,7 +111,7 @@ class Wifi { _cb = () => {} } /** - * (internal function) Create sanitized wpa_supplicant.conf stanza for + * Create sanitized wpa_supplicant.conf stanza for * configuring wifi without storing plaintext passwords * @example * network={ @@ -121,9 +121,10 @@ class Wifi { * } * * @param {string} ssid SSID of wifi network - * @param {string} + * @param {string} pwd Plaintext passphrase of wifi network + * @param {function} callback Function called after psk hash is generated */ - _createPSK (ssid, pwd, callback) { + createPSK (ssid, pwd, callback) { const cmd = `wpa_passphrase "${ssid}" "${pwd}" | grep "psk="` let lines let hash