1
0
mirror of https://github.com/moparisthebest/mail synced 2025-02-07 02:20:14 -05:00

implement attachement encryption

This commit is contained in:
Tankred Hase 2013-08-28 14:12:39 +02:00
parent 0ebe4d3b3d
commit 14e37fd768
3 changed files with 82 additions and 33 deletions

View File

@ -167,10 +167,16 @@ define(function(require) {
function encrypt(email, receiverPubkey) {
var ptItems = [email],
receiverPubkeys = [receiverPubkey],
to, greeting;
to, greeting, ct, i;
to = (email.to[0].name || email.to[0].address).split('@')[0].split('.')[0].split(' ')[0];
greeting = 'Hi ' + to + ',\n\n';
// add attachment to encryption batch and remove from email object
if (email.attachments) {
email.attachments.forEach(function(attachment) {
attachment.id = email.id;
ptItems.push(attachment);
});
delete email.attachments;
}
// encrypt the email
crypto.encryptListForUser(ptItems, receiverPubkeys, function(err, encryptedList) {
@ -179,11 +185,27 @@ define(function(require) {
return;
}
// build message envelope
var ct = btoa(JSON.stringify(encryptedList[0]));
// get first name of recipient
to = (email.to[0].name || email.to[0].address).split('@')[0].split('.')[0].split(' ')[0];
greeting = 'Hi ' + to + ',\n\n';
// build encrypted text body
ct = btoa(JSON.stringify(encryptedList[0]));
email.body = greeting + MESSAGE + PREFIX + ct + SUFFIX + SIGNATURE;
email.subject = SUBJECT;
// add encrypted attachments
if (encryptedList.length > 1) {
email.attachments = [];
}
for (i = 1; i < encryptedList.length; i++) {
email.attachments.push({
fileName: 'Encrypted Attachment ' + i,
contentType: 'application/octet-stream',
uint8Array: util.binStr2Uint8Arr(JSON.stringify(encryptedList[i]))
});
}
send(email);
});
}

View File

@ -65,11 +65,11 @@ define(['jquery', 'underscore', 'backbone', 'js/app-config'], function($, _, Bac
self._attachments.push({
fileName: file.name,
contentType: file.type,
uint8Array: new Uint8Array(f.target.result)
binStr: f.target.result
});
};
reader.readAsArrayBuffer(file);
reader.readAsBinaryString(file);
}
for (i = 0; i < files.length; i++) {
@ -167,6 +167,9 @@ define(['jquery', 'underscore', 'backbone', 'js/app-config'], function($, _, Bac
return;
}
// break attachments reference from memory
delete self._attachments;
self.goBackToLastPage();
});
},

View File

@ -14,7 +14,17 @@ define(function(require) {
asymKeySize: 512
};
var dummyMail = {
var dummyMail;
var publicKey = "-----BEGIN PUBLIC KEY-----\r\n" + "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxy+Te5dyeWd7g0P+8LNO7fZDQ\r\n" + "g96xTb1J6pYE/pPTMlqhB6BRItIYjZ1US5q2vk5Zk/5KasBHAc9RbCqvh9v4XFEY\r\n" + "JVmTXC4p8ft1LYuNWIaDk+R3dyYXmRNct/JC4tks2+8fD3aOvpt0WNn3R75/FGBt\r\n" + "h4BgojAXDE+PRQtcVQIDAQAB\r\n" + "-----END PUBLIC KEY-----";
describe('Email DAO unit tests', function() {
var emailDao, account,
keychainStub, imapClientStub, smtpClientStub;
beforeEach(function() {
dummyMail = {
from: [{
name: 'Whiteout Test',
address: 'whiteout.test@t-online.de'
@ -26,12 +36,6 @@ define(function(require) {
body: "Hello world" // plaintext body
};
describe('Email DAO unit tests', function() {
var emailDao, account,
keychainStub, imapClientStub, smtpClientStub;
beforeEach(function() {
account = {
emailAddress: emaildaoTest.user,
symKeySize: app.config.symKeySize,
@ -117,19 +121,12 @@ define(function(require) {
done();
});
});
it('should fail due to invalid email address input', function(done) {
var badMail = {
from: [{
name: 'Whiteout Test',
address: 'whiteout.test@t-online.de'
}], // sender address
to: [{
dummyMail.to = [{
address: 'asfd'
}], // list of receivers
subject: "Hello", // Subject line
body: "Hello world" // plaintext body
};
emailDao.smtpSend(badMail, function(err) {
}];
emailDao.smtpSend(dummyMail, function(err) {
expect(smtpClientStub.send.called).to.be.false;
expect(keychainStub.getReveiverPublicKey.called).to.be.false;
expect(err).to.exist;
@ -137,9 +134,7 @@ define(function(require) {
});
});
it('should work', function(done) {
var publicKey = "-----BEGIN PUBLIC KEY-----\r\n" + "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxy+Te5dyeWd7g0P+8LNO7fZDQ\r\n" + "g96xTb1J6pYE/pPTMlqhB6BRItIYjZ1US5q2vk5Zk/5KasBHAc9RbCqvh9v4XFEY\r\n" + "JVmTXC4p8ft1LYuNWIaDk+R3dyYXmRNct/JC4tks2+8fD3aOvpt0WNn3R75/FGBt\r\n" + "h4BgojAXDE+PRQtcVQIDAQAB\r\n" + "-----END PUBLIC KEY-----";
it('should work without attachments', function(done) {
keychainStub.getReveiverPublicKey.yields(null, {
_id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3",
userId: "safewithme.testuser@gmail.com",
@ -150,6 +145,35 @@ define(function(require) {
emailDao.smtpSend(dummyMail, function(err) {
expect(keychainStub.getReveiverPublicKey.calledOnce).to.be.true;
expect(smtpClientStub.send.calledOnce).to.be.true;
smtpClientStub.send.calledWith(sinon.match(function(o) {
return typeof o.attachments === 'undefined';
}));
expect(err).to.not.exist;
done();
});
});
it('should work with attachments', function(done) {
dummyMail.attachments = [{
fileName: 'bar.txt',
contentType: 'text/plain',
binStr: 'barbarbarbarbar'
}];
keychainStub.getReveiverPublicKey.yields(null, {
_id: "fcf8b4aa-5d09-4089-8b4f-e3bc5091daf3",
userId: "safewithme.testuser@gmail.com",
publicKey: publicKey
});
smtpClientStub.send.yields();
emailDao.smtpSend(dummyMail, function(err) {
expect(keychainStub.getReveiverPublicKey.calledOnce).to.be.true;
expect(smtpClientStub.send.calledOnce).to.be.true;
smtpClientStub.send.calledWith(sinon.match(function(o) {
var ptAt = dummyMail.attachments[0];
var ctAt = o.attachments[0];
return ctAt.uint8Array && !ctAt.binStr && ctAt.fileName && ctAt.fileName !== ptAt.fileName;
}));
expect(err).to.not.exist;
done();
});