Expose previous DAOs as angular services and as common.js modules

This commit is contained in:
Tankred Hase 2014-11-18 18:44:00 +01:00
parent f7b7e174b0
commit c8d56b4bd1
10 changed files with 167 additions and 146 deletions

View File

@ -3,10 +3,10 @@
var ngModule = angular.module('woServices');
ngModule.service('account', Account);
var EmailDAO = require('../dao/email-dao');
var Email = require('./email');
function Account() {
this._emailDAOs = [];
this._emailDAOs = [];
}
/**
@ -14,9 +14,9 @@ function Account() {
* @return {Array<Object>} The account objects containing folder and message objects
*/
Account.prototype.all = function() {
return this._emailDAOs.map(function(emailDao) {
return emailDao._account;
});
return this._emailDAOs.map(function(emailDao) {
return emailDao._account;
});
};
/**
@ -24,8 +24,8 @@ Account.prototype.all = function() {
* @param {String} options.emailAddress The account's email address
*/
Account.prototype.login = function(options) {
var emailDao = new EmailDAO();
this._emailDAOs.push(emailDao);
var emailDao = new Email();
this._emailDAOs.push(emailDao);
};
/**

View File

@ -1,8 +1,12 @@
'use strict';
var AdminDAO = function(restDao) {
var ngModule = angular.module('woServices');
ngModule.service('admin', Admin);
module.exports = Admin;
function Admin(restDao) {
this._restDao = restDao;
};
}
/**
* Create a new email account.
@ -11,7 +15,7 @@ var AdminDAO = function(restDao) {
* @param {String} options.phone The user's mobile phone number (required for verification and password reset).
* @param {Function} callback(error)
*/
AdminDAO.prototype.createUser = function(options, callback) {
Admin.prototype.createUser = function(options, callback) {
var uri;
if (!options.emailAddress || !options.password || !options.phone) {
@ -39,7 +43,7 @@ AdminDAO.prototype.createUser = function(options, callback) {
* @param {String} options.token The validation token.
* @param {Function} callback(error)
*/
AdminDAO.prototype.validateUser = function(options, callback) {
Admin.prototype.validateUser = function(options, callback) {
var uri;
if (!options.emailAddress || !options.token) {
@ -56,6 +60,4 @@ AdminDAO.prototype.validateUser = function(options, callback) {
callback(new Error('Validation failed!'));
}
});
};
module.exports = AdminDAO;
};

View File

@ -1,14 +1,17 @@
'use strict';
var ngModule = angular.module('woServices');
ngModule.service('deviceStorage', DeviceStorage);
module.exports = DeviceStorage;
/**
* High level storage api that handles all persistence on the device.
*/
'use strict';
var DeviceStorageDAO = function(localDbDao) {
function DeviceStorage(localDbDao) {
this._localDbDao = localDbDao;
};
}
DeviceStorageDAO.prototype.init = function(emailAddress, callback) {
DeviceStorage.prototype.init = function(emailAddress, callback) {
this._localDbDao.init(emailAddress, callback);
};
@ -17,7 +20,7 @@ DeviceStorageDAO.prototype.init = function(emailAddress, callback) {
* @param list [Array] The list of items to be persisted
* @param type [String] The type of item to be persisted e.g. 'email'
*/
DeviceStorageDAO.prototype.storeList = function(list, type, callback) {
DeviceStorage.prototype.storeList = function(list, type, callback) {
var key, items = [];
// nothing to store
@ -49,7 +52,7 @@ DeviceStorageDAO.prototype.storeList = function(list, type, callback) {
/**
* Deletes items of a certain type from storage
*/
DeviceStorageDAO.prototype.removeList = function(type, callback) {
DeviceStorage.prototype.removeList = function(type, callback) {
this._localDbDao.removeList(type, callback);
};
@ -59,7 +62,7 @@ DeviceStorageDAO.prototype.removeList = function(type, callback) {
* @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)
*/
DeviceStorageDAO.prototype.listItems = function(type, offset, num, callback) {
DeviceStorage.prototype.listItems = function(type, offset, num, callback) {
// fetch all items of a certain type from the data-store
this._localDbDao.list(type, offset, num, callback);
};
@ -67,7 +70,7 @@ DeviceStorageDAO.prototype.listItems = function(type, offset, num, callback) {
/**
* Clear the whole device data-store
*/
DeviceStorageDAO.prototype.clear = function(callback) {
DeviceStorage.prototype.clear = function(callback) {
this._localDbDao.clear(callback);
};
@ -88,6 +91,4 @@ function createKey(i, type) {
}
return key;
}
module.exports = DeviceStorageDAO;
}

View File

@ -1,5 +1,9 @@
'use strict';
var ngModule = angular.module('woServices');
ngModule.service('email', Email);
module.exports = Email;
var config = require('../app-config').config,
str = require('../app-config').string;
@ -32,7 +36,7 @@ var MSG_PART_TYPE_HTML = 'html';
//
//
// Email Dao
// Email Service
//
//
@ -46,13 +50,13 @@ var MSG_PART_TYPE_HTML = 'html';
* @param {Object} pgpbuilder Generates and encrypts MIME and SMTP messages
* @param {Object} mailreader Parses MIME messages received from IMAP
*/
var EmailDAO = function(keychain, pgp, devicestorage, pgpbuilder, mailreader) {
function Email(keychain, pgp, devicestorage, pgpbuilder, mailreader) {
this._keychain = keychain;
this._pgp = pgp;
this._devicestorage = devicestorage;
this._pgpbuilder = pgpbuilder;
this._mailreader = mailreader;
};
}
//
@ -71,7 +75,7 @@ var EmailDAO = function(keychain, pgp, devicestorage, pgpbuilder, mailreader) {
* @param {String} options.account.realname The user's id
* @param {Function} callback(error, keypair) Invoked with the keypair or error information when the email dao is initialized
*/
EmailDAO.prototype.init = function(options, callback) {
Email.prototype.init = function(options, callback) {
this._account = options.account;
this._account.busy = 0; // > 0 triggers the spinner
this._account.online = false;
@ -86,7 +90,7 @@ EmailDAO.prototype.init = function(options, callback) {
* @param {String} options.passphrase The passphrase to decrypt the private key
* @param {Function} callback(error) Invoked when the the keychain is unlocked or when an error occurred buring unlocking
*/
EmailDAO.prototype.unlock = function(options, callback) {
Email.prototype.unlock = function(options, callback) {
var self = this;
if (options.keypair) {
@ -202,7 +206,7 @@ EmailDAO.prototype.unlock = function(options, callback) {
* @param {Object} options.folder The folder to be opened
* @param {Function} callback(error) Invoked when the folder has been opened
*/
EmailDAO.prototype.openFolder = function(options, callback) {
Email.prototype.openFolder = function(options, callback) {
var self = this,
err;
@ -229,7 +233,7 @@ EmailDAO.prototype.openFolder = function(options, callback) {
* @param {Object} options.folder The folder to synchronize
* @param {Function} callback [description]
*/
EmailDAO.prototype.refreshFolder = function(options, callback) {
Email.prototype.refreshFolder = function(options, callback) {
var self = this,
folder = options.folder;
@ -289,7 +293,7 @@ EmailDAO.prototype.refreshFolder = function(options, callback) {
* @param {Object} options.folder The folder for which to fetch the message
* @param {Function} callback(error) Invoked when the message is persisted and added to folder.messages
*/
EmailDAO.prototype.fetchMessages = function(options, callback) {
Email.prototype.fetchMessages = function(options, callback) {
var self = this,
folder = options.folder;
@ -440,7 +444,7 @@ EmailDAO.prototype.fetchMessages = function(options, callback) {
* @param {Boolean} options.localOnly Indicated if the message should not be removed from IMAP
* @param {Function} callback(error) Invoked when the message was delete, or an error occurred
*/
EmailDAO.prototype.deleteMessage = function(options, callback) {
Email.prototype.deleteMessage = function(options, callback) {
var self = this,
folder = options.folder,
message = options.message;
@ -508,7 +512,7 @@ EmailDAO.prototype.deleteMessage = function(options, callback) {
* @param {[type]} options [description]
* @param {Function} callback [description]
*/
EmailDAO.prototype.setFlags = function(options, callback) {
Email.prototype.setFlags = function(options, callback) {
var self = this,
folder = options.folder,
message = options.message;
@ -600,7 +604,7 @@ EmailDAO.prototype.setFlags = function(options, callback) {
* @param {Object} options.message The message that should be moved
* @param {Function} callback(error) Invoked when the message was moved, or an error occurred
*/
EmailDAO.prototype.moveMessage = function(options, callback) {
Email.prototype.moveMessage = function(options, callback) {
var self = this,
folder = options.folder,
destination = options.destination,
@ -652,7 +656,7 @@ EmailDAO.prototype.moveMessage = function(options, callback) {
* @param {Object} options.folder The IMAP folder
* @param {Function} callback(error, message) Invoked when the message is streamed, or provides information if an error occurred
*/
EmailDAO.prototype.getBody = function(options, callback) {
Email.prototype.getBody = function(options, callback) {
var self = this,
message = options.message,
folder = options.folder;
@ -837,7 +841,7 @@ EmailDAO.prototype.getBody = function(options, callback) {
}
};
EmailDAO.prototype._checkSignatures = function(message, callback) {
Email.prototype._checkSignatures = function(message, callback) {
var self = this;
self._keychain.getReceiverPublicKey(message.from[0].address, function(err, senderPublicKey) {
@ -866,7 +870,7 @@ EmailDAO.prototype._checkSignatures = function(message, callback) {
* @param {Object} options.attachment The attachment body part to fetch and parse from IMAP
* @param {Function} callback(error, attachment) Invoked when the attachment body part was retrieved and parsed, or an error occurred
*/
EmailDAO.prototype.getAttachment = function(options, callback) {
Email.prototype.getAttachment = function(options, callback) {
var self = this,
attachment = options.attachment;
@ -896,7 +900,7 @@ EmailDAO.prototype.getAttachment = function(options, callback) {
* @param {Object} options.message The message
* @param {Function} callback(error, message)
*/
EmailDAO.prototype.decryptBody = function(options, callback) {
Email.prototype.decryptBody = function(options, callback) {
var self = this,
message = options.message;
@ -1016,7 +1020,7 @@ EmailDAO.prototype.decryptBody = function(options, callback) {
* @param {Object} options.email The message to be sent
* @param {Function} callback(error) Invoked when the message was sent, or an error occurred
*/
EmailDAO.prototype.sendEncrypted = function(options, callback) {
Email.prototype.sendEncrypted = function(options, callback) {
var self = this;
if (!self._account.online) {
@ -1056,7 +1060,7 @@ EmailDAO.prototype.sendEncrypted = function(options, callback) {
* @param {Object} options.email The message to be sent
* @param {Function} callback(error) Invoked when the message was sent, or an error occurred
*/
EmailDAO.prototype.sendPlaintext = function(options, callback) {
Email.prototype.sendPlaintext = function(options, callback) {
var self = this;
if (!self._account.online) {
@ -1097,7 +1101,7 @@ EmailDAO.prototype.sendPlaintext = function(options, callback) {
* @param {Object} options.email The message to be encrypted
* @param {Function} callback(error, message) Invoked when the message was encrypted, or an error occurred
*/
EmailDAO.prototype.encrypt = function(options, callback) {
Email.prototype.encrypt = function(options, callback) {
var self = this;
self.busy();
@ -1124,7 +1128,7 @@ EmailDAO.prototype.encrypt = function(options, callback) {
* @param {Object} options.pgpMailer The SMTP client used to send messages
* @param {Function} callback [description]
*/
EmailDAO.prototype.onConnect = function(options, callback) {
Email.prototype.onConnect = function(options, callback) {
var self = this;
self._account.loggingIn = true;
@ -1212,7 +1216,7 @@ EmailDAO.prototype.onConnect = function(options, callback) {
* This handler should be invoked when navigator.onLine === false.
* It will discard the imap client and pgp mailer
*/
EmailDAO.prototype.onDisconnect = function(callback) {
Email.prototype.onDisconnect = function(callback) {
var self = this;
// logout of imap-client
@ -1239,7 +1243,7 @@ EmailDAO.prototype.onDisconnect = function(callback) {
* @param {String} options.path The mailbox for which updates are available
* @param {Array} options.list Array containing update information. Number (uid) or mail with Object (uid and flags), respectively
*/
EmailDAO.prototype._onSyncUpdate = function(options) {
Email.prototype._onSyncUpdate = function(options) {
var self = this;
var folder = _.findWhere(self._account.folders, {
@ -1319,7 +1323,7 @@ EmailDAO.prototype._onSyncUpdate = function(options) {
*
* @param {Function} callback Invoked when the folders are up to date
*/
EmailDAO.prototype._initFoldersFromDisk = function(callback) {
Email.prototype._initFoldersFromDisk = function(callback) {
var self = this;
self.busy(); // start the spinner
@ -1347,7 +1351,7 @@ EmailDAO.prototype._initFoldersFromDisk = function(callback) {
*
* @param {Function} callback Invoked when the folders are up to date
*/
EmailDAO.prototype._initFoldersFromImap = function(callback) {
Email.prototype._initFoldersFromImap = function(callback) {
var self = this;
self.busy(); // start the spinner
@ -1498,7 +1502,7 @@ EmailDAO.prototype._initFoldersFromImap = function(callback) {
*
* @param {Function} callback Invoked when the folders are filled with messages
*/
EmailDAO.prototype._initMessagesFromDisk = function(callback) {
Email.prototype._initMessagesFromDisk = function(callback) {
var self = this;
if (!self._account.folders || self._account.folders.length === 0) {
@ -1526,11 +1530,11 @@ EmailDAO.prototype._initMessagesFromDisk = function(callback) {
});
};
EmailDAO.prototype.busy = function() {
Email.prototype.busy = function() {
this._account.busy++;
};
EmailDAO.prototype.done = function() {
Email.prototype.done = function() {
if (this._account.busy > 0) {
this._account.busy--;
}
@ -1552,7 +1556,7 @@ EmailDAO.prototype.done = function() {
* @param {Number} options.unread Un-/Read flag
* @param {Number} options.answered Un-/Answered flag
*/
EmailDAO.prototype._imapMark = function(options, callback) {
Email.prototype._imapMark = function(options, callback) {
if (!this._account.online) {
callback({
errMsg: 'Client is currently offline!',
@ -1573,7 +1577,7 @@ EmailDAO.prototype._imapMark = function(options, callback) {
* @param {Number} options.uid The uid of the message
* @param {Function} callback(error) Callback with an error object in case something went wrong.
*/
EmailDAO.prototype._imapDeleteMessage = function(options, callback) {
Email.prototype._imapDeleteMessage = function(options, callback) {
if (!this._account.online) {
callback({
errMsg: 'Client is currently offline!',
@ -1611,7 +1615,7 @@ EmailDAO.prototype._imapDeleteMessage = function(options, callback) {
* @param {String} options.uid the message's uid
* @param {Function} callback (error) The callback when the message is moved
*/
EmailDAO.prototype._imapMoveMessage = function(options, callback) {
Email.prototype._imapMoveMessage = function(options, callback) {
this._imapClient.moveMessage({
path: options.folder.path,
destination: options.destination.path,
@ -1628,7 +1632,7 @@ EmailDAO.prototype._imapMoveMessage = function(options, callback) {
* @param {Number} options.lastUid The upper bound of the uid range (inclusive)
* @param {Function} callback (error, messages) The callback when the imap client is done fetching message metadata
*/
EmailDAO.prototype._imapListMessages = function(options, callback) {
Email.prototype._imapListMessages = function(options, callback) {
var self = this;
if (!this._account.online) {
@ -1650,7 +1654,7 @@ EmailDAO.prototype._imapListMessages = function(options, callback) {
* @param {String} options.message The rfc2822 compatible raw ASCII e-mail source
* @param {Function} callback (error) The callback when the imap client is done uploading
*/
EmailDAO.prototype._imapUploadMessage = function(options, callback) {
Email.prototype._imapUploadMessage = function(options, callback) {
this._imapClient.uploadMessage({
path: options.folder.path,
message: options.message
@ -1664,7 +1668,7 @@ EmailDAO.prototype._imapUploadMessage = function(options, callback) {
* @param {Object} options.bodyParts The message, as retrieved by _imapListMessages
* @param {Function} callback (error, message) The callback when the imap client is done streaming message text content
*/
EmailDAO.prototype._getBodyParts = function(options, callback) {
Email.prototype._getBodyParts = function(options, callback) {
var self = this;
if (!self._account.online) {
@ -1702,7 +1706,7 @@ EmailDAO.prototype._getBodyParts = function(options, callback) {
* @param {Object} options.uid A specific uid to look up locally in the folder
* @param {Function} callback(error, list) Invoked with the results of the query, or further information, if an error occurred
*/
EmailDAO.prototype._localListMessages = function(options, callback) {
Email.prototype._localListMessages = function(options, callback) {
var dbType = 'email_' + options.folder.path + (options.uid ? '_' + options.uid : '');
this._devicestorage.listItems(dbType, 0, null, callback);
};
@ -1714,7 +1718,7 @@ EmailDAO.prototype._localListMessages = function(options, callback) {
* @param {Array} options.messages The messages to store
* @param {Function} callback(error, list) Invoked with the results of the query, or further information, if an error occurred
*/
EmailDAO.prototype._localStoreMessages = function(options, callback) {
Email.prototype._localStoreMessages = function(options, callback) {
var dbType = 'email_' + options.folder.path;
this._devicestorage.storeList(options.emails, dbType, callback);
};
@ -1726,7 +1730,7 @@ EmailDAO.prototype._localStoreMessages = function(options, callback) {
* @param {Array} options.messages The messages to store
* @param {Function} callback(error, list) Invoked with the results of the query, or further information, if an error occurred
*/
EmailDAO.prototype._localDeleteMessage = function(options, callback) {
Email.prototype._localDeleteMessage = function(options, callback) {
var path = options.folder.path,
uid = options.uid,
id = options.id;
@ -1756,7 +1760,7 @@ EmailDAO.prototype._localDeleteMessage = function(options, callback) {
* @param {String} options.message The rfc2822 compatible raw ASCII e-mail source
* @param {Function} callback (error) The callback when the imap client is done uploading
*/
EmailDAO.prototype._uploadToSent = function(options, callback) {
Email.prototype._uploadToSent = function(options, callback) {
var self = this;
self.busy();
@ -1796,7 +1800,7 @@ EmailDAO.prototype._uploadToSent = function(options, callback) {
* @param {String} hostname The hostname to check
* @return {Boolean} true if upload can be ignored, otherwise false
*/
EmailDAO.prototype.checkIgnoreUploadOnSent = function(hostname) {
Email.prototype.checkIgnoreUploadOnSent = function(hostname) {
for (var i = 0; i < config.ignoreUploadOnSentDomains.length; i++) {
if (config.ignoreUploadOnSentDomains[i].test(hostname)) {
return true;
@ -1876,6 +1880,4 @@ function inlineExternalImages(message) {
return prefix + localSource + suffix;
});
}
module.exports = EmailDAO;
}

View File

@ -1,20 +1,24 @@
'use strict';
var ngModule = angular.module('woServices');
ngModule.service('invitation', Invitation);
module.exports = Invitation;
/**
* The InvitationDAO is a high level Data Access Object that access the invitation service REST endpoint.
* The Invitation is a high level Data Access Object that access the invitation service REST endpoint.
* @param {Object} restDao The REST Data Access Object abstraction
*/
var InvitationDAO = function(restDao) {
function Invitation(restDao) {
this._restDao = restDao;
};
}
//
// Constants
//
InvitationDAO.INVITE_MISSING = 1;
InvitationDAO.INVITE_PENDING = 2;
InvitationDAO.INVITE_SUCCESS = 4;
Invitation.INVITE_MISSING = 1;
Invitation.INVITE_PENDING = 2;
Invitation.INVITE_SUCCESS = 4;
//
// API
@ -26,7 +30,7 @@ InvitationDAO.INVITE_SUCCESS = 4;
* @param {String} options.sender User ID of the sender
* @param {Function} callback(error, status) Returns information if the invitation worked (INVITE_SUCCESS), if an invitation is already pendin (INVITE_PENDING), or information if an error occurred.
*/
InvitationDAO.prototype.invite = function(options, callback) {
Invitation.prototype.invite = function(options, callback) {
if (typeof options !== 'object' || typeof options.recipient !== 'string' || typeof options.recipient !== 'string') {
callback({
errMsg: 'erroneous usage of api: incorrect parameters!'
@ -44,10 +48,10 @@ InvitationDAO.prototype.invite = function(options, callback) {
}
if (status === 201) {
callback(null, InvitationDAO.INVITE_SUCCESS);
callback(null, Invitation.INVITE_SUCCESS);
return;
} else if (status === 304) {
callback(null, InvitationDAO.INVITE_PENDING);
callback(null, Invitation.INVITE_PENDING);
return;
}
@ -55,6 +59,4 @@ InvitationDAO.prototype.invite = function(options, callback) {
errMsg: 'unexpected invitation state'
});
}
};
module.exports = InvitationDAO;
};

View File

@ -1,10 +1,9 @@
/**
* A high-level Data-Access Api for handling Keypair synchronization
* between the cloud service and the device's local storage
*/
'use strict';
var ngModule = angular.module('woServices');
ngModule.service('keychain', Keychain);
module.exports = Keychain;
var util = require('crypto-lib').util,
config = require('../app-config').config;
@ -13,13 +12,17 @@ var DB_PUBLICKEY = 'publickey',
DB_DEVICENAME = 'devicename',
DB_DEVICE_SECRET = 'devicesecret';
var KeychainDAO = function(localDbDao, publicKeyDao, privateKeyDao, crypto, pgp) {
/**
* A high-level Data-Access Api for handling Keypair synchronization
* between the cloud service and the device's local storage
*/
function Keychain(localDbDao, publicKeyDao, privateKeyDao, crypto, pgp) {
this._localDbDao = localDbDao;
this._publicKeyDao = publicKeyDao;
this._privateKeyDao = privateKeyDao;
this._crypto = crypto;
this._pgp = pgp;
};
}
//
// Public key functions
@ -30,7 +33,7 @@ var KeychainDAO = function(localDbDao, publicKeyDao, privateKeyDao, crypto, pgp)
* @param {String} uuid The uuid to verify the key
* @param {Function} callback(error) Callback with an optional error object when the verification is done. If the was an error, the error object contains the information for it.
*/
KeychainDAO.prototype.verifyPublicKey = function(uuid, callback) {
Keychain.prototype.verifyPublicKey = function(uuid, callback) {
this._publicKeyDao.verify(uuid, callback);
};
@ -40,7 +43,7 @@ KeychainDAO.prototype.verifyPublicKey = function(uuid, callback) {
* @param ids [Array] the key ids as [{_id, userId}]
* @return [PublicKeyCollection] The requiested public keys
*/
KeychainDAO.prototype.getPublicKeys = function(ids, callback) {
Keychain.prototype.getPublicKeys = function(ids, callback) {
var self = this,
after, already, pubkeys = [];
@ -85,7 +88,7 @@ KeychainDAO.prototype.getPublicKeys = function(ids, callback) {
* @param {String} options.overridePermission (optional) Indicates if the update should happen automatically (true) or with the user being queried (false). Defaults to false
* @param {Function} callback(error, key) Invoked when the key has been updated or an error occurred
*/
KeychainDAO.prototype.refreshKeyForUserId = function(options, callback) {
Keychain.prototype.refreshKeyForUserId = function(options, callback) {
var self = this,
userId = options.userId,
overridePermission = options.overridePermission;
@ -189,7 +192,7 @@ KeychainDAO.prototype.refreshKeyForUserId = function(options, callback) {
* Look up a reveiver's public key by user id
* @param userId [String] the receiver's email address
*/
KeychainDAO.prototype.getReceiverPublicKey = function(userId, callback) {
Keychain.prototype.getReceiverPublicKey = function(userId, callback) {
var self = this;
// search local keyring for public key
@ -266,7 +269,7 @@ KeychainDAO.prototype.getReceiverPublicKey = function(userId, callback) {
* @param {String} deviceName The device name
* @param {Function} callback(error)
*/
KeychainDAO.prototype.setDeviceName = function(deviceName, callback) {
Keychain.prototype.setDeviceName = function(deviceName, callback) {
if (!deviceName) {
callback(new Error('Please set a device name!'));
return;
@ -280,7 +283,7 @@ KeychainDAO.prototype.setDeviceName = function(deviceName, callback) {
* @param {Function} callback(error, deviceName)
* @return {String} The device name
*/
KeychainDAO.prototype.getDeviceName = function(callback) {
Keychain.prototype.getDeviceName = function(callback) {
// check if deviceName is already persisted in storage
this._localDbDao.read(DB_DEVICENAME, function(err, deviceName) {
if (err) {
@ -301,7 +304,7 @@ KeychainDAO.prototype.getDeviceName = function(callback) {
* Geneate a device specific key and secret to authenticate to the private key service.
* @param {Function} callback(error, deviceSecret:[base64 encoded string])
*/
KeychainDAO.prototype.getDeviceSecret = function(callback) {
Keychain.prototype.getDeviceSecret = function(callback) {
var self = this;
// generate random deviceSecret or get from storage
@ -336,7 +339,7 @@ KeychainDAO.prototype.getDeviceSecret = function(callback) {
* @param {String} options.userId The user's email address
* @param {Function} callback(error)
*/
KeychainDAO.prototype.registerDevice = function(options, callback) {
Keychain.prototype.registerDevice = function(options, callback) {
var self = this,
devName;
@ -435,7 +438,7 @@ KeychainDAO.prototype.registerDevice = function(options, callback) {
* @param {Function} callback(error, authSessionKey)
* @return {Object} {sessionId:String, sessionKey:[base64 encoded]}
*/
KeychainDAO.prototype._authenticateToPrivateKeyServer = function(userId, callback) {
Keychain.prototype._authenticateToPrivateKeyServer = function(userId, callback) {
var self = this,
sessionId;
@ -552,7 +555,7 @@ KeychainDAO.prototype._authenticateToPrivateKeyServer = function(userId, callbac
* @param {String} options.code The randomly generated or self selected code used to derive the key for the encryption of the private PGP key
* @param {Function} callback(error)
*/
KeychainDAO.prototype.uploadPrivateKey = function(options, callback) {
Keychain.prototype.uploadPrivateKey = function(options, callback) {
var self = this,
keySize = config.symKeySize,
salt;
@ -646,7 +649,7 @@ KeychainDAO.prototype.uploadPrivateKey = function(options, callback) {
* @param {String} options.keyId The private PGP key id
* @param {Function} callback(error)
*/
KeychainDAO.prototype.requestPrivateKeyDownload = function(options, callback) {
Keychain.prototype.requestPrivateKeyDownload = function(options, callback) {
this._privateKeyDao.requestDownload(options, callback);
};
@ -656,7 +659,7 @@ KeychainDAO.prototype.requestPrivateKeyDownload = function(options, callback) {
* @param {String} options.keyId The private PGP key id
* @param {Function} callback(error)
*/
KeychainDAO.prototype.hasPrivateKey = function(options, callback) {
Keychain.prototype.hasPrivateKey = function(options, callback) {
this._privateKeyDao.hasPrivateKey(options, callback);
};
@ -667,7 +670,7 @@ KeychainDAO.prototype.hasPrivateKey = function(options, callback) {
* @param {String} options.recoveryToken The recovery token acquired via email/sms from the key server
* @param {Function} callback(error, encryptedPrivateKey)
*/
KeychainDAO.prototype.downloadPrivateKey = function(options, callback) {
Keychain.prototype.downloadPrivateKey = function(options, callback) {
this._privateKeyDao.download(options, callback);
};
@ -681,7 +684,7 @@ KeychainDAO.prototype.downloadPrivateKey = function(options, callback) {
* @param {String} options.iv The iv used to encrypt the private PGP key
* @param {Function} callback(error, keyObject)
*/
KeychainDAO.prototype.decryptAndStorePrivateKeyLocally = function(options, callback) {
Keychain.prototype.decryptAndStorePrivateKeyLocally = function(options, callback) {
var self = this,
code = options.code,
salt = options.salt,
@ -756,7 +759,7 @@ KeychainDAO.prototype.decryptAndStorePrivateKeyLocally = function(options, callb
* If no key pair exists, null is returned.
* return [Object] The user's key pair {publicKey, privateKey}
*/
KeychainDAO.prototype.getUserKeyPair = function(userId, callback) {
Keychain.prototype.getUserKeyPair = function(userId, callback) {
var self = this;
// search for user's public key locally
@ -833,7 +836,7 @@ KeychainDAO.prototype.getUserKeyPair = function(userId, callback) {
* locally and in the cloud and persist arccordingly
* @param [Object] The user's key pair {publicKey, privateKey}
*/
KeychainDAO.prototype.putUserKeyPair = function(keypair, callback) {
Keychain.prototype.putUserKeyPair = function(keypair, callback) {
var self = this;
// validate input
@ -872,7 +875,7 @@ KeychainDAO.prototype.putUserKeyPair = function(keypair, callback) {
// Helper functions
//
KeychainDAO.prototype.lookupPublicKey = function(id, callback) {
Keychain.prototype.lookupPublicKey = function(id, callback) {
var self = this;
if (!id) {
@ -917,30 +920,28 @@ KeychainDAO.prototype.lookupPublicKey = function(id, callback) {
/**
* List all the locally stored public keys
*/
KeychainDAO.prototype.listLocalPublicKeys = function(callback) {
Keychain.prototype.listLocalPublicKeys = function(callback) {
// search local keyring for public key
this._localDbDao.list(DB_PUBLICKEY, 0, null, callback);
};
KeychainDAO.prototype.removeLocalPublicKey = function(id, callback) {
Keychain.prototype.removeLocalPublicKey = function(id, callback) {
this._localDbDao.remove(DB_PUBLICKEY + '_' + id, callback);
};
KeychainDAO.prototype.lookupPrivateKey = function(id, callback) {
Keychain.prototype.lookupPrivateKey = function(id, callback) {
// lookup in local storage
this._localDbDao.read(DB_PRIVATEKEY + '_' + id, callback);
};
KeychainDAO.prototype.saveLocalPublicKey = function(pubkey, callback) {
Keychain.prototype.saveLocalPublicKey = function(pubkey, callback) {
// persist public key (email, _id)
var pkLookupKey = DB_PUBLICKEY + '_' + pubkey._id;
this._localDbDao.persist(pkLookupKey, pubkey, callback);
};
KeychainDAO.prototype.saveLocalPrivateKey = function(privkey, callback) {
Keychain.prototype.saveLocalPrivateKey = function(privkey, callback) {
// persist private key (email, _id)
var prkLookupKey = DB_PRIVATEKEY + '_' + privkey._id;
this._localDbDao.persist(prkLookupKey, privkey, callback);
};
module.exports = KeychainDAO;
};

View File

@ -1,11 +1,18 @@
'use strict';
var ngModule = angular.module('woServices');
ngModule.service('lawnchairDAO', LawnchairDAO);
module.exports = LawnchairDAO;
/**
* Handles generic caching of JSON objects in a lawnchair adapter
*/
function LawnchairDAO() {}
'use strict';
var LawnchairDAO = function() {};
/**
* Initialize the lawnchair database
* @param {String} dbName The name of the database
*/
LawnchairDAO.prototype.init = function(dbName, callback) {
if (!dbName) {
callback({
@ -211,6 +218,4 @@ LawnchairDAO.prototype.removeList = function(type, callback) {
*/
LawnchairDAO.prototype.clear = function(callback) {
this._db.nuke(callback);
};
module.exports = LawnchairDAO;
};

View File

@ -1,8 +1,12 @@
'use strict';
var PrivateKeyDAO = function(restDao) {
var ngModule = angular.module('woServices');
ngModule.service('privateKey', PrivateKey);
module.exports = PrivateKey;
function PrivateKey(restDao) {
this._restDao = restDao;
};
}
//
// Device registration functions
@ -15,7 +19,7 @@ var PrivateKeyDAO = function(restDao) {
* @param {Function} callback(error, regSessionKey)
* @return {Object} {encryptedRegSessionKey:[base64]}
*/
PrivateKeyDAO.prototype.requestDeviceRegistration = function(options, callback) {
PrivateKey.prototype.requestDeviceRegistration = function(options, callback) {
var uri;
if (!options.userId || !options.deviceName) {
@ -35,7 +39,7 @@ PrivateKeyDAO.prototype.requestDeviceRegistration = function(options, callback)
* @param {String} options.iv The iv used for encryption
* @param {Function} callback(error)
*/
PrivateKeyDAO.prototype.uploadDeviceSecret = function(options, callback) {
PrivateKey.prototype.uploadDeviceSecret = function(options, callback) {
var uri;
if (!options.userId || !options.deviceName || !options.encryptedDeviceSecret || !options.iv) {
@ -57,7 +61,7 @@ PrivateKeyDAO.prototype.uploadDeviceSecret = function(options, callback) {
* @param {Function} callback(error, authSessionKey)
* @return {Object} {sessionId, encryptedAuthSessionKey:[base64 encoded], encryptedChallenge:[base64 encoded]}
*/
PrivateKeyDAO.prototype.requestAuthSessionKey = function(options, callback) {
PrivateKey.prototype.requestAuthSessionKey = function(options, callback) {
var uri;
if (!options.userId) {
@ -77,7 +81,7 @@ PrivateKeyDAO.prototype.requestAuthSessionKey = function(options, callback) {
* @param {String} options.iv The iv used for encryption
* @param {Function} callback(error)
*/
PrivateKeyDAO.prototype.verifyAuthentication = function(options, callback) {
PrivateKey.prototype.verifyAuthentication = function(options, callback) {
var uri;
if (!options.userId || !options.sessionId || !options.encryptedChallenge || !options.encryptedDeviceSecret || !options.iv) {
@ -97,7 +101,7 @@ PrivateKeyDAO.prototype.verifyAuthentication = function(options, callback) {
* @param {String} options.sessionId The session id
* @param {Function} callback(error)
*/
PrivateKeyDAO.prototype.upload = function(options, callback) {
PrivateKey.prototype.upload = function(options, callback) {
var uri;
if (!options._id || !options.userId || !options.encryptedPrivateKey || !options.sessionId || !options.salt || !options.iv) {
@ -116,7 +120,7 @@ PrivateKeyDAO.prototype.upload = function(options, callback) {
* @param {Function} callback(error, found)
* @return {Boolean} whether the key was found on the server or not.
*/
PrivateKeyDAO.prototype.hasPrivateKey = function(options, callback) {
PrivateKey.prototype.hasPrivateKey = function(options, callback) {
if (!options.userId || !options.keyId) {
callback(new Error('Incomplete arguments!'));
return;
@ -147,7 +151,7 @@ PrivateKeyDAO.prototype.hasPrivateKey = function(options, callback) {
* @param {Function} callback(error, found)
* @return {Boolean} whether the key was found on the server or not.
*/
PrivateKeyDAO.prototype.requestDownload = function(options, callback) {
PrivateKey.prototype.requestDownload = function(options, callback) {
if (!options.userId || !options.keyId) {
callback(new Error('Incomplete arguments!'));
return;
@ -179,7 +183,7 @@ PrivateKeyDAO.prototype.requestDownload = function(options, callback) {
* @param {Function} callback(error, encryptedPrivateKey)
* @return {Object} {_id:[hex encoded capital 16 char key id], encryptedPrivateKey:[base64 encoded], encryptedUserId: [base64 encoded]}
*/
PrivateKeyDAO.prototype.download = function(options, callback) {
PrivateKey.prototype.download = function(options, callback) {
var uri;
if (!options.userId || !options.keyId || !options.recoveryToken) {
@ -191,6 +195,4 @@ PrivateKeyDAO.prototype.download = function(options, callback) {
this._restDao.get({
uri: uri
}, callback);
};
module.exports = PrivateKeyDAO;
};

View File

@ -1,13 +1,17 @@
'use strict';
var PublicKeyDAO = function(restDao) {
var ngModule = angular.module('woServices');
ngModule.service('publicKey', PublicKey);
module.exports = PublicKey;
function PublicKey(restDao) {
this._restDao = restDao;
};
}
/**
* Verify the public key behind the given uuid
*/
PublicKeyDAO.prototype.verify = function(uuid, callback) {
PublicKey.prototype.verify = function(uuid, callback) {
var uri = '/verify/' + uuid;
this._restDao.get({
@ -27,7 +31,7 @@ PublicKeyDAO.prototype.verify = function(uuid, callback) {
/**
* Find the user's corresponding public key
*/
PublicKeyDAO.prototype.get = function(keyId, callback) {
PublicKey.prototype.get = function(keyId, callback) {
var uri = '/publickey/key/' + keyId;
this._restDao.get({
@ -50,7 +54,7 @@ PublicKeyDAO.prototype.get = function(keyId, callback) {
/**
* Find the user's corresponding public key by email
*/
PublicKeyDAO.prototype.getByUserId = function(userId, callback) {
PublicKey.prototype.getByUserId = function(userId, callback) {
var uri = '/publickey/user/' + userId;
this._restDao.get({
@ -87,7 +91,7 @@ PublicKeyDAO.prototype.getByUserId = function(userId, callback) {
/**
* Persist the user's publc key
*/
PublicKeyDAO.prototype.put = function(pubkey, callback) {
PublicKey.prototype.put = function(pubkey, callback) {
var uri = '/publickey/user/' + pubkey.userId + '/key/' + pubkey._id;
this._restDao.put(pubkey, uri, callback);
};
@ -95,9 +99,7 @@ PublicKeyDAO.prototype.put = function(pubkey, callback) {
/**
* Delete the public key from the cloud storage service
*/
PublicKeyDAO.prototype.remove = function(keyId, callback) {
PublicKey.prototype.remove = function(keyId, callback) {
var uri = '/publickey/key/' + keyId;
this._restDao.remove(uri, callback);
};
module.exports = PublicKeyDAO;
};

View File

@ -1,14 +1,20 @@
'use strict';
var ngModule = angular.module('woServices');
ngModule.factory('restDao', function() {
return new RestDAO();
});
module.exports = RestDAO;
var config = require('../app-config').config;
var RestDAO = function(baseUri) {
function RestDAO(baseUri) {
if (baseUri) {
this._baseUri = baseUri;
} else {
this._baseUri = config.cloudUrl;
}
};
}
/**
* GET (read) request
@ -116,6 +122,4 @@ RestDAO.prototype._processRequest = function(options, callback) {
};
xhr.send(options.payload ? JSON.stringify(options.payload) : undefined);
};
module.exports = RestDAO;
};