1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 08:52:15 -05:00

refactored all files to use functional strict scope

This commit is contained in:
Tankred Hase 2013-04-02 00:12:15 +02:00
parent 5ee864fe01
commit 5c0e04cc31
26 changed files with 1669 additions and 1505 deletions

View File

@ -1,4 +1,4 @@
var app;
var app; // container for the application namespace
(function() {
'use strict';

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
// import web worker dependencies
@ -11,8 +12,6 @@ importScripts('../app-config.js');
importScripts('./aes-ccm.js');
importScripts('./util.js');
var AESBATCHWORKER = (function () {
/**
* In the web worker thread context, 'this' and 'self' can be used as a global
* variable namespace similar to the 'window' object in the main thread

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -21,7 +22,11 @@ app.crypto.AesCBC = function() {
var ivWords = CryptoJS.enc.Base64.parse(iv);
var plaintextWords = CryptoJS.enc.Utf8.parse(plaintext);
var encrypted = CryptoJS.AES.encrypt(plaintextWords, keyWords, { iv: ivWords, mode: mode, padding: padding });
var encrypted = CryptoJS.AES.encrypt(plaintextWords, keyWords, {
iv: ivWords,
mode: mode,
padding: padding
});
var ctBase64 = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
return ctBase64;
@ -39,10 +44,16 @@ app.crypto.AesCBC = function() {
var keyWords = CryptoJS.enc.Base64.parse(key);
var ivWords = CryptoJS.enc.Base64.parse(iv);
var decrypted = CryptoJS.AES.decrypt(ciphertext, keyWords, { iv: ivWords, mode: mode, padding: padding });
var decrypted = CryptoJS.AES.decrypt(ciphertext, keyWords, {
iv: ivWords,
mode: mode,
padding: padding
});
var pt = decrypted.toString(CryptoJS.enc.Utf8);
return pt;
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -49,3 +50,5 @@ app.crypto.AesCCM = function() {
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -49,3 +50,5 @@ app.crypto.AesGCM = function() {
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
// import web worker dependencies
@ -10,8 +11,6 @@ importScripts('../../lib/sjcl/ccm.js');
importScripts('../app-config.js');
importScripts('./aes-ccm.js');
var AESWORKER = (function () {
/**
* In the web worker thread context, 'this' and 'self' can be used as a global
* variable namespace similar to the 'window' object in the main thread

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -32,7 +33,10 @@ app.crypto.Crypto = function(window, util) {
symmetricUserKey = util.random(keySize);
var iv = util.random(ivSize);
var key = aes.encrypt(symmetricUserKey, pbkdf2, iv);
keyStore.persist(storageId, { key: key, iv: iv });
keyStore.persist(storageId, {
key: key,
iv: iv
});
} else {
// decrypt key
symmetricUserKey = aes.decrypt(encryptedKey.key, pbkdf2, encryptedKey.iv);
@ -58,7 +62,10 @@ app.crypto.Crypto = function(window, util) {
}, false);
// send plaintext data to the worker
worker.postMessage({ password:password, keySize:keySize });
worker.postMessage({
password: password,
keySize: keySize
});
} else {
// no WebWorker support... do synchronous call
@ -79,7 +86,12 @@ app.crypto.Crypto = function(window, util) {
worker.addEventListener('message', function(e) {
callback(e.data);
}, false);
worker.postMessage({ type:'encrypt', plaintext:plaintext, key:key, iv:iv });
worker.postMessage({
type: 'encrypt',
plaintext: plaintext,
key: key,
iv: iv
});
} else {
var ct = this.aesEncryptSync(plaintext, key, iv);
@ -94,7 +106,12 @@ app.crypto.Crypto = function(window, util) {
worker.addEventListener('message', function(e) {
callback(e.data);
}, false);
worker.postMessage({ type:'decrypt', ciphertext:ciphertext, key:key, iv:iv });
worker.postMessage({
type: 'decrypt',
ciphertext: ciphertext,
key: key,
iv: iv
});
} else {
var pt = this.aesDecryptSync(ciphertext, key, iv);
@ -121,7 +138,10 @@ app.crypto.Crypto = function(window, util) {
worker.addEventListener('message', function(e) {
callback(e.data);
}, false);
worker.postMessage({ type:'encrypt', list:list });
worker.postMessage({
type: 'encrypt',
list: list
});
} else {
var encryptedList = util.encryptList(aes, list);
@ -136,7 +156,10 @@ app.crypto.Crypto = function(window, util) {
worker.addEventListener('message', function(e) {
callback(e.data);
}, false);
worker.postMessage({ type:'decrypt', list:list });
worker.postMessage({
type: 'decrypt',
list: list
});
} else {
var decryptedList = util.decryptList(aes, list);
@ -164,7 +187,8 @@ app.crypto.Crypto = function(window, util) {
};
this.aesEncryptListForUser = function(list, callback) {
var i, envelope, envelopes = [], self = this;
var i, envelope, envelopes = [],
self = this;
// package objects into batchable envelope format
for (i = 0; i < list.length; i++) {
@ -221,3 +245,5 @@ app.crypto.Crypto = function(window, util) {
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
// import web worker dependencies
@ -9,8 +10,6 @@ importScripts('../../lib/crypto-js/pbkdf2.js');
importScripts('../app-config.js');
importScripts('./pbkdf2.js');
var PBKDF2WORKER = (function () {
/**
* In the web worker thread context, 'this' and 'self' can be used as a global
* variable namespace similar to the 'window' object in the main thread

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -13,10 +14,15 @@ app.crypto.PBKDF2 = function() {
*/
this.getKey = function(password, keySize) {
var salt = CryptoJS.enc.Base64.parse("vbhmLjC+Ub6MSbhS6/CkOwxB25wvwRkSLP2DzDtYb+4="); // from random 256 bit value
var key = CryptoJS.PBKDF2(password, salt, { keySize: keySize/32, iterations: 1000 });
var key = CryptoJS.PBKDF2(password, salt, {
keySize: keySize / 32,
iterations: 1000
});
var keyBase64 = CryptoJS.enc.Base64.stringify(key);
return keyBase64;
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -51,7 +52,9 @@ app.crypto.PGP = function(window, openpgp, util, server) {
*/
self.generateKeys = function(numBits) {
// check passphrase
if (!passphrase && passphrase !== '') { throw 'No passphrase set!'; }
if (!passphrase && passphrase !== '') {
throw 'No passphrase set!';
}
var userId = 'SafeWith.me User <anonymous@dunno.com>';
var keys = openpgp.generate_key_pair(1, numBits, userId, passphrase); // keytype 1=RSA
@ -66,7 +69,9 @@ app.crypto.PGP = function(window, openpgp, util, server) {
*/
self.importKeys = function(publicKeyArmored, privateKeyArmored) {
// check passphrase
if (!passphrase && passphrase !== '') { throw 'No passphrase set!'; }
if (!passphrase && passphrase !== '') {
throw 'No passphrase set!';
}
// store keys in html5 local storage
openpgp.keyring.importPrivateKey(privateKeyArmored, passphrase);
@ -164,6 +169,7 @@ app.crypto.PGP = function(window, openpgp, util, server) {
});
// then upload private key
function uploadPrivateKeys() {
server.xhr({
type: 'POST',
@ -202,6 +208,7 @@ app.crypto.PGP = function(window, openpgp, util, server) {
});
// get private key
function getPrivateKey(pubKey) {
server.xhr({
type: 'GET',
@ -210,7 +217,10 @@ app.crypto.PGP = function(window, openpgp, util, server) {
success: function(privKey) {
// import keys
self.importKeys(pubKey.asciiArmored, privKey.asciiArmored, email);
callback({ privateKey:privKey, publicKey:pubKey });
callback({
privateKey: privKey,
publicKey: pubKey
});
}
});
}
@ -220,7 +230,9 @@ app.crypto.PGP = function(window, openpgp, util, server) {
* Get the current user's private key
*/
self.getPrivateKey = function() {
if (!privateKey) { return undefined; }
if (!privateKey) {
return undefined;
}
return privateKey.armored;
};
@ -228,7 +240,9 @@ app.crypto.PGP = function(window, openpgp, util, server) {
* Get the current user's public key
*/
self.getPublicKey = function() {
if (!publicKey) { return undefined; }
if (!publicKey) {
return undefined;
}
return publicKey.armored;
};
@ -289,19 +303,25 @@ app.crypto.PGP = function(window, openpgp, util, server) {
// Find the private (sub)key for the session key of the message
for (var i = 0; i < msg[0].sessionKeys.length; i++) {
if (priv_key[0].privateKeyPacket.publicKey.getKeyId() == msg[0].sessionKeys[i].keyId.bytes) {
keymat = { key: priv_key[0], keymaterial: priv_key[0].privateKeyPacket};
keymat = {
key: priv_key[0],
keymaterial: priv_key[0].privateKeyPacket
};
sesskey = msg[0].sessionKeys[i];
break;
}
for (var j = 0; j < priv_key[0].subKeys.length; j++) {
if (priv_key[0].subKeys[j].publicKey.getKeyId() == msg[0].sessionKeys[i].keyId.bytes) {
keymat = { key: priv_key[0], keymaterial: priv_key[0].subKeys[j]};
keymat = {
key: priv_key[0],
keymaterial: priv_key[0].subKeys[j]
};
sesskey = msg[0].sessionKeys[i];
break;
}
}
}
if (keymat != null) {
if (keymat !== null) {
if (!keymat.keymaterial.decryptSecretMPIs(passphrase)) {
throw "Passphrase for secrect key was incorrect!";
}
@ -316,7 +336,10 @@ app.crypto.PGP = function(window, openpgp, util, server) {
};
}());
/**
* This function needs to be implemented, since it is used by the openpgp utils
*/
function showMessages(str) {}

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.crypto.Util = function(window, uuid) {
@ -42,7 +43,12 @@ app.crypto.Util = function(window, uuid) {
// stringify to JSON before encryption
json = JSON.stringify(list[i].plaintext);
ct = aes.encrypt(json, list[i].key, list[i].iv);
outList.push({ id:list[i].id, ciphertext:ct, key:list[i].key, iv:list[i].iv });
outList.push({
id: list[i].id,
ciphertext: ct,
key: list[i].key,
iv: list[i].iv
});
}
return outList;
@ -60,7 +66,12 @@ app.crypto.Util = function(window, uuid) {
// decrypt JSON and parse to object literal
json = aes.decrypt(list[i].ciphertext, list[i].key, list[i].iv);
pt = JSON.parse(json);
outList.push({ id:list[i].id, plaintext:pt, key:list[i].key, iv:list[i].iv });
outList.push({
id: list[i].id,
plaintext: pt,
key: list[i].key,
iv: list[i].iv
});
}
return outList;
@ -97,7 +108,9 @@ app.crypto.Util = function(window, uuid) {
*/
this.arrBuf2Blob = function(buf, mimeType) {
var b = new Uint8Array(buf);
var blob = new Blob([b], {type: mimeType});
var blob = new Blob([b], {
type: mimeType
});
return blob;
};
@ -151,3 +164,5 @@ app.crypto.Util = function(window, uuid) {
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -25,7 +26,10 @@ app.dao.CloudStorage = function(window, $) {
callback(list);
},
error: function(xhr, textStatus, err) {
callback({error: err, status: textStatus});
callback({
error: err,
status: textStatus
});
}
});
};
@ -55,7 +59,10 @@ app.dao.CloudStorage = function(window, $) {
callback();
},
error: function(xhr, textStatus, err) {
callback({error: err, status: textStatus});
callback({
error: err,
status: textStatus
});
}
});
};
@ -78,21 +85,30 @@ app.dao.CloudStorage = function(window, $) {
success: function(fetchedKey) {
if ((!storedKey || !storedKey.key) && fetchedKey && fetchedKey.encryptedKey && fetchedKey.keyIV) {
// no local key... persist fetched key
keyStore.persist(storageId, { key: fetchedKey.encryptedKey, iv: fetchedKey.keyIV });
keyStore.persist(storageId, {
key: fetchedKey.encryptedKey,
iv: fetchedKey.keyIV
});
replaceCallback();
} else if (storedKey && fetchedKey && (storedKey.key !== fetchedKey.encryptedKey || storedKey.iv !== fetchedKey.keyIV)) {
// local and fetched keys are not equal
if (confirm('Swap local key?')) {
// replace local key with fetched key
keyStore.persist(storageId, { key: fetchedKey.encryptedKey, iv: fetchedKey.keyIV });
keyStore.persist(storageId, {
key: fetchedKey.encryptedKey,
iv: fetchedKey.keyIV
});
replaceCallback();
} else {
if (confirm('Swap cloud key?')) {
// upload local key to cloud
self.persistUserSecretKey(emailAddress, callback);
} else {
callback({error: 'err', status: 'Key not synced!'});
callback({
error: 'err',
status: 'Key not synced!'
});
}
}
@ -102,9 +118,14 @@ app.dao.CloudStorage = function(window, $) {
}
},
error: function(xhr, textStatus, err) {
callback({error: err, status: textStatus});
callback({
error: err,
status: textStatus
});
}
});
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -27,7 +28,10 @@ app.dao.DeviceStorage = function(util, crypto, jsonDao, sqlcipherDao) {
key = crypto.emailAddress + '_' + type + '_' + list[i].id;
}
items.push({ key:key, object:list[i] });
items.push({
key: key,
object: list[i]
});
}
jsonDao.batch(items, function() {
@ -61,3 +65,5 @@ app.dao.DeviceStorage = function(util, crypto, jsonDao, sqlcipherDao) {
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -38,7 +39,9 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
* Fetch an email with the following id
*/
this.getItem = function(folderName, itemId) {
var folder = this.account.get('folders').where({name: folderName})[0];
var folder = this.account.get('folders').where({
name: folderName
})[0];
var mail = _.find(folder.get('items').models, function(email) {
return email.id + '' === itemId + '';
});
@ -54,7 +57,9 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
var collection, folder, self = this;
// check if items are in memory already (account.folders model)
folder = this.account.get('folders').where({name: folderName})[0];
folder = this.account.get('folders').where({
name: folderName
})[0];
if (!folder) {
// get items from storage
@ -64,7 +69,9 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
// cache collection in folder memory
if (decryptedList.length > 0) {
folder = new app.model.Folder({name: folderName});
folder = new app.model.Folder({
name: folderName
});
folder.set('items', collection);
self.account.get('folders').add(folder);
}
@ -98,7 +105,9 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
// persist encrypted list in device storage
devicestorage.storeEcryptedList(res, 'email_' + folderName, function() {
// remove cached folder in account model
folder = self.account.get('folders').where({name: folderName})[0];
folder = self.account.get('folders').where({
name: folderName
})[0];
if (folder) {
self.account.get('folders').remove(folder);
}
@ -108,3 +117,5 @@ app.dao.EmailDAO = function(_, crypto, devicestorage, cloudstorage) {
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -5,37 +6,38 @@
*/
app.dao.LawnchairDAO = function(window) {
var db = new Lawnchair({
name: 'data-store'
}, function() {});
/**
* Create or update an object
*/
this.persist = function(key, object, callback) {
Lawnchair(function() {
this.save({ key:key, object:object }, callback);
});
db.save({
key: key,
object: object
}, callback);
};
/**
* Persist a bunch of items at once
*/
this.batch = function(list, callback) {
Lawnchair(function() {
this.batch(list, callback);
});
db.batch(list, callback);
};
/**
* Read a single item by its key
*/
this.read = function(key, callback) {
Lawnchair(function() {
this.get(key, function(o) {
db.get(key, function(o) {
if (o) {
callback(o.object);
} else {
callback(null);
}
});
});
};
/**
@ -45,13 +47,12 @@ app.dao.LawnchairDAO = function(window) {
* @param num [Number] The number of items to fetch (null means fetch all)
*/
this.list = function(type, offset, num, callback) {
var i, list = [], matchingKeys = [], parts, timeStr, time;
Lawnchair(function() {
var self = this;
var i, list = [],
matchingKeys = [],
parts, timeStr, time;
// get all keys
this.keys(function(keys) {
db.keys(function(keys) {
// check if key begins with type
for (i = 0; i < keys.length; i++) {
@ -87,7 +88,7 @@ app.dao.LawnchairDAO = function(window) {
}
// fetch all items from data-store with matching key
self.get(matchingKeys, function(matchingList) {
db.get(matchingKeys, function(matchingList) {
for (i = 0; i < matchingList.length; i++) {
list.push(matchingList[i].object);
}
@ -97,25 +98,22 @@ app.dao.LawnchairDAO = function(window) {
});
});
});
};
/**
* Removes an object liter from local storage by its key (delete)
*/
this.remove = function(key, callback) {
Lawnchair(function() {
this.remove(key, callback);
});
db.remove(key, callback);
};
/**
* Clears the whole local storage cache
*/
this.clear = function(callback) {
Lawnchair(function() {
this.nuke(callback);
});
db.nuke(callback);
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
/**
@ -54,3 +55,5 @@ app.dao.LocalStorageDAO = function(window) {
};
};
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.model.Account = Backbone.Model.extend({
@ -19,7 +20,8 @@ app.model.AccountCollection = Backbone.Collection.extend({
model: app.model.Account,
findByName: function (key) {
}
findByName: function(key) {}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.model.Email = Backbone.Model.extend({
@ -29,7 +30,8 @@ app.model.EmailCollection = Backbone.Collection.extend({
model: app.model.Email,
findByName: function (key) {
}
findByName: function(key) {}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.model.Folder = Backbone.Model.extend({
@ -7,8 +8,7 @@ app.model.Folder = Backbone.Model.extend({
items: null
},
initialize: function () {
}
initialize: function() {}
});
@ -16,7 +16,8 @@ app.model.FolderCollection = Backbone.Collection.extend({
model: app.model.Folder,
findByName: function (key) {
}
findByName: function(key) {}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.view.AccountsView = Backbone.View.extend({
@ -11,3 +12,5 @@ app.view.AccountsView = Backbone.View.extend({
return this;
}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.view.ComposeView = Backbone.View.extend({
@ -11,3 +12,5 @@ app.view.ComposeView = Backbone.View.extend({
return this;
}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.view.FolderListView = Backbone.View.extend({
@ -21,3 +22,5 @@ app.view.FolderListView = Backbone.View.extend({
return this;
}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.view.LoginView = Backbone.View.extend({
@ -8,7 +9,8 @@ app.view.LoginView = Backbone.View.extend({
},
render: function(eventName) {
var self = this, page = $(this.el);
var self = this,
page = $(this.el);
page.html(this.template());
page.attr('data-theme', 'a');
@ -32,10 +34,16 @@ app.view.LoginView = Backbone.View.extend({
});
// show loading msg during init
$.mobile.loading('show', { text: 'Unlocking...', textVisible: true, theme: 'c' });
$.mobile.loading('show', {
text: 'Unlocking...',
textVisible: true,
theme: 'c'
});
this.dao.init(account, password, function() {
$.mobile.loading('hide');
window.location = '#accounts/' + account.get('emailAddress') + '/folders';
});
}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.view.MessageListView = Backbone.View.extend({
@ -27,7 +28,10 @@ app.view.MessageListView = Backbone.View.extend({
syncFolder: function() {
var self = this;
$.mobile.loading('show', { text: 'Syncing...', textVisible: true });
$.mobile.loading('show', {
text: 'Syncing...',
textVisible: true
});
// sync from cloud
this.dao.syncFromCloud(this.folder, function(res) {
$.mobile.loading('hide');
@ -52,7 +56,10 @@ app.view.MessageListView = Backbone.View.extend({
list = page.find('#message-list'),
listItemArgs, i, email;
$.mobile.loading('show', { text: 'decrypting...', textVisible: true });
$.mobile.loading('show', {
text: 'decrypting...',
textVisible: true
});
this.dao.listItems(this.folder, 0, 10, function(collection) {
// clear list
list.html('');
@ -60,7 +67,11 @@ app.view.MessageListView = Backbone.View.extend({
// append items to list in reverse order so mails with the most recent date will be displayed first
for (i = collection.models.length - 1; i >= 0; i--) {
email = collection.at(i);
listItemArgs = {account: self.options.account, folder: self.folder, model: email};
listItemArgs = {
account: self.options.account,
folder: self.folder,
model: email
};
list.append(new app.view.MessageListItemView(listItemArgs).render().el);
}
@ -71,3 +82,5 @@ app.view.MessageListView = Backbone.View.extend({
}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.view.MessageListItemView = Backbone.View.extend({
@ -22,3 +23,5 @@ app.view.MessageListItemView = Backbone.View.extend({
return this;
}
});
}());

View File

@ -1,3 +1,4 @@
(function() {
'use strict';
app.view.ReadView = Backbone.View.extend({
@ -30,3 +31,5 @@ app.view.ReadView = Backbone.View.extend({
}
});
}());