1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-21 23:08:50 -05:00

migrated cloudstorage to use aws service

This commit is contained in:
Tankred Hase 2013-04-18 20:34:02 +02:00
parent 32a52ad6e7
commit ba376d166e
7 changed files with 114 additions and 67 deletions

View File

@ -1,9 +1,6 @@
/**
* A simple server for serving static files using node.js
*/
'use strict';
var express = require('express'),
fs = require('fs'),
port, app, dev;
port = process.env.PORT || 8585;

View File

@ -18,7 +18,7 @@ var app; // container for the application namespace
* Global app configurations
*/
app.config = {
cloudUrl: 'https://whiteout-io.appspot.com',
cloudUrl: 'http://storage.whiteout.io',
symKeySize: 128,
symIvSize: 104,
asymKeySize: 2048,

View File

@ -23,34 +23,29 @@ app.crypto.Crypto = function(window, util) {
// fetch user's encrypted secret key from keychain/storage
var keyStore = new app.dao.LocalStorageDAO(window);
var storageId = emailAddress + '_encryptedSymmetricKey';
var encryptedKey = keyStore.read(storageId);
var storedKey = keyStore.read(storageId);
// check if key exists
if (!encryptedKey) {
if (!storedKey) {
// generate key, encrypt and persist if none exists
symmetricUserKey = util.random(keySize);
var iv = util.random(ivSize);
var key = aes.encrypt(symmetricUserKey, pbkdf2, iv);
keyStore.persist(storageId, {
key: key,
iv: iv
_id: util.UUID(),
userId: emailAddress,
encryptedKey: key,
keyIV: iv
});
} else {
// decrypt key
symmetricUserKey = aes.decrypt(encryptedKey.key, pbkdf2, encryptedKey.iv);
symmetricUserKey = aes.decrypt(storedKey.encryptedKey, pbkdf2, storedKey.keyIV);
}
callback();
});
};
/**
* Generates the user's asymmetric keypair from the user's secret key
*/
this.generateKeypair = function(naclCrypto) {
return naclCrypto.generateKeypair(symmetricUserKey);
};
/**
* Do PBKDF2 key derivation in a WebWorker thread
*/

View File

@ -37,21 +37,22 @@ app.dao.CloudStorage = function(window, $) {
*/
this.persistUserSecretKey = function(emailAddress, callback) {
// fetch user's encrypted secret key from keychain/storage
var keyStore = new app.dao.LocalStorageDAO(window);
var storageId = emailAddress + '_encryptedSymmetricKey';
var encryptedKey = keyStore.read(storageId);
var keyStore = new app.dao.LocalStorageDAO(window),
storageId = emailAddress + '_encryptedSymmetricKey',
storedKey = keyStore.read(storageId);
var payload = {
userId: emailAddress,
encryptedKey: encryptedKey.key,
keyIV: encryptedKey.iv
};
if (!storedKey) {
callback({
error: 'err',
status: 'No key found in storage!'
});
return;
}
var uri = app.config.cloudUrl + '/keys/user/' + emailAddress;
$.ajax({
url: uri,
url: app.config.cloudUrl + '/secretkey/user/' + emailAddress + '/key/' + storedKey._id,
type: 'PUT',
data: JSON.stringify(payload),
data: JSON.stringify(storedKey),
contentType: 'application/json',
success: function() {
callback();
@ -70,50 +71,25 @@ app.dao.CloudStorage = function(window, $) {
*/
this.getUserSecretKey = function(emailAddress, callback, replaceCallback) {
// fetch user's encrypted secret key from keychain/storage
var self = this;
var keyStore = new app.dao.LocalStorageDAO(window);
var storageId = emailAddress + '_encryptedSymmetricKey';
var storedKey = keyStore.read(storageId);
var self = this,
keyStore = new app.dao.LocalStorageDAO(window),
storageId = emailAddress + '_encryptedSymmetricKey',
storedKey = keyStore.read(storageId);
var uri = app.config.cloudUrl + '/keys/user/' + emailAddress;
$.ajax({
url: uri,
url: app.config.cloudUrl + '/secretkey/user/' + emailAddress,
type: 'GET',
dataType: 'json',
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
success: function(keys) {
if (!keys || keys.length === 0) {
callback({
error: 'err',
status: 'Key not synced!'
});
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
});
replaceCallback();
} else {
if (confirm('Swap cloud key?')) {
// upload local key to cloud
self.persistUserSecretKey(emailAddress, callback);
} else {
callback({
error: 'err',
status: 'Key not synced!'
});
}
}
} else {
// local and cloud keys are equal or cloud key is null
callback();
return;
}
handleKey(keys[0], callback);
},
error: function(xhr, textStatus, err) {
callback({
@ -122,6 +98,36 @@ app.dao.CloudStorage = function(window, $) {
});
}
});
function handleKey(fetchedKey, callback) {
if ((!storedKey || !storedKey.encryptedKey) && fetchedKey && fetchedKey.encryptedKey && fetchedKey.keyIV) {
// no local key... persist fetched key
keyStore.persist(storageId, fetchedKey);
replaceCallback();
} else if (storedKey && fetchedKey && (storedKey.encryptedKey !== fetchedKey.encryptedKey || storedKey.keyIV !== fetchedKey.keyIV)) {
// local and fetched keys are not equal
if (confirm('Swap local key?')) {
// replace local key with fetched key
keyStore.persist(storageId, fetchedKey);
replaceCallback();
} else {
if (confirm('Swap cloud key?')) {
// upload local key to cloud
self.persistUserSecretKey(emailAddress, callback);
} else {
callback({
error: 'err',
status: 'Key not synced!'
});
}
}
} else {
// local and cloud keys are equal or cloud key is null
callback();
}
}
};
};

View File

@ -0,0 +1,24 @@
(function() {
'use strict';
app.model.PublicKey = Backbone.Model.extend({
defaults: {
_id: null,
userId: null,
publicKey: null
},
initialize: function() {}
});
app.model.PublicKeyCollection = Backbone.Collection.extend({
model: app.model.PublicKey,
findByName: function(key) {}
});
}());

View File

@ -0,0 +1,25 @@
(function() {
'use strict';
app.model.SecretKey = Backbone.Model.extend({
defaults: {
_id: null,
userId: null,
encryptedKey: null,
keyIV: null
},
initialize: function() {}
});
app.model.SecretKeyCollection = Backbone.Collection.extend({
model: app.model.SecretKey,
findByName: function(key) {}
});
}());

View File

@ -47,7 +47,7 @@
<script src="../js/app-config.js"></script>
<script>
app.config.workerPath = '../js';
app.config.cloudUrl = 'http://localhost:8888';
//app.config.cloudUrl = 'http://localhost:8888';
</script>
<script src="../js/model/email-model.js"></script>