1
0
mirror of https://github.com/moparisthebest/mail synced 2024-08-13 16:43:47 -04:00

login to imap and smtp via chrome identity api works

This commit is contained in:
Tankred Hase 2013-08-16 20:31:18 +02:00
parent 6623379a2c
commit 2186d20a7c
10 changed files with 119 additions and 56 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
node_modules node_modules
.DS_Store .DS_Store
*-browserified.js

View File

@ -10,11 +10,13 @@
"start": "grunt prod" "start": "grunt prod"
}, },
"dependencies": { "dependencies": {
"grunt": "~0.4.1", "crypto-lib": "https://github.com/whiteout-io/crypto-lib/tarball/master",
"grunt-contrib-connect": "~0.3.0", "imap-client": "git+ssh://git@github.com:whiteout-io/imap-client.git#master",
"crypto-lib": "https://github.com/whiteout-io/crypto-lib/tarball/master" "smtp-client": "git+ssh://git@github.com:whiteout-io/smtp-client.git#master"
}, },
"devDependencies": { "devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-connect": "~0.3.0",
"grunt-contrib-jshint": "~0.5.3", "grunt-contrib-jshint": "~0.5.3",
"grunt-contrib-qunit": "~0.2.1" "grunt-contrib-qunit": "~0.2.1"
} }

View File

