Merge pull request #17 from mailvelope/node_v6

Use ES6 destructuring (not available in node v4)
This commit is contained in:
Tankred Hase 2017-08-14 11:43:54 +08:00 committed by GitHub
commit b721dc9f9b
5 changed files with 29 additions and 39 deletions

View File

@ -1,9 +1,7 @@
sudo: false sudo: false
language: node_js language: node_js
node_js: node_js:
- "4"
- "6" - "6"
- "7"
before_script: before_script:
- npm install -g grunt-cli - npm install -g grunt-cli
- mongo test_db --eval 'db.addUser("travis", "test");' - mongo test_db --eval 'db.addUser("travis", "test");'

View File

@ -7,7 +7,7 @@
"url": "https://github.com/mailvelope/keyserver.git" "url": "https://github.com/mailvelope/keyserver.git"
}, },
"engines": { "engines": {
"node": ">=4" "node": ">=6"
}, },
"scripts": { "scripts": {
"start": ": ${NODE_ENV=development} && node index.js", "start": ": ${NODE_ENV=development} && node index.js",

View File

@ -31,9 +31,9 @@ class Mongo {
* @param {String} pass The database user's password * @param {String} pass The database user's password
* @yield {undefined} * @yield {undefined}
*/ */
*init(options) { *init({ uri, user, pass }) {
let uri = 'mongodb://' + options.user + ':' + options.pass + '@' + options.uri; let url = 'mongodb://' + user + ':' + pass + '@' + uri;
this._db = yield MongoClient.connect(uri); this._db = yield MongoClient.connect(url);
} }
/** /**

View File

@ -37,18 +37,18 @@ class Email {
* @param {boolean} starttls (optional) force STARTTLS to prevent downgrade attack. Defaults to true. * @param {boolean} starttls (optional) force STARTTLS to prevent downgrade attack. Defaults to true.
* @param {boolean} pgp (optional) if outgoing emails are encrypted to the user's public key. * @param {boolean} pgp (optional) if outgoing emails are encrypted to the user's public key.
*/ */
init(options) { init({ host, port=465, auth, tls, starttls, pgp, sender }) {
this._transport = nodemailer.createTransport({ this._transport = nodemailer.createTransport({
host: options.host, host,
port: options.port || 465, port,
auth: options.auth, auth,
secure: (options.tls !== undefined) ? util.isTrue(options.tls) : true, secure: (tls !== undefined) ? util.isTrue(tls) : true,
requireTLS: (options.starttls !== undefined) ? util.isTrue(options.starttls) : true, requireTLS: (starttls !== undefined) ? util.isTrue(starttls) : true,
}); });
if (util.isTrue(options.pgp)) { if (util.isTrue(pgp)) {
this._transport.use('stream', openpgpEncrypt()); this._transport.use('stream', openpgpEncrypt());
} }
this._sender = options.sender; this._sender = sender;
} }
/** /**
@ -59,8 +59,7 @@ class Email {
* @param {Object} origin origin of the server * @param {Object} origin origin of the server
* @yield {Object} send response from the SMTP server * @yield {Object} send response from the SMTP server
*/ */
*send(options) { *send({ template, userId, keyId, origin }) {
let template = options.template, userId = options.userId, keyId = options.keyId, origin = options.origin;
let message = { let message = {
from: this._sender, from: this._sender,
to: userId, to: userId,
@ -87,26 +86,25 @@ class Email {
* @param {Object} params (optional) nodermailer template parameters * @param {Object} params (optional) nodermailer template parameters
* @yield {Object} reponse object containing SMTP info * @yield {Object} reponse object containing SMTP info
*/ */
*_sendHelper(options) { *_sendHelper({ from, to, subject, text, html, params={} }) {
let template = { let template = {
subject: options.subject, subject,
text: options.text, text,
html: options.html, html,
encryptionKeys: [options.to.publicKeyArmored] encryptionKeys: [to.publicKeyArmored]
}; };
let sender = { let sender = {
from: { from: {
name: options.from.name, name: from.name,
address: options.from.email address: from.email
} }
}; };
let recipient = { let recipient = {
to: { to: {
name: options.to.name, name: to.name,
address: options.to.email address: to.email
} }
}; };
let params = options.params || {};
try { try {
let sendFn = this._transport.templateSender(template, sender); let sendFn = this._transport.templateSender(template, sender);
@ -116,7 +114,7 @@ class Email {
} }
return info; return info;
} catch(error) { } catch(error) {
log.error('email', 'Sending message failed.', error, options); log.error('email', 'Sending message failed.', error);
util.throw(500, 'Sending email to user failed'); util.throw(500, 'Sending email to user failed');
} }
} }

View File

@ -66,9 +66,8 @@ class PublicKey {
* @param {Object} origin Required for links to the keyserver e.g. { protocol:'https', host:'openpgpkeys@example.com' } * @param {Object} origin Required for links to the keyserver e.g. { protocol:'https', host:'openpgpkeys@example.com' }
* @yield {undefined} * @yield {undefined}
*/ */
*put(options) { *put({ publicKeyArmored, primaryEmail, origin }) {
// parse key block // parse key block
let publicKeyArmored = options.publicKeyArmored, primaryEmail = options.primaryEmail, origin = options.origin;
let key = this._pgp.parseKey(publicKeyArmored); let key = this._pgp.parseKey(publicKeyArmored);
// check for existing verfied key by id or email addresses // check for existing verfied key by id or email addresses
let verified = yield this.getVerified(key); let verified = yield this.getVerified(key);
@ -128,8 +127,7 @@ class PublicKey {
* @param {string} nonce The verification nonce proving email address ownership * @param {string} nonce The verification nonce proving email address ownership
* @yield {undefined} * @yield {undefined}
*/ */
*verify(options) { *verify({ keyId, nonce }) {
let keyId = options.keyId, nonce = options.nonce;
// look for verification nonce in database // look for verification nonce in database
let query = { keyId, 'userIds.nonce':nonce }; let query = { keyId, 'userIds.nonce':nonce };
let key = yield this._mongo.get(query, DB_TYPE); let key = yield this._mongo.get(query, DB_TYPE);
@ -157,8 +155,7 @@ class PublicKey {
* @param {string} keyId (optional) The public key id * @param {string} keyId (optional) The public key id
* @yield {Object} The verified key document * @yield {Object} The verified key document
*/ */
*getVerified(options) { *getVerified({ userIds, fingerprint, keyId }) {
let fingerprint = options.fingerprint, userIds = options.userIds, keyId = options.keyId;
let queries = []; let queries = [];
// query by fingerprint // query by fingerprint
if (fingerprint) { if (fingerprint) {
@ -196,8 +193,7 @@ class PublicKey {
* @param {String} email (optional) The user's email address * @param {String} email (optional) The user's email address
* @yield {Object} The public key document * @yield {Object} The public key document
*/ */
*get(options) { *get({ fingerprint, keyId, email }) {
let fingerprint = options.fingerprint, keyId = options.keyId, email = options.email;
// look for verified key // look for verified key
let userIds = email ? [{ email:email }] : undefined; let userIds = email ? [{ email:email }] : undefined;
let key = yield this.getVerified({ keyId, fingerprint, userIds }); let key = yield this.getVerified({ keyId, fingerprint, userIds });
@ -224,8 +220,7 @@ class PublicKey {
* @param {Object} origin Required for links to the keyserver e.g. { protocol:'https', host:'openpgpkeys@example.com' } * @param {Object} origin Required for links to the keyserver e.g. { protocol:'https', host:'openpgpkeys@example.com' }
* @yield {undefined} * @yield {undefined}
*/ */
*requestRemove(options) { *requestRemove({ keyId, email, origin }) {
let keyId = options.keyId, email = options.email, origin = options.origin;
// flag user ids for removal // flag user ids for removal
let key = yield this._flagForRemove(keyId, email); let key = yield this._flagForRemove(keyId, email);
if (!key) { if (!key) {
@ -277,8 +272,7 @@ class PublicKey {
* @param {string} nonce The verification nonce proving email address ownership * @param {string} nonce The verification nonce proving email address ownership
* @yield {undefined} * @yield {undefined}
*/ */
*verifyRemove(options) { *verifyRemove({ keyId, nonce }) {
let keyId = options.keyId, nonce = options.nonce;
// check if key exists in database // check if key exists in database
let flagged = yield this._mongo.get({ keyId, 'userIds.nonce':nonce }, DB_TYPE); let flagged = yield this._mongo.get({ keyId, 'userIds.nonce':nonce }, DB_TYPE);
if (!flagged) { if (!flagged) {