2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, $q, auth, email, privateKey, keychain) {
|
2014-11-19 14:54:59 -05:00
|
|
|
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
|
2014-10-02 16:05:44 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
//
|
2015-02-27 14:04:44 -05:00
|
|
|
// scope functions
|
2014-11-12 10:12:26 -05:00
|
|
|
//
|
2014-08-13 10:16:28 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
$scope.checkCode = function() {
|
|
|
|
if ($scope.form.$invalid) {
|
|
|
|
$scope.errMsg = 'Please fill out all required fields!';
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
var cachedKeypair;
|
2014-12-17 07:32:12 -05:00
|
|
|
var userId = auth.emailAddress;
|
2014-11-12 10:12:26 -05:00
|
|
|
|
2014-12-17 07:32:12 -05:00
|
|
|
return $q(function(resolve) {
|
|
|
|
$scope.busy = true;
|
2014-11-12 10:12:26 -05:00
|
|
|
$scope.errMsg = undefined;
|
2014-12-17 07:32:12 -05:00
|
|
|
resolve();
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
}).then(function() {
|
|
|
|
// login to imap
|
|
|
|
return privateKey.init();
|
|
|
|
|
2014-12-17 07:32:12 -05:00
|
|
|
}).then(function() {
|
|
|
|
// get public key id for reference
|
|
|
|
return keychain.getUserKeyPair(userId);
|
2014-07-22 15:05:34 -04:00
|
|
|
|
2014-12-17 07:32:12 -05:00
|
|
|
}).then(function(keypair) {
|
2014-10-02 16:05:44 -04:00
|
|
|
// remember for storage later
|
2015-02-27 14:04:44 -05:00
|
|
|
cachedKeypair = keypair;
|
|
|
|
return privateKey.download({
|
2014-10-02 16:05:44 -04:00
|
|
|
userId: userId,
|
2015-02-27 14:04:44 -05:00
|
|
|
keyId: keypair.publicKey._id
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
2014-12-17 07:32:12 -05:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
}).then(function(encryptedKey) {
|
|
|
|
// set decryption code
|
|
|
|
encryptedKey.code = $scope.code.toUpperCase();
|
|
|
|
// decrypt the downloaded encrypted private key
|
|
|
|
return privateKey.decrypt(encryptedKey);
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
}).then(function(privkey) {
|
|
|
|
// add private key to cached keypair object
|
|
|
|
cachedKeypair.privateKey = privkey;
|
|
|
|
// store the decrypted private key locally
|
|
|
|
return keychain.putUserKeyPair(cachedKeypair);
|
2014-12-17 07:32:12 -05:00
|
|
|
|
|
|
|
}).then(function() {
|
2014-10-02 16:05:44 -04:00
|
|
|
// try empty passphrase
|
2014-12-17 07:32:12 -05:00
|
|
|
return email.unlock({
|
2015-02-27 14:04:44 -05:00
|
|
|
keypair: cachedKeypair,
|
2014-10-02 16:05:44 -04:00
|
|
|
passphrase: undefined
|
2015-02-27 14:04:44 -05:00
|
|
|
}).catch(function() {
|
2014-12-17 07:32:12 -05:00
|
|
|
// passphrase incorrct ... go to passphrase login screen
|
|
|
|
$scope.goTo('/login-existing');
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
2014-12-17 07:32:12 -05:00
|
|
|
|
|
|
|
}).then(function() {
|
|
|
|
// passphrase is corrent ...
|
|
|
|
return auth.storeCredentials();
|
|
|
|
|
2015-02-27 14:04:44 -05:00
|
|
|
}).then(function() {
|
|
|
|
// logout of imap
|
|
|
|
return privateKey.destroy();
|
|
|
|
|
2014-12-17 07:32:12 -05:00
|
|
|
}).then(function() {
|
|
|
|
// continue to main app
|
|
|
|
$scope.goTo('/account');
|
|
|
|
|
|
|
|
}).catch(displayError);
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
//
|
|
|
|
// helper functions
|
|
|
|
//
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.goTo = function(location) {
|
|
|
|
$location.path(location);
|
2014-06-13 06:33:30 -04:00
|
|
|
};
|
2014-11-12 10:12:26 -05:00
|
|
|
|
|
|
|
function displayError(err) {
|
|
|
|
$scope.busy = false;
|
|
|
|
$scope.incorrect = true;
|
|
|
|
$scope.errMsg = err.errMsg || err.message;
|
|
|
|
}
|
2014-10-02 16:05:44 -04:00
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-08 06:34:34 -04:00
|
|
|
module.exports = LoginPrivateKeyDownloadCtrl;
|