2013-06-06 07:34:22 -04:00
|
|
|
/**
|
|
|
|
* The main application controller
|
|
|
|
*/
|
2013-08-20 09:19:13 -04:00
|
|
|
define(function(require) {
|
2013-08-20 13:48:49 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var $ = require('jquery'),
|
2013-09-19 12:35:12 -04:00
|
|
|
ImapClient = require('imap-client'),
|
|
|
|
SmtpClient = require('smtp-client'),
|
2013-08-20 13:48:49 -04:00
|
|
|
EmailDAO = require('js/dao/email-dao'),
|
|
|
|
KeychainDAO = require('js/dao/keychain-dao'),
|
|
|
|
cloudstorage = require('js/dao/cloudstorage-dao'),
|
2013-09-26 07:26:57 -04:00
|
|
|
DeviceStorageDAO = require('js/dao/devicestorage-dao'),
|
2013-10-11 21:19:01 -04:00
|
|
|
PGP = require('js/crypto/pgp'),
|
2013-09-15 09:13:19 -04:00
|
|
|
config = require('js/app-config').config;
|
2013-08-20 13:48:49 -04:00
|
|
|
require('cordova');
|
|
|
|
|
|
|
|
var self = {};
|
|
|
|
|
|
|
|
/**
|
2013-10-09 10:40:36 -04:00
|
|
|
* Start the application
|
2013-08-20 13:48:49 -04:00
|
|
|
*/
|
|
|
|
self.start = function(callback) {
|
|
|
|
// are we running in native app or in browser?
|
|
|
|
if (document.URL.indexOf("http") === 0 || document.URL.indexOf("app") === 0 || document.URL.indexOf("chrome") === 0) {
|
|
|
|
console.log('Assuming Browser environment...');
|
|
|
|
onDeviceReady();
|
|
|
|
} else {
|
|
|
|
console.log('Assuming Cordova environment...');
|
|
|
|
document.addEventListener("deviceready", onDeviceReady, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onDeviceReady() {
|
|
|
|
console.log('Starting app.');
|
2013-10-09 10:40:36 -04:00
|
|
|
// init app config storage
|
|
|
|
self._appConfigStore = new DeviceStorageDAO();
|
|
|
|
self._appConfigStore.init('app-config', callback);
|
2013-08-20 13:48:49 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-09-26 09:48:32 -04:00
|
|
|
/**
|
|
|
|
* Request an OAuth token from chrome for gmail users
|
|
|
|
*/
|
2013-10-12 13:39:09 -04:00
|
|
|
self.fetchOAuthToken = function(passphrase, callback) {
|
2013-08-20 13:48:49 -04:00
|
|
|
// get OAuth Token from chrome
|
|
|
|
chrome.identity.getAuthToken({
|
|
|
|
'interactive': true
|
|
|
|
},
|
|
|
|
function(token) {
|
2013-09-26 09:48:32 -04:00
|
|
|
if (!token) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Error fetching an OAuth token for the user!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get email address for the token
|
|
|
|
self.queryEmailAddress(token, function(err, emailAddress) {
|
|
|
|
if (err || !emailAddress) {
|
2013-08-20 13:48:49 -04:00
|
|
|
callback({
|
2013-09-26 09:48:32 -04:00
|
|
|
errMsg: 'Error looking up email address on login!',
|
2013-08-20 13:48:49 -04:00
|
|
|
err: err
|
|
|
|
});
|
2013-09-26 09:48:32 -04:00
|
|
|
return;
|
2013-08-20 13:48:49 -04:00
|
|
|
}
|
|
|
|
|
2013-10-12 13:39:09 -04:00
|
|
|
// init the email dao
|
|
|
|
self.init(emailAddress, passphrase, token, callback);
|
2013-09-26 09:48:32 -04:00
|
|
|
});
|
2013-08-20 13:48:49 -04:00
|
|
|
}
|
|
|
|
);
|
2013-09-04 15:01:32 -04:00
|
|
|
};
|
2013-08-20 13:48:49 -04:00
|
|
|
|
2013-09-26 09:48:32 -04:00
|
|
|
/**
|
|
|
|
* Lookup the user's email address. Check local cache if available, otherwise query google's token info api to learn the user's email address
|
|
|
|
*/
|
|
|
|
self.queryEmailAddress = function(token, callback) {
|
2013-10-09 10:40:36 -04:00
|
|
|
var itemKey = 'emailaddress';
|
2013-09-26 11:17:47 -04:00
|
|
|
|
2013-10-09 10:40:36 -04:00
|
|
|
self._appConfigStore.listItems(itemKey, 0, null, function(err, cachedItems) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-09-26 11:17:47 -04:00
|
|
|
|
2013-10-09 10:40:36 -04:00
|
|
|
// do roundtrip to google api if no email address is cached yet
|
|
|
|
if (!cachedItems || cachedItems.length < 1) {
|
|
|
|
queryGoogleApi();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, cachedItems[0]);
|
2013-09-26 09:48:32 -04:00
|
|
|
});
|
2013-09-26 11:17:47 -04:00
|
|
|
|
|
|
|
function queryGoogleApi() {
|
|
|
|
// fetch gmail user's email address from the Google Authorization Server endpoint
|
|
|
|
$.ajax({
|
|
|
|
url: 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' + token,
|
|
|
|
type: 'GET',
|
|
|
|
dataType: 'json',
|
|
|
|
success: function(info) {
|
|
|
|
if (!info || !info.email) {
|
|
|
|
callback({
|
|
|
|
errMsg: 'Error looking up email address on google api!'
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cache the email address on the device
|
2013-10-09 10:40:36 -04:00
|
|
|
self._appConfigStore.storeList([info.email], itemKey, function(err) {
|
2013-09-26 11:17:47 -04:00
|
|
|
callback(err, info.email);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, err) {
|
|
|
|
callback({
|
|
|
|
errMsg: xhr.status + ': ' + xhr.statusText,
|
|
|
|
err: err
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-09-26 09:48:32 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instanciate the mail email data access object and its dependencies. Login to imap on init.
|
|
|
|
*/
|
2013-10-12 13:39:09 -04:00
|
|
|
self.init = function(userId, passphrase, token, callback) {
|
2013-08-20 13:48:49 -04:00
|
|
|
var auth, imapOptions, smtpOptions,
|
2013-10-11 21:19:01 -04:00
|
|
|
keychain, imapClient, smtpClient, pgp, userStorage;
|
2013-08-20 13:48:49 -04:00
|
|
|
|
|
|
|
// create mail credentials objects for imap/smtp
|
|
|
|
auth = {
|
|
|
|
XOAuth2: {
|
|
|
|
user: userId,
|
2013-09-15 09:13:19 -04:00
|
|
|
clientId: config.gmail.clientId,
|
2013-08-20 13:48:49 -04:00
|
|
|
accessToken: token
|
|
|
|
}
|
|
|
|
};
|
|
|
|
imapOptions = {
|
2013-09-15 09:13:19 -04:00
|
|
|
secure: config.gmail.imap.secure,
|
|
|
|
port: config.gmail.imap.port,
|
|
|
|
host: config.gmail.imap.host,
|
2013-08-20 13:48:49 -04:00
|
|
|
auth: auth
|
|
|
|
};
|
|
|
|
smtpOptions = {
|
2013-09-15 09:13:19 -04:00
|
|
|
secure: config.gmail.smtp.secure,
|
|
|
|
port: config.gmail.smtp.port,
|
|
|
|
host: config.gmail.smtp.host,
|
2013-08-20 13:48:49 -04:00
|
|
|
auth: auth
|
|
|
|
};
|
|
|
|
|
|
|
|
// init objects and inject dependencies
|
|
|
|
keychain = new KeychainDAO(cloudstorage);
|
|
|
|
imapClient = new ImapClient(imapOptions);
|
|
|
|
smtpClient = new SmtpClient(smtpOptions);
|
2013-10-11 21:19:01 -04:00
|
|
|
pgp = new PGP();
|
2013-10-09 10:40:36 -04:00
|
|
|
userStorage = new DeviceStorageDAO();
|
2013-10-11 21:19:01 -04:00
|
|
|
self._emailDao = new EmailDAO(keychain, imapClient, smtpClient, pgp, userStorage);
|
2013-08-20 13:48:49 -04:00
|
|
|
|
|
|
|
// init email dao
|
|
|
|
var account = {
|
|
|
|
emailAddress: userId,
|
2013-10-11 21:19:01 -04:00
|
|
|
asymKeySize: config.asymKeySize
|
2013-08-20 13:48:49 -04:00
|
|
|
};
|
2013-10-12 13:39:09 -04:00
|
|
|
self._emailDao.init(account, passphrase, callback);
|
2013-08-20 13:48:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
return self;
|
2013-06-10 11:57:33 -04:00
|
|
|
});
|