Skip to content

Commit

Permalink
Codestyle, modernisation, misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwhitman committed Feb 2, 2022
1 parent a899190 commit 72e856e
Show file tree
Hide file tree
Showing 13 changed files with 901 additions and 1,370 deletions.
4 changes: 4 additions & 0 deletions .eslintrc.js → .eslintrc.cjs
@@ -1,4 +1,8 @@
module.exports = {
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"env": {
"node": true,
"es6": true
Expand Down
2 changes: 0 additions & 2 deletions index.js

This file was deleted.

18 changes: 9 additions & 9 deletions lib/internal/chats.js
Expand Up @@ -3,7 +3,7 @@ import PushBullet from '../pushbullet.js';
/**
* Get a list of current chats.
*
* @param {Object} options Optional options object.
* @param {Object} options Optional options object.
* @returns {Promise}
*/
PushBullet.prototype.chats = async function chats(options) {
Expand All @@ -19,11 +19,11 @@ PushBullet.prototype.chats = async function chats(options) {
/**
* Create a new chat.
*
* @param {String} channelTag Email of the person to create the chat with.
* @param {String} channelTag Email of the person to create the chat with.
* @returns {Promise}
*/
PushBullet.prototype.createChat = async function createChat(email) {
var options = {
const options = {
json: {
email: email
}
Expand All @@ -35,7 +35,7 @@ PushBullet.prototype.createChat = async function createChat(email) {
/**
* Mute a chat.
*
* @param {String} chatIden The iden of the chat to mute.
* @param {String} chatIden The iden of the chat to mute.
* @returns {Promise}
*/
PushBullet.prototype.muteChat = async function muteChat(chatIden) {
Expand All @@ -45,7 +45,7 @@ PushBullet.prototype.muteChat = async function muteChat(chatIden) {
/**
* Unmute chat.
*
* @param {String} chatIden The iden of the chat to unmute.
* @param {String} chatIden The iden of the chat to unmute.
* @returns {Promise}
*/
PushBullet.prototype.unmuteChat = async function unmuteChat(chatIden) {
Expand All @@ -55,12 +55,12 @@ PushBullet.prototype.unmuteChat = async function unmuteChat(chatIden) {
/**
* Update a chat.
*
* @param {String} chatIden The iden of the chat to ubsubscribe from.
* @param {Object} updates Updates to make to chat.
* @param {String} chatIden The iden of the chat to ubsubscribe from.
* @param {Object} updates Updates to make to chat.
* @returns {Promise}
*/
PushBullet.prototype.updateChat = async function updateChat(chatIden, updates) {
var options = {
const options = {
json: updates
};

Expand All @@ -70,7 +70,7 @@ PushBullet.prototype.updateChat = async function updateChat(chatIden, updates) {
/**
* Delete a chat.
*
* @param {String} chatIden The iden of the chat to delete.
* @param {String} chatIden The iden of the chat to delete.
* @returns {Promise}
*/
PushBullet.prototype.deleteChat = async function deleteChat(chatIden) {
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/devices.js
Expand Up @@ -10,7 +10,7 @@ import PushBullet from '../pushbullet.js';
* - `cursor` is used to select the page if the results have been paginated.
* - `limit` is used to limit the number of objects in the reponse.
*
* @param {Object} options Optional options object.
* @param {Object} options Optional options object.
* @returns {Promise}
*/
PushBullet.prototype.devices = async function devices(options) {
Expand All @@ -26,11 +26,11 @@ PushBullet.prototype.devices = async function devices(options) {
/**
* Create a new device.
*
* @param {Object} deviceOptions Object of device options.
* @param {Object} deviceOptions Object of device options.
* @returns {Promise}
*/
PushBullet.prototype.createDevice = async function createDevice(deviceOptions) {
var options = {
const options = {
json: deviceOptions
};

Expand All @@ -40,11 +40,11 @@ PushBullet.prototype.createDevice = async function createDevice(deviceOptions) {
/**
* Update new device.
*
* @param {Object} deviceOptions Object of device options.
* @param {Object} deviceOptions Object of device options.
* @returns {Promise}
*/
PushBullet.prototype.updateDevice = async function updateDevice(deviceIden, deviceOptions) {
var options = {
const options = {
json: deviceOptions
};

Expand All @@ -54,7 +54,7 @@ PushBullet.prototype.updateDevice = async function updateDevice(deviceIden, devi
/**
* Delete a device.
*
* @param {String} deviceIden Device IDEN of the device to delete.
* @param {String} deviceIden Device IDEN of the device to delete.
* @returns {Promise}
*/
PushBullet.prototype.deleteDevice = async function deleteDevice(deviceIden) {
Expand Down
147 changes: 75 additions & 72 deletions lib/internal/encryption.js
Expand Up @@ -4,88 +4,91 @@ import forge from 'node-forge';
* Encryption module for the PushBullet API.
*
* The encryption key is created from a user-supplied password and passed through PBKDF2.
*
* @param {String} encryptionPassword End-to-End encryption password set by the user.
* @param {String} userIden The iden of the user (aquired e.g. by the /me request).
*/
export default function Encryption(encryptionPassword, userIden) {
var derivedKeyLength = 32;
var iterations = 30000;
var pseudorandomFunction = forge.md.sha256.create();
var encryptionKey = forge.pkcs5.pbkdf2(encryptionPassword, userIden, iterations, derivedKeyLength, pseudorandomFunction);
export default class Encryption {
/**
* @param {String} encryptionPassword End-to-End encryption password set by the user.
* @param {String} userIden The iden of the user (aquired e.g. by the /me request).
*/
constructor(encryptionPassword, userIden) {
const derivedKeyLength = 32;
const iterations = 30000;
const pseudorandomFunction = forge.md.sha256.create();
const encryptionKey = forge.pkcs5.pbkdf2(encryptionPassword, userIden, iterations, derivedKeyLength, pseudorandomFunction);

this.encryptionKey = encryptionKey;
}
this.encryptionKey = encryptionKey;
}

/**
* Decodes a base-64 encoded string.
*
* @param {String} input A base-64 encoded message to be decoded.
* @returns Decoded string in a binary form.
*/
Encryption.prototype.atob = function atob(input) {
return new Buffer(input, 'base64').toString('binary');
};
/**
* Decodes a base-64 encoded string.
*
* @param {String} input A base-64 encoded message to be decoded.
* @returns {Buffer} Decoded string in a binary form.
*/
atob(input) {
return new Buffer.from(input, 'base64').toString('binary');
}

/**
* Encodes a string in base-64.
*
* @param {String} input A binary string to be encoded.
* @returns Base-64 encoded string.
*/
Encryption.prototype.btoa = function btoa(input) {
return new Buffer(input, 'binary').toString('base64');
};
/**
* Encodes a string in base-64.
*
* @param {String} input A binary string to be encoded.
* @returns {Buffer} Base-64 encoded string.
*/
btoa(input) {
return new Buffer.from(input, 'binary').toString('base64');
}

/**
* Encrypts a message.
*
* @param {String} message A message to encrypt.
* @returns {String} Encrypted message
*/
Encryption.prototype.encrypt = function encrypt(message) {
var key = this.encryptionKey;
/**
* Encrypts a message.
*
* @param {String} message A message to encrypt.
* @returns {String} Encrypted message
*/
encrypt(message) {
const key = this.encryptionKey;

var initializationVector = forge.random.getBytes(12);
var cipher = forge.cipher.createCipher('AES-GCM', key);
cipher.start({ iv: initializationVector });
cipher.update(forge.util.createBuffer(forge.util.encodeUtf8(message)));
cipher.finish();
const initializationVector = forge.random.getBytes(12);
const cipher = forge.cipher.createCipher('AES-GCM', key);
cipher.start({ iv: initializationVector });
cipher.update(forge.util.createBuffer(forge.util.encodeUtf8(message)));
cipher.finish();

var tag = cipher.mode.tag.getBytes();
var encryptedMessage = cipher.output.getBytes();
const tag = cipher.mode.tag.getBytes();
const encryptedMessage = cipher.output.getBytes();

var result = this.btoa('1' + tag + initializationVector + encryptedMessage);
const result = this.btoa('1' + tag + initializationVector + encryptedMessage);

return result;
};
return result;
}

/**
* Decrypts a message.
*
* @param {String} message A message to decrypt.
* @return {String} Decrypted message.
*/
Encryption.prototype.decrypt = function decrypt(message) {
var binaryMessage = this.atob(message);
var key = this.encryptionKey;
var version = binaryMessage.substr(0, 1);
var tag = binaryMessage.substr(1, 16);
var initializationVector = binaryMessage.substr(17, 12);
var encryptedMessage = binaryMessage.substr(29);
/**
* Decrypts a message.
*
* @param {String} message A message to decrypt.
* @return {String} Decrypted message.
*/
decrypt(message) {
const binaryMessage = this.atob(message);
const key = this.encryptionKey;
const version = binaryMessage.substr(0, 1);
const tag = binaryMessage.substr(1, 16);
const initializationVector = binaryMessage.substr(17, 12);
const encryptedMessage = binaryMessage.substr(29);

if (version !== '1') {
throw 'invalid version';
}
if (version !== '1') {
throw new Error('Invalid cipher version');
}

var decipher = forge.cipher.createDecipher('AES-GCM', key);
decipher.start({
iv: initializationVector,
tag: tag
});
decipher.update(forge.util.createBuffer(encryptedMessage));
decipher.finish();
const decipher = forge.cipher.createDecipher('AES-GCM', key);
decipher.start({
iv: initializationVector,
tag: tag
});
decipher.update(forge.util.createBuffer(encryptedMessage));
decipher.finish();

var result = decipher.output.toString('utf8');
return result;
};
const result = decipher.output.toString('utf8');
return result;
}
}
21 changes: 10 additions & 11 deletions lib/internal/ephemerals.js
@@ -1,5 +1,4 @@
import clone from 'clone';

import clone from 'clone';
import PushBullet from '../pushbullet.js';

/**
Expand All @@ -8,11 +7,11 @@ import PushBullet from '../pushbullet.js';
* The options require source_user_iden, target_device_iden, conversation_iden and message.
* See https://docs.pushbullet.com/#send-sms for details.
*
* @param {Object} smsOptions SMS options.
* @param {Object} smsOptions SMS options.
* @returns {Promise}
*/
PushBullet.prototype.sendSMS = async function sendSMS(smsOptions) {
var options = clone(smsOptions);
const options = clone(smsOptions);

options.package_name = 'com.pushbullet.android';
options.type = 'messaging_extension_reply';
Expand All @@ -26,11 +25,11 @@ PushBullet.prototype.sendSMS = async function sendSMS(smsOptions) {
* The options require body, source_user_iden and source_device_iden.
* See https://docs.pushbullet.com/#universal-copypaste for details.
*
* @param {Object} clipOptions Clipboard options.
* @param {Object} clipOptions Clipboard options.
* @returns {Promise}
*/
PushBullet.prototype.sendClipboard = async function sendClipboard(clipOptions) {
var options = clone(clipOptions);
const options = clone(clipOptions);

options.type = 'clip';

Expand All @@ -43,11 +42,11 @@ PushBullet.prototype.sendClipboard = async function sendClipboard(clipOptions) {
* The options require package_name, notification_id, notification_tag and source_user_iden.
* See https://docs.pushbullet.com/#dismissal-ephemeral for details.
*
* @param {Object} ephemeralOptions Ephemeral dismissal options.
* @param {Object} ephemeralOptions Ephemeral dismissal options.
* @returns {Promise}
*/
PushBullet.prototype.dismissEphemeral = async function dismissEphemeral(ephemeralOptions) {
var options = clone(ephemeralOptions);
const options = clone(ephemeralOptions);

options.type = 'dismissal';

Expand All @@ -57,19 +56,19 @@ PushBullet.prototype.dismissEphemeral = async function dismissEphemeral(ephemera
/**
* Send an ephemeral.
*
* @param {Object} ephemeralOptions Ephemeral options.
* @param {Object} ephemeralOptions Ephemeral options.
* @returns {Promise}
*/
PushBullet.prototype.sendEphemeral = async function sendEphemeral(ephemeralOptions) {
if (this.encryption) {
var encryptedOptions = this.encryption.encrypt(JSON.stringify(ephemeralOptions));
const encryptedOptions = this.encryption.encrypt(JSON.stringify(ephemeralOptions));
ephemeralOptions = {
ciphertext: encryptedOptions,
encrypted: true
};
}

var options = {
const options = {
json: {
type: 'push',
push: ephemeralOptions
Expand Down

0 comments on commit 72e856e

Please sign in to comment.