@ -6,5 +6,19 @@ echo "--> copying dependencies to src\n"
cd `dirname $0` cd `dirname $0`
cd .. cd ..
# copy crypto lib
cp ./node_modules/crypto-lib/src/*.js ./src/js/crypto/ cp ./node_modules/crypto-lib/src/*.js ./src/js/crypto/
cp ./node_modules/crypto-lib/node_modules/node-forge/js/*.js ./src/lib/ cp ./node_modules/crypto-lib/node_modules/node-forge/js/*.js ./src/lib/
# build imap/smtp modules and copy
cd ./node_modules/imap-client/
npm install && node build.js
cp ./src-gen/*.js ../../src/lib/
cd ../../
cd ./node_modules/smtp-client/
npm install && node build.js
cp ./src-gen/*.js ../../src/lib/
cd ../../
echo "\n--> finished copying dependencies.\n"

View File

@ -1,20 +1,11 @@
require(['jquery', 'js/app-controller', 'js/app-config'], function($, controller, app) { require(['jquery', 'js/app-controller', 'js/app-config'], function($, controller, app) {
'use strict'; 'use strict';
chrome.identity.getAuthToken({
'interactive': true
}, function(token) {
console.log(token);
// Use the token.
});
/** /**
* Load templates and start the application * Load templates and start the application
*/ */
$(document).ready(function() { $(document).ready(function() {
controller.init(function() { controller.start(startApp);
controller.start(startApp);
});
}); });
function startApp() { function startApp() {

View File

@ -1,23 +1,13 @@
/** /**
* The main application controller * The main application controller
*/ */
define(['jquery', 'js/dao/email-dao', 'js/dao/keychain-dao', 'js/dao/cloudstorage-dao', define(['jquery', 'ImapClient', 'SmtpClient', 'js/dao/email-dao', 'js/dao/keychain-dao',
'js/app-config', 'cordova' 'js/dao/cloudstorage-dao', 'js/app-config', 'cordova'
], function($, EmailDAO, KeychainDAO, cloudstorage, app) { ], function($, ImapClient, SmtpClient, EmailDAO, KeychainDAO, cloudstorage, app) {
'use strict'; 'use strict';
var self = {}; var self = {},
emailDao;
var emailDao;
/**
* Initializes modules through dependecy injection
*/
self.init = function(callback) {
var keychain = new KeychainDAO(cloudstorage);
emailDao = new EmailDAO(cloudstorage, keychain);
callback();
};
/** /**
* Start the application by loading the view templates * Start the application by loading the view templates
@ -25,7 +15,7 @@ define(['jquery', 'js/dao/email-dao', 'js/dao/keychain-dao', 'js/dao/cloudstorag
self.start = function(callback) { self.start = function(callback) {
// the views to load // the views to load
var views = ['login', 'compose', 'folderlist', 'messagelist', var views = ['login', 'compose', 'folderlist', 'messagelist',
'messagelistitem', 'read' 'messagelistitem', 'read'
]; ];
// are we running in native app or in browser? // are we running in native app or in browser?
@ -49,7 +39,7 @@ define(['jquery', 'js/dao/email-dao', 'js/dao/keychain-dao', 'js/dao/cloudstorag
self.execute = function(cmd, args, callback) { self.execute = function(cmd, args, callback) {
if (cmd === 'login') { if (cmd === 'login') {
// login user // login user
login(args.userId, args.password, function(err) { fetchOAuthToken(args.userId, args.password, function(err) {
callback({ callback({
err: err err: err
}); });
@ -102,8 +92,52 @@ define(['jquery', 'js/dao/email-dao', 'js/dao/keychain-dao', 'js/dao/cloudstorag
// Helper methods // Helper methods
// //
function login(userId, password, callback) { function fetchOAuthToken(userId, password, callback) {
// get OAuth Token from chrome
chrome.identity.getAuthToken({
'interactive': true
},
function(token) {
login(userId, password, token, callback);
}
);
}
function login(userId, password, token, callback) {
var auth, imapOptions, smtpOptions,
keychain, imapClient, smtpClient;
// create mail credentials objects for imap/smtp
auth = {
XOAuth2: {
user: userId,
clientId: '440907777130.apps.googleusercontent.com',
accessToken: token
}
};
imapOptions = {
secure: true,
port: 993,
host: 'imap.gmail.com',
auth: auth
};
smtpOptions = {
secure: true,
port: 465,
host: 'smtp.gmail.com',
auth: auth
};
// init objects and inject dependencies
keychain = new KeychainDAO(cloudstorage);
imapClient = new ImapClient(imapOptions);
smtpClient = new SmtpClient(smtpOptions);
emailDao = new EmailDAO(cloudstorage, keychain, imapClient, smtpClient);
// init email dao
var account = new app.model.Account({ var account = new app.model.Account({
imapOptions: imapOptions,
smtpOptions: smtpOptions,
emailAddress: userId, emailAddress: userId,
symKeySize: app.config.symKeySize, symKeySize: app.config.symKeySize,
symIvSize: app.config.symIvSize, symIvSize: app.config.symIvSize,

View File

@ -3,11 +3,11 @@
* between the cloud service and the device's local storage * between the cloud service and the device's local storage
*/ */
define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-dao', define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-dao',
'js/dao/devicestorage-dao', 'js/app-config', 'js/model/account-model' 'js/dao/devicestorage-dao', 'js/app-config', 'js/model/account-model'
], function(_, util, crypto, jsonDB, devicestorage, app) { ], function(_, util, crypto, jsonDB, devicestorage, app) {
'use strict'; 'use strict';
var EmailDAO = function(cloudstorage, keychain) { var EmailDAO = function(cloudstorage, keychain, imapClient, smtpClient) {
var self = this; var self = this;
/** /**
@ -25,18 +25,30 @@ define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-da
return; return;
} }
// init user's local database // login IMAP client if existent
jsonDB.init(emailAddress); if (imapClient) {
imapClient.login(function() {
console.log('logged into imap.');
initKeychain();
});
} else {
initKeychain();
}
// call getUserKeyPair to read/sync keypair with devicestorage/cloud function initKeychain() {
keychain.getUserKeyPair(emailAddress, function(err, storedKeypair) { // init user's local database
if (err) { jsonDB.init(emailAddress);
callback(err);
return; // call getUserKeyPair to read/sync keypair with devicestorage/cloud
} keychain.getUserKeyPair(emailAddress, function(err, storedKeypair) {
// init crypto if (err) {
initCrypto(storedKeypair); callback(err);
}); return;
}
// init crypto
initCrypto(storedKeypair);
});
}
function initCrypto(storedKeypair) { function initCrypto(storedKeypair) {
crypto.init({ crypto.init({
@ -300,10 +312,15 @@ define(['underscore', 'cryptoLib/util', 'js/crypto/crypto', 'js/dao/lawnchair-da
} }
function send(email) { function send(email) {
// send email to cloud service if (smtpClient) {
cloudstorage.deliverEmail(email, userId, recipient, function(err) { // send email directly client side
callback(err); smtpClient.send(email, callback);
}); } else {
// send email via cloud service
cloudstorage.deliverEmail(email, userId, recipient, function(err) {
callback(err);
});
}
} }
}; };
}; };

View File

@ -23,7 +23,7 @@ define(['jquery', 'underscore', 'backbone', 'js/app-config'], function($, _, Bac
login: function() { login: function() {
var page = $(this.el), var page = $(this.el),
userId = page.find('#userId').val() + '@mail.whiteout.io', userId = page.find('#userId').val(),
password = page.find('#password').val(); password = page.find('#password').val();
// show loading msg during init // show loading msg during init

View File

@ -8,14 +8,16 @@
"128": "css/images/mail-128.png" "128": "css/images/mail-128.png"
}, },
"permissions": [ "permissions": [
"https://storage.whiteout.io/", "https://storage.whiteout.io/",
"identity" "identity", {
"socket": ["tcp-connect"]
}
], ],
"key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBXqC3/oX5fP/gLORcVN62Pf3Ph+pO4qEB+FynSMWMoqWUt7FDoaKuHrsP/KInuP/0PUZcqpij9kB9MytLTqYzGIoRsUd37i1Dt6R69fnNsIqAISgoWIRg4VyRdon9cTIniv3DVV45PPyNCvN+oQoBMv9NbojWnlL9W05bKYkABQIDAQAB", "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBXqC3/oX5fP/gLORcVN62Pf3Ph+pO4qEB+FynSMWMoqWUt7FDoaKuHrsP/KInuP/0PUZcqpij9kB9MytLTqYzGIoRsUd37i1Dt6R69fnNsIqAISgoWIRg4VyRdon9cTIniv3DVV45PPyNCvN+oQoBMv9NbojWnlL9W05bKYkABQIDAQAB",
"oauth2": { "oauth2": {
"client_id": "440907777130.apps.googleusercontent.com", "client_id": "440907777130.apps.googleusercontent.com",
"scopes": [ "scopes": [
"https://mail.google.com/" "https://mail.google.com/"
] ]
}, },
"app": { "app": {

View File

@ -14,7 +14,9 @@
lawnchair: 'lawnchair/lawnchair-git', lawnchair: 'lawnchair/lawnchair-git',
lawnchairSQL: 'lawnchair/lawnchair-adapter-webkit-sqlite-git', lawnchairSQL: 'lawnchair/lawnchair-adapter-webkit-sqlite-git',
lawnchairIDB: 'lawnchair/lawnchair-adapter-indexed-db-git', lawnchairIDB: 'lawnchair/lawnchair-adapter-indexed-db-git',
cordova: 'cordova-2.5.0' cordova: 'cordova-2.5.0',
ImapClient: 'imap-client-browserified',
SmtpClient: 'smtp-client-browserified'
}, },
shim: { shim: {
lawnchair: { lawnchair: {

View File

@ -7,9 +7,9 @@
<div id="loginForm"> <div id="loginForm">
<h3>Please sign in</h3> <h3>Please sign in</h3>
<label for="userId" class="ui-hidden-accessible">Username:</label> <label for="userId" class="ui-hidden-accessible">Username:</label>
<input type="email" name="user" id="userId" value="" placeholder="user" data-theme="a"> <input type="email" name="user" id="userId" value="safewithme.testuser@gmail.com" placeholder="user" data-theme="a">
<label for="password" class="ui-hidden-accessible">Password:</label> <label for="password" class="ui-hidden-accessible">Password:</label>
<input type="password" name="pass" id="password" value="" placeholder="password" data-theme="a"> <input type="password" name="pass" id="password" value="passphrase" placeholder="password" data-theme="a">
<input type="button" data-theme="b" data-icon="check" id="loginBtn" value="Sign in"> <input type="button" data-theme="b" data-icon="check" id="loginBtn" value="Sign in">
</div> </div>
</div><!-- /content --> </div><!-- /content -->