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
|
|
|
//
|
2013-05-31 17:27:19 -04:00
|
|
|
// Generic Ajax helper functions
|
2013-04-19 07:55:21 -04:00
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
2013-05-31 17:27:19 -04:00
|
|
|
* GET (read) request
|
2013-04-19 07:55:21 -04:00
|
|
|
*/
|
2013-05-31 17:27:19 -04:00
|
|
|
this.get = function(uri, callback) {
|
2013-04-19 07:55:21 -04:00
|
|
|
$.ajax({
|
|
|
|
url: uri,
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
2013-05-31 17:27:19 -04:00
|
|
|
success: function(res) {
|
|
|
|
callback(null, res);
|
2013-04-19 07:55:21 -04:00
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
2013-05-31 17:27:19 -04:00
|
|
|
errMsg: xhr.status + ': ' + xhr.statusText
|
2013-04-19 07:55:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-05-31 17:27:19 -04:00
|
|
|
* PUT (update) request
|
2013-04-19 07:55:21 -04:00
|
|
|
*/
|
2013-05-31 17:27:19 -04:00
|
|
|
this.put = function(item, uri, callback) {
|
2013-04-19 07:55:21 -04:00
|
|
|
$.ajax({
|
|
|
|
url: uri,
|
|
|
|
type: 'PUT',
|
2013-05-31 17:27:19 -04:00
|
|
|
data: JSON.stringify(item),
|
2013-04-19 07:55:21 -04:00
|
|
|
contentType: 'application/json',
|
|
|
|
success: function() {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
2013-05-31 17:27:19 -04:00
|
|
|
errMsg: xhr.status + ': ' + xhr.statusText
|
2013-04-19 07:55:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-04-19 13:13:27 -04:00
|
|
|
/**
|
2013-05-31 17:27:19 -04:00
|
|
|
* DELETE (remove) request
|
2013-04-19 13:13:27 -04:00
|
|
|
*/
|
2013-05-31 17:27:19 -04:00
|
|
|
this.remove = function(uri, callback) {
|
2013-04-19 13:13:27 -04:00
|
|
|
$.ajax({
|
|
|
|
url: uri,
|
2013-05-31 17:27:19 -04:00
|
|
|
type: 'DELETE',
|
2013-04-19 13:13:27 -04:00
|
|
|
success: function() {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
2013-05-31 17:27:19 -04:00
|
|
|
errMsg: xhr.status + ': ' + xhr.statusText
|
2013-04-19 13:13:27 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-05-31 17:27:19 -04:00
|
|
|
//
|
|
|
|
// Encrypted Mail storage
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + item.id;
|
|
|
|
this.put(item, uri, callback);
|
|
|
|
};
|
|
|
|
|
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) {
|
2013-05-31 17:27:19 -04:00
|
|
|
var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName + '/' + id;
|
|
|
|
this.remove(uri, callback);
|
2013-04-23 10:35:01 -04:00
|
|
|
};
|
|
|
|
|
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-05-31 17:27:19 -04:00
|
|
|
var uri = app.config.cloudUrl + '/' + type + '/user/' + emailAddress + '/folder/' + folderName;
|
|
|
|
this.get(uri, callback);
|
|
|
|
};
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-05-31 17:27:19 -04:00
|
|
|
//
|
|
|
|
// Public Key
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the user's corresponding public key
|
|
|
|
*/
|
|
|
|
this.getPublicKey = function(keyId, callback) {
|
|
|
|
var uri = app.config.cloudUrl + '/publickey/key/' + keyId;
|
|
|
|
|
|
|
|
this.get(uri, function(err, key) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!key || !key._id) {
|
2013-04-02 09:02:57 -04:00
|
|
|
callback({
|
2013-05-31 17:27:19 -04:00
|
|
|
errMsg: 'No public key for that user!'
|
2013-04-02 09:02:57 -04:00
|
|
|
});
|
2013-05-31 17:27:19 -04:00
|
|
|
return;
|
2013-04-02 09:02:57 -04:00
|
|
|
}
|
2013-05-31 17:27:19 -04:00
|
|
|
|
|
|
|
callback(null, key);
|
2013-04-02 09:02:57 -04:00
|
|
|
});
|
|
|
|
};
|
2013-04-01 18:12:15 -04:00
|
|
|
|
2013-05-31 17:27:19 -04:00
|
|
|
/**
|
|
|
|
* Persist the user's publc key
|
|
|
|
*/
|
|
|
|
this.putPublicKey = function(pubkey, callback) {
|
|
|
|
var uri = app.config.cloudUrl + '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id;
|
|
|
|
this.put(pubkey, uri, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the public key from the cloud storage service
|
|
|
|
*/
|
|
|
|
this.removePublicKey = function(keyId, callback) {
|
|
|
|
var uri = app.config.cloudUrl + '/publickey/key/' + keyId;
|
|
|
|
this.remove(uri, callback);
|
|
|
|
};
|
|
|
|
|
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-31 17:27:19 -04:00
|
|
|
* Fetch private key by id
|
2013-04-02 09:02:57 -04:00
|
|
|
*/
|
2013-05-31 17:27:19 -04:00
|
|
|
this.getPrivateKey = function(keyId, callback) {
|
|
|
|
var uri = app.config.cloudUrl + '/privatekey/key/' + keyId;
|
|
|
|
this.get(uri, function(err, key) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-05-31 17:27:19 -04:00
|
|
|
if (!key || !key._id) {
|
2013-04-02 09:02:57 -04:00
|
|
|
callback({
|
2013-05-31 17:27:19 -04:00
|
|
|
errMsg: 'No private key for that user!'
|
2013-04-02 09:02:57 -04:00
|
|
|
});
|
2013-05-31 17:27:19 -04:00
|
|
|
return;
|
2013-04-02 09:02:57 -04:00
|
|
|
}
|
2013-05-31 17:27:19 -04:00
|
|
|
|
|
|
|
callback(null, key);
|
2013-04-02 09:02:57 -04:00
|
|
|
});
|
|
|
|
};
|
2013-03-13 11:58:46 -04:00
|
|
|
|
2013-05-31 17:27:19 -04:00
|
|
|
/**
|
|
|
|
* Persist encrypted private key to cloud service
|
|
|
|
*/
|
|
|
|
this.putPrivateKey = function(privkey, callback) {
|
|
|
|
var uri = app.config.cloudUrl + '/privatekey/user/' + privkey.userId + '/key/' + privkey._id;
|
|
|
|
this.put(privkey, uri, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete the private key from the cloud storage service
|
|
|
|
*/
|
|
|
|
this.removePrivateKey = function(keyId, callback) {
|
|
|
|
var uri = app.config.cloudUrl + '/privatekey/key/' + keyId;
|
|
|
|
this.remove(uri, callback);
|
|
|
|
};
|
|
|
|
|
2013-04-02 09:02:57 -04:00
|
|
|
};
|