From 844d7525180c2e35f2ac46bebf0e65b54650bb9b Mon Sep 17 00:00:00 2001 From: Felix Hammerl Date: Tue, 29 Oct 2013 15:22:19 +0100 Subject: [PATCH] [WO-38] use pgp-compliant formating --- src/js/app-config.js | 2 +- src/js/dao/email-dao.js | 44 ++++++++++++++++++--------------- test/new-unit/email-dao-test.js | 17 ++++++------- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/js/app-config.js b/src/js/app-config.js index b15ca45..0447439 100644 --- a/src/js/app-config.js +++ b/src/js/app-config.js @@ -35,7 +35,7 @@ define([], function() { * Strings are maintained here */ app.string = { - subject: '[whiteout] Encrypted message', + subjectPrefix: '[whiteout] ', message: 'this is a private conversation. To read my encrypted message below, simply install Whiteout Mail for Chrome. The app is really easy to use and automatically encrypts sent emails, so that only the two of us can read them: https://chrome.google.com/webstore/detail/whiteout-mail/jjgghafhamholjigjoghcfcekhkonijg', cryptPrefix: '-----BEGIN PGP MESSAGE-----', cryptSuffix: '-----END PGP MESSAGE-----', diff --git a/src/js/dao/email-dao.js b/src/js/dao/email-dao.js index e479210..0645f06 100644 --- a/src/js/dao/email-dao.js +++ b/src/js/dao/email-dao.js @@ -246,6 +246,7 @@ define(function(require) { messageBlock = email.body.split(str.cryptPrefix)[1].split(str.cryptSuffix)[0]; // add prefix and suffix again email.body = str.cryptPrefix + messageBlock + str.cryptSuffix; + email.subject = email.subject.split(str.subjectPrefix)[1]; } catch (e) { callback({ errMsg: 'Error parsing encrypted message block!' @@ -273,9 +274,7 @@ define(function(require) { return; } - decrypted = JSON.parse(decrypted); - i.subject = decrypted.subject; - i.body = decrypted.body; + i.body = decrypted; after(); }); @@ -310,7 +309,7 @@ define(function(require) { }); function fetchList(callback) { - var encryptedList = []; + var headers = []; // fetch imap folder's message list self.imapListMessages({ @@ -325,45 +324,50 @@ define(function(require) { // find encrypted messages by subject emails.forEach(function(i) { - if (i.subject === str.subject) { - encryptedList.push(i); + if (i.subject.indexOf(str.subjectPrefix) !== -1) { + headers.push(i); } }); // fetch message bodies - fetchBodies(encryptedList, callback); + fetchBodies(headers, callback); }); } - function fetchBodies(messageList, callback) { + function fetchBodies(headers, callback) { var emails = []; - if (messageList.length < 1) { + if (headers.length < 1) { callback(null, emails); return; } - var after = _.after(messageList.length, function() { + var after = _.after(headers.length, function() { callback(null, emails); }); - _.each(messageList, function(messageItem) { + _.each(headers, function(header) { self.imapGetMessage({ folder: options.folder, - uid: messageItem.uid + uid: header.uid }, function(err, message) { if (err) { callback(err); return; } - // set gotten attributes like body to message object containing list meta data like 'unread' or 'replied' - messageItem.id = message.id; - messageItem.body = message.body; - messageItem.html = message.html; - messageItem.attachments = message.attachments; + if (typeof message.body !== 'string' || message.body.indexOf(str.cryptPrefix) === -1 || message.body.indexOf(str.cryptSuffix) === -1) { + after(); + return; + } - emails.push(messageItem); + // set gotten attributes like body to message object containing list meta data like 'unread' or 'replied' + header.id = message.id; + header.body = message.body; + header.html = message.html; + header.attachments = message.attachments; + + emails.push(header); after(); }); }); @@ -514,7 +518,7 @@ define(function(require) { */ EmailDAO.prototype.encryptForUser = function(email, receiverPubkey, callback) { var self = this, - pt = JSON.stringify(email), + pt = email.body, receiverPubkeys = [receiverPubkey]; // get own public key so send message can be read @@ -557,7 +561,7 @@ define(function(require) { // build encrypted text body email.body = greeting + MESSAGE + ct + SIGNATURE; - email.subject = str.subject; + email.subject = str.subjectPrefix + email.subject; return email; } diff --git a/test/new-unit/email-dao-test.js b/test/new-unit/email-dao-test.js index 54fb894..36abab0 100644 --- a/test/new-unit/email-dao-test.js +++ b/test/new-unit/email-dao-test.js @@ -35,7 +35,7 @@ define(function(require) { to: [{ address: 'safewithme.testuser@gmail.com' }], // list of receivers - subject: "Hello", // Subject line + subject: "[whiteout] Hello", // Subject line body: "Hello world" // plaintext body }; @@ -521,8 +521,10 @@ define(function(require) { it('should not list unencrypted messages', function(done) { imapClientStub.listMessages.yields(null, [{ uid: 413, + subject: '' }, { uid: 414, + subject: '' }]); imapClientStub.getMessagePreview.yields(null, { body: 'asdf' @@ -547,13 +549,13 @@ define(function(require) { it('should work', function(done) { imapClientStub.listMessages.yields(null, [{ uid: 413, - subject: app.string.subject + subject: app.string.subjectPrefix + 'asd' }, { uid: 414, - subject: app.string.subject + subject: app.string.subjectPrefix + 'asd' }]); imapClientStub.getMessagePreview.yields(null, { - body: 'asdf' + body: app.string.cryptPrefix + '\nasdf\n' + app.string.cryptSuffix }); devicestorageStub.removeList.yields(); devicestorageStub.storeList.yields(); @@ -582,20 +584,17 @@ define(function(require) { userId: "safewithme.testuser@gmail.com", publicKey: publicKey }); - pgpStub.decrypt.yields(null, JSON.stringify({ - body: 'test body', - subject: 'test subject' - })); + pgpStub.decrypt.yields(null, 'test body'); emailDao.listMessages({ folder: 'INBOX', offset: 0, num: 2 }, function(err, emails) { + expect(err).to.not.exist; expect(devicestorageStub.listItems.calledOnce).to.be.true; expect(keychainStub.getReveiverPublicKey.calledTwice).to.be.true; expect(pgpStub.decrypt.calledTwice).to.be.true; - expect(err).to.not.exist; expect(emails.length).to.equal(2); done(); });