2013-04-02 09:02:57 -04:00
|
|
|
/**
|
|
|
|
* High level storage api for handling syncing of data to
|
|
|
|
* and from the cloud.
|
|
|
|
*/
|
|
|
|
app.dao.CloudStorage = function(window, $) {
|
2013-04-01 18:12:15 -04:00
|
|
|
'use strict';
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-04-19 07:55:21 -04:00
|
|
|
//
|
|
|
|
// Public Key
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the user's corresponding public key
|
|
|
|
*/
|
2013-04-19 13:13:27 -04:00
|
|
|
this.getPublicKey = function(keyId, callback) {
|
2013-04-19 07:55:21 -04:00
|
|
|
var uri;
|
|
|
|
|
2013-04-19 13:13:27 -04:00
|
|
|
uri = app.config.cloudUrl + '/publickey/key/' + keyId;
|
2013-04-19 07:55:21 -04:00
|
|
|
$.ajax({
|
|
|
|
url: uri,
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
2013-04-19 13:13:27 -04:00
|
|
|
success: function(key) {
|
|
|
|
if (!key || !key._id) {
|
2013-04-19 07:55:21 -04:00
|
|
|
callback({
|
|
|
|
error: 'No public key for that user!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-04-19 13:13:27 -04:00
|
|
|
callback(null, key);
|
2013-04-19 07:55:21 -04:00
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
|
|
|
error: err,
|
|
|
|
status: textStatus
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persist the user's publc key
|
|
|
|
*/
|
|
|
|
this.putPublicKey = function(pubkey, callback) {
|
|
|
|
var uri;
|
|
|
|
|
|
|
|
uri = app.config.cloudUrl + '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id;
|
|
|
|
$.ajax({
|
|
|
|
url: uri,
|
|
|
|
type: 'PUT',
|
|
|
|
data: JSON.stringify(pubkey),
|
|
|
|
contentType: 'application/json',
|
|
|
|
success: function() {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
|
|
|
error: err,
|
|
|
|
status: textStatus
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2013-05-02 12:49:22 -04:00
|
|
|
// Encrypted Mail storage
|
2013-04-19 07:55:21 -04:00
|
|
|
//
|
|
|
|
|
2013-04-19 13:13:27 -04:00
|
|
|
/**
|
|
|
|
* Pushes an encrypted item to the user's cloud storage
|
|
|
|
* @param type [String] The type of item e.g. 'email'
|
|
|
|
*/
|
|
|
|
this.putEncryptedItem = function(item, type, emailAddress, folderName, callback) {
|
|
|
|
var uri;
|
|
|
|
|
|
|
|
uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + item.id;
|
|
|
|
$.ajax({
|
|
|
|
url: uri,
|
|
|
|
type: 'PUT',
|
|
|
|
data: JSON.stringify(item),
|
|
|
|
contentType: 'application/json',
|
|
|
|
success: function() {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
|
|
|
error: err,
|
|
|
|
status: textStatus
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-04-23 10:35:01 -04:00
|
|
|
/**
|
|
|
|
* Delete an encrypted item from the cloud
|
|
|
|
* @param type [String] The type of item e.g. 'email'
|
|
|
|
*/
|
|
|
|
this.deleteEncryptedItem = function(id, type, emailAddress, folderName, callback) {
|
|
|
|
var uri;
|
|
|
|
|
|
|
|
uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + id;
|
|
|
|
$.ajax({
|
|
|
|
url: uri,
|
|
|
|
type: 'DELETE',
|
|
|
|
success: function() {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
|
|
|
error: err,
|
|
|
|
status: textStatus
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-13 11:58:46 -04:00
|
|
|
/**
|
2013-04-02 09:02:57 -04:00
|
|
|
* Lists the encrypted items
|
|
|
|
* @param type [String] The type of item e.g. 'email'
|
|
|
|
* @param offset [Number] The offset of items to fetch (0 is the last stored item)
|
|
|
|
* @param num [Number] The number of items to fetch (null means fetch all)
|
2013-03-13 11:58:46 -04:00
|
|
|
*/
|
2013-04-02 09:02:57 -04:00
|
|
|
this.listEncryptedItems = function(type, emailAddress, folderName, callback) {
|
2013-04-19 07:55:21 -04:00
|
|
|
var uri;
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
// fetch encrypted json objects from cloud service
|
|
|
|
uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName;
|
|
|
|
$.ajax({
|
|
|
|
url: uri,
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(list) {
|
2013-04-19 07:55:21 -04:00
|
|
|
callback(null, list);
|
2013-04-02 09:02:57 -04:00
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
|
|
|
error: err,
|
|
|
|
status: textStatus
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-19 07:55:21 -04:00
|
|
|
//
|
2013-05-18 22:00:53 -04:00
|
|
|
// Ecrypted Private Key
|
2013-04-19 07:55:21 -04:00
|
|
|
//
|
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
/**
|
2013-05-18 22:00:53 -04:00
|
|
|
* Persist encrypted private key to cloud service
|
2013-04-02 09:02:57 -04:00
|
|
|
*/
|
2013-05-18 22:00:53 -04:00
|
|
|
this.putPrivateKey = function(privkey, callback) {
|
2013-04-02 09:02:57 -04:00
|
|
|
// fetch user's encrypted secret key from keychain/storage
|
2013-05-18 22:00:53 -04:00
|
|
|
var uri;
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-05-18 22:00:53 -04:00
|
|
|
uri = app.config.cloudUrl + '/privatekey/user/' + privkey.userId + '/key/' + privkey._id;
|
2013-04-02 09:02:57 -04:00
|
|
|
$.ajax({
|
2013-04-19 07:55:21 -04:00
|
|
|
url: uri,
|
2013-04-02 09:02:57 -04:00
|
|
|
type: 'PUT',
|
2013-05-18 22:00:53 -04:00
|
|
|
data: JSON.stringify(privkey),
|
2013-04-02 09:02:57 -04:00
|
|
|
contentType: 'application/json',
|
|
|
|
success: function() {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
|
|
|
error: err,
|
|
|
|
status: textStatus
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
/**
|
2013-05-18 22:00:53 -04:00
|
|
|
* Sync encrypted private key from cloud service
|
2013-04-02 09:02:57 -04:00
|
|
|
*/
|
2013-05-18 22:00:53 -04:00
|
|
|
this.syncPrivateKey = function(emailAddress, storedkey, callback, replaceCallback) {
|
2013-04-02 09:02:57 -04:00
|
|
|
// fetch user's encrypted secret key from keychain/storage
|
2013-04-18 14:34:02 -04:00
|
|
|
var self = this,
|
2013-04-23 09:30:43 -04:00
|
|
|
gottenKey, uri;
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-05-18 22:00:53 -04:00
|
|
|
uri = app.config.cloudUrl + '/privatekey/user/' + emailAddress;
|
2013-04-02 09:02:57 -04:00
|
|
|
$.ajax({
|
2013-04-19 07:55:21 -04:00
|
|
|
url: uri,
|
2013-04-02 09:02:57 -04:00
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
2013-04-18 14:34:02 -04:00
|
|
|
success: function(keys) {
|
2013-04-23 09:30:43 -04:00
|
|
|
if (!keys) {
|
2013-04-18 14:34:02 -04:00
|
|
|
callback({
|
|
|
|
error: 'err',
|
|
|
|
status: 'Key not synced!'
|
2013-04-01 18:12:15 -04:00
|
|
|
});
|
2013-04-18 14:34:02 -04:00
|
|
|
return;
|
2013-04-02 09:02:57 -04:00
|
|
|
}
|
2013-04-18 14:34:02 -04:00
|
|
|
|
2013-04-23 09:30:43 -04:00
|
|
|
// use first key, if it exists
|
|
|
|
if (keys.length > 0) {
|
|
|
|
gottenKey = keys[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
handleKey(gottenKey, callback);
|
2013-04-02 09:02:57 -04:00
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
|
|
|
error: err,
|
|
|
|
status: textStatus
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-04-18 14:34:02 -04:00
|
|
|
|
|
|
|
function handleKey(fetchedKey, callback) {
|
2013-05-18 22:00:53 -04:00
|
|
|
if ((!storedkey || !storedkey.encryptedKey) && fetchedKey && fetchedKey.encryptedKey) {
|
2013-04-18 14:34:02 -04:00
|
|
|
// no local key... persist fetched key
|
2013-05-18 22:00:53 -04:00
|
|
|
replaceCallback(fetchedKey);
|
2013-04-18 14:34:02 -04:00
|
|
|
|
2013-05-18 22:00:53 -04:00
|
|
|
} else if (!fetchedKey && storedkey && storedkey.encryptedKey) {
|
2013-04-23 09:30:43 -04:00
|
|
|
// no key in cloud... push local key to cloud
|
2013-05-18 22:00:53 -04:00
|
|
|
self.putPrivateKey(storedkey, callback);
|
2013-04-23 09:30:43 -04:00
|
|
|
|
2013-05-18 22:00:53 -04:00
|
|
|
} else if (storedkey && fetchedKey && (storedkey.encryptedKey !== fetchedKey.encryptedKey || storedkey.iv !== fetchedKey.iv)) {
|
2013-04-18 14:34:02 -04:00
|
|
|
// local and fetched keys are not equal
|
2013-04-19 07:55:21 -04:00
|
|
|
if (window.confirm('Swap local key?')) {
|
2013-04-18 14:34:02 -04:00
|
|
|
// replace local key with fetched key
|
2013-05-18 22:00:53 -04:00
|
|
|
replaceCallback(fetchedKey);
|
2013-04-18 14:34:02 -04:00
|
|
|
} else {
|
2013-04-19 07:55:21 -04:00
|
|
|
if (window.confirm('Swap cloud key?')) {
|
2013-04-18 14:34:02 -04:00
|
|
|
// upload local key to cloud
|
2013-05-18 22:00:53 -04:00
|
|
|
self.putPrivateKey(emailAddress, callback);
|
2013-04-18 14:34:02 -04:00
|
|
|
} else {
|
|
|
|
callback({
|
|
|
|
error: 'err',
|
|
|
|
status: 'Key not synced!'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2013-04-23 09:30:43 -04:00
|
|
|
// local and cloud keys are equal
|
2013-04-18 14:34:02 -04:00
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
2013-03-13 11:58:46 -04:00
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
};
|