1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-04 08:15:03 -05:00
mail/src/js/controller/login/login-privatekey-download.js

148 lines
4.0 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams, 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;
}
$scope.busy = true;
$scope.errMsg = undefined;
$scope.verifyRecoveryToken(function() {
$scope.busy = false;
$scope.errMsg = undefined;
$scope.step++;
$scope.$apply();
});
2014-10-02 16:05:44 -04:00
};
2014-10-02 16:05:44 -04:00
$scope.verifyRecoveryToken = function(callback) {
var userId = auth.emailAddress;
2014-10-02 16:05:44 -04:00
keychain.getUserKeyPair(userId, function(err, keypair) {
if (err) {
displayError(err);
return;
}
2014-10-02 16:05:44 -04:00
// remember for storage later
$scope.cachedKeypair = keypair;
2014-10-02 16:05:44 -04:00
keychain.downloadPrivateKey({
userId: userId,
keyId: keypair.publicKey._id,
recoveryToken: $scope.recoveryToken.toUpperCase()
}, function(err, encryptedPrivateKey) {
if (err) {
displayError(err);
return;
}
2014-10-02 16:05:44 -04:00
$scope.encryptedPrivateKey = encryptedPrivateKey;
callback();
});
});
};
//
// 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;
}
$scope.busy = true;
$scope.errMsg = undefined;
$scope.decryptAndStorePrivateKeyLocally();
};
$scope.decryptAndStorePrivateKeyLocally = function() {
var inputCode = '' + $scope.code0 + $scope.code1 + $scope.code2 + $scope.code3 + $scope.code4 + $scope.code5;
2014-10-02 16:05:44 -04:00
var options = $scope.encryptedPrivateKey;
options.code = inputCode.toUpperCase();
2014-10-02 16:05:44 -04:00
keychain.decryptAndStorePrivateKeyLocally(options, function(err, privateKey) {
if (err) {
displayError(err);
return;
}
2014-10-02 16:05:44 -04:00
// add private key to cached keypair object
$scope.cachedKeypair.privateKey = privateKey;
2014-10-02 16:05:44 -04:00
// try empty passphrase
email.unlock({
2014-10-02 16:05:44 -04:00
keypair: $scope.cachedKeypair,
passphrase: undefined
}, function(err) {
if (err) {
2014-10-02 16:05:44 -04:00
// go to passphrase login screen
$scope.goTo('/login-existing');
return;
}
2014-10-02 16:05:44 -04:00
// passphrase is corrent ... go to main app
auth.storeCredentials(function(err) {
if (err) {
displayError(err);
return;
}
2014-10-02 16:05:44 -04:00
$scope.goTo('/desktop');
});
});
2014-10-02 16:05:44 -04:00
});
};
$scope.handlePaste = function(event) {
var evt = event;
if (evt.originalEvent) {
evt = evt.originalEvent;
2014-10-02 16:05:44 -04:00
}
var value = evt.clipboardData.getData('text/plain');
if (!value) {
2014-10-02 16:05:44 -04:00
return;
}
value = value.replace(/-/g, '');
$scope.code0 = value.slice(0, 4);
$scope.code1 = value.slice(4, 8);
$scope.code2 = value.slice(8, 12);
$scope.code3 = value.slice(12, 16);
$scope.code4 = value.slice(16, 20);
$scope.code5 = value.slice(20, 24);
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);
$scope.$apply();
};
function displayError(err) {
$scope.busy = false;
$scope.incorrect = true;
$scope.errMsg = err.errMsg || err.message;
$scope.$apply();
}
2014-10-02 16:05:44 -04:00
};
2014-10-08 06:34:34 -04:00
module.exports = LoginPrivateKeyDownloadCtrl;