mail/src/js/controller/login/login-privatekey-download.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, $q, auth, email, keychain) {
!$routeParams.dev && !auth.isInitialized() && $location.path('/'); // init app
2014-10-02 16:05:44 -04:00
$scope.step = 1;
//
// Token
//
$scope.checkToken = function() {
if ($scope.tokenForm.$invalid) {
$scope.errMsg = 'Please enter a valid recovery token!';
2014-10-02 16:05:44 -04:00
return;
}
var userId = auth.emailAddress;
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined;
resolve();
}).then(function() {
// get public key id for reference
return keychain.getUserKeyPair(userId);
}).then(function(keypair) {
2014-10-02 16:05:44 -04:00
// remember for storage later
$scope.cachedKeypair = keypair;
return keychain.downloadPrivateKey({
2014-10-02 16:05:44 -04:00
userId: userId,
keyId: keypair.publicKey._id,
recoveryToken: $scope.recoveryToken.toUpperCase()
});
}).then(function(encryptedPrivateKey) {
$scope.encryptedPrivateKey = encryptedPrivateKey;
$scope.busy = false;
$scope.step++;
}).catch(displayError);
2014-10-02 16:05:44 -04:00
};
//
// Keychain code
//
$scope.checkCode = function() {
if ($scope.codeForm.$invalid) {
$scope.errMsg = 'Please fill out all required fields!';
2014-10-02 16:05:44 -04:00
return;
}
2014-10-02 16:05:44 -04:00
var options = $scope.encryptedPrivateKey;
2014-12-01 05:53:45 -05:00
options.code = $scope.code.toUpperCase();
return $q(function(resolve) {
$scope.busy = true;
$scope.errMsg = undefined;
resolve();
}).then(function() {
return keychain.decryptAndStorePrivateKeyLocally(options);
}).then(function(privateKey) {
2014-10-02 16:05:44 -04:00
// add private key to cached keypair object
$scope.cachedKeypair.privateKey = privateKey;
// try empty passphrase
return email.unlock({
2014-10-02 16:05:44 -04:00
keypair: $scope.cachedKeypair,
passphrase: undefined
}).catch(function(err) {
// passphrase incorrct ... go to passphrase login screen
$scope.goTo('/login-existing');
throw err;
});
}).then(function() {
// passphrase is corrent ...
return auth.storeCredentials();
}).then(function() {
// continue to main app
$scope.goTo('/account');
}).catch(displayError);
2014-10-02 16:05:44 -04:00
};
//
// helper functions
//
2014-10-02 16:05:44 -04:00
$scope.goTo = function(location) {
$location.path(location);
};
function displayError(err) {
$scope.busy = false;
$scope.incorrect = true;
$scope.errMsg = err.errMsg || err.message;
}
2014-10-02 16:05:44 -04:00
};
2014-10-08 06:34:34 -04:00
module.exports = LoginPrivateKeyDownloadCtrl;