From 24f97db09e12c7652783803524d8cfe254cc9493 Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Mon, 4 Nov 2013 14:20:14 +0100 Subject: [PATCH] implement feedback on keygen and login --- src/js/app.js | 2 +- src/js/controller/login-existing.js | 50 ++++++++------------ src/js/controller/login-initial.js | 22 ++++----- src/js/controller/login-new-device.js | 67 +++++++++++---------------- src/js/controller/login.js | 11 ++++- src/js/util/download.js | 41 +++++++++++----- src/sass/all.scss | 1 + src/sass/views/_shared.scss | 3 ++ src/tpl/loading.html | 1 - src/tpl/login-initial.html | 12 ++--- src/tpl/login.html | 13 ++++++ 11 files changed, 121 insertions(+), 102 deletions(-) create mode 100644 src/sass/views/_shared.scss delete mode 100644 src/tpl/loading.html create mode 100644 src/tpl/login.html diff --git a/src/js/app.js b/src/js/app.js index edeb0ec..0408105 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -22,7 +22,7 @@ require([ // set router paths app.config(function($routeProvider) { $routeProvider.when('/login', { - templateUrl: 'tpl/loading.html', + templateUrl: 'tpl/login.html', controller: LoginCtrl }); $routeProvider.when('/login-existing', { diff --git a/src/js/controller/login-existing.js b/src/js/controller/login-existing.js index 82834f7..28190b5 100644 --- a/src/js/controller/login-existing.js +++ b/src/js/controller/login-existing.js @@ -4,49 +4,42 @@ define(function(require) { var appController = require('js/app-controller'); var LoginExistingCtrl = function($scope, $location) { + var emailDao = appController._emailDao; + $scope.buttonEnabled = true; $scope.incorrect = false; $scope.confirmPassphrase = function() { - var passphrase = $scope.passphrase, - emailDao = appController._emailDao; - - if (!passphrase) { + if (!$scope.passphrase) { return; } // disable button once loggin has started $scope.buttonEnabled = false; $scope.incorrect = false; - unlockCrypto(imapLogin); + unlockCrypto(); + }; - function unlockCrypto(callback) { - var userId = emailDao._account.emailAddress; - emailDao._keychain.getUserKeyPair(userId, function(err, keypair) { - if (err) { - callback(err); - return; - } - emailDao.unlock(keypair, passphrase, callback); - }); - } - - function imapLogin(err) { + function unlockCrypto() { + var userId = emailDao._account.emailAddress; + appController._emailDao._keychain.getUserKeyPair(userId, function(err, keypair) { if (err) { handleError(err); return; } + emailDao.unlock(keypair, $scope.passphrase, onUnlock); + }); + } - // login to imap backend - appController._emailDao.imapLogin(function(err) { - if (err) { - handleError(err); - return; - } - onLogin(); - }); + function onUnlock(err) { + if (err) { + handleError(err); + return; } - }; + + $location.path('/desktop'); + $scope.$apply(); + } function handleError(err) { $scope.incorrect = true; @@ -54,11 +47,6 @@ define(function(require) { $scope.$apply(); console.error(err); } - - function onLogin() { - $location.path('/desktop'); - $scope.$apply(); - } }; return LoginExistingCtrl; diff --git a/src/js/controller/login-initial.js b/src/js/controller/login-initial.js index c1e6899..c4f722f 100644 --- a/src/js/controller/login-initial.js +++ b/src/js/controller/login-initial.js @@ -51,26 +51,26 @@ define(function(require) { } var id = keys.keyId.substring(8, keys.keyId.length); - dl.createDownload(keys.publicKeyArmored + keys.privateKeyArmored, id + '.asc', 'text/plain'); - $scope.exported = true; + dl.createDownload({ + content: keys.publicKeyArmored + keys.privateKeyArmored, + filename: id + '.asc', + contentType: 'text/plain' + }, onSave); }); - }; - $scope.proceed = function() { - // login to imap backend - appController._emailDao.imapLogin(function(err) { + function onSave(err) { if (err) { console.error(err); return; } - onLogin(); - }); + $scope.proceed(); + $scope.$apply(); + } }; - function onLogin() { + $scope.proceed = function() { $location.path('/desktop'); - $scope.$apply(); - } + }; function setState(state, async) { $scope.state = state; diff --git a/src/js/controller/login-new-device.js b/src/js/controller/login-new-device.js index 6ff1043..b690494 100644 --- a/src/js/controller/login-new-device.js +++ b/src/js/controller/login-new-device.js @@ -5,64 +5,53 @@ define(function(require) { appController = require('js/app-controller'); var LoginExistingCtrl = function($scope, $location) { + var emailDao = appController._emailDao; + $scope.incorrect = false; $scope.confirmPassphrase = function() { - var passphrase = $scope.passphrase, - emailDao = appController._emailDao; - - if (!passphrase) { + if (!$scope.passphrase) { $scope.incorrect = true; return; } $scope.incorrect = false; - unlockCrypto(imapLogin); + unlockCrypto(); + }; - function unlockCrypto(callback) { - var userId = emailDao._account.emailAddress; - emailDao._keychain.getUserKeyPair(userId, function(err, keypair) { - if (err) { - callback(err); - return; - } - - keypair.privateKey = { - _id: keypair.publicKey._id, - userId: userId, - encryptedKey: $scope.key.privateKeyArmored - }; - emailDao.unlock(keypair, passphrase, function(err) { - if (err) { - $scope.incorrect = true; - $scope.$apply(); - callback(err); - return; - } - - emailDao._keychain.putUserKeyPair(keypair, callback); - }); - }); - } - - function imapLogin(err) { + function unlockCrypto() { + var userId = emailDao._account.emailAddress; + emailDao._keychain.getUserKeyPair(userId, function(err, keypair) { if (err) { console.error(err); return; } - // login to imap backend - appController._emailDao.imapLogin(function(err) { + keypair.privateKey = { + _id: keypair.publicKey._id, + userId: userId, + encryptedKey: $scope.key.privateKeyArmored + }; + + emailDao.unlock(keypair, $scope.passphrase, function(err) { if (err) { + $scope.incorrect = true; + $scope.$apply(); console.error(err); return; } - onLogin(); - }); - } - }; - function onLogin() { + emailDao._keychain.putUserKeyPair(keypair, onUnlock); + }); + }); + } + + function onUnlock(err) { + if (err) { + console.error(err); + return; + } + $location.path('/desktop'); $scope.$apply(); } diff --git a/src/js/controller/login.js b/src/js/controller/login.js index 9ed17da..32b46da 100644 --- a/src/js/controller/login.js +++ b/src/js/controller/login.js @@ -27,13 +27,22 @@ define(function(require) { return; } + // initiate controller by creating email dao appController.init(auth.emailAddress, auth.token, function(err, availableKeys) { if (err) { console.error(err); return; } - redirect(availableKeys); + // login to imap backend + appController._emailDao.imapLogin(function(err) { + if (err) { + console.error(err); + console.log('Error logging into IMAP... proceeding in offline mode.'); + } + + redirect(availableKeys); + }); }); }); } diff --git a/src/js/util/download.js b/src/js/util/download.js index f8ee752..5f674fa 100644 --- a/src/js/util/download.js +++ b/src/js/util/download.js @@ -3,23 +3,40 @@ define(function() { var dl = {}; - dl.createDownload = function(content, filename, contentType) { - contentType = contentType || 'application/octet-stream'; + dl.createDownload = function(options, callback) { + var contentType = options.contentType || 'application/octet-stream'; + chrome.fileSystem.chooseEntry({ type: 'saveFile', - suggestedName: filename - }, function(file) { + suggestedName: options.filename + }, onEntry); + + function onEntry(file) { if (!file) { + callback(); return; } - file.createWriter(function(writer) { - writer.onerror = console.error; - writer.onwriteend = function() {}; - writer.write(new Blob([content], { - type: contentType - })); - }, console.error); - }); + file.createWriter(onWriter, onError); + } + + function onWriter(writer) { + writer.onerror = onError; + writer.onwriteend = onEnd; + writer.write(new Blob([options.content], { + type: contentType + })); + } + + function onError(e) { + console.error(e); + callback({ + errMsg: 'Error exporting keypair to file!' + }); + } + + function onEnd() { + callback(); + } }; return dl; diff --git a/src/sass/all.scss b/src/sass/all.scss index 896f5e7..9379eae 100755 --- a/src/sass/all.scss +++ b/src/sass/all.scss @@ -19,6 +19,7 @@ @import "components/layout"; // Views +@import "views/shared"; @import "views/navigation"; @import "views/mail-list"; @import "views/read"; diff --git a/src/sass/views/_shared.scss b/src/sass/views/_shared.scss new file mode 100644 index 0000000..33463be --- /dev/null +++ b/src/sass/views/_shared.scss @@ -0,0 +1,3 @@ +.waiting-cursor { + cursor: wait; +} \ No newline at end of file diff --git a/src/tpl/loading.html b/src/tpl/loading.html deleted file mode 100644 index 821d04c..0000000 --- a/src/tpl/loading.html +++ /dev/null @@ -1 +0,0 @@ -

loading...

\ No newline at end of file diff --git a/src/tpl/login-initial.html b/src/tpl/login-initial.html index cd08eab..9c4629a 100644 --- a/src/tpl/login-initial.html +++ b/src/tpl/login-initial.html @@ -1,4 +1,4 @@ -
+