2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var appController = require('../app-controller');
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var LoginPrivateKeyDownloadCtrl = function($scope, $location, $routeParams) {
|
|
|
|
if (!appController._emailDao && !$routeParams.dev) {
|
|
|
|
$location.path('/'); // init app
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var keychain = appController._keychain,
|
|
|
|
emailDao = appController._emailDao,
|
|
|
|
userId = emailDao._account.emailAddress;
|
|
|
|
|
|
|
|
$scope.step = 1;
|
|
|
|
|
|
|
|
$scope.handlePaste = function(event) {
|
|
|
|
var evt = event;
|
|
|
|
if (evt.originalEvent) {
|
|
|
|
evt = evt.originalEvent;
|
2014-08-13 10:16:28 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var value = evt.clipboardData.getData('text/plain');
|
|
|
|
if (!value) {
|
|
|
|
return;
|
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
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-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.verifyRecoveryToken = function(callback) {
|
|
|
|
if (!$scope.recoveryToken) {
|
|
|
|
$scope.onError(new Error('Please set the recovery token!'));
|
|
|
|
return;
|
|
|
|
}
|
2014-07-22 15:05:34 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
keychain.getUserKeyPair(userId, function(err, keypair) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
2014-07-22 15:05:34 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// remember for storage later
|
|
|
|
$scope.cachedKeypair = keypair;
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
keychain.downloadPrivateKey({
|
|
|
|
userId: userId,
|
|
|
|
keyId: keypair.publicKey._id,
|
|
|
|
recoveryToken: $scope.recoveryToken.toUpperCase()
|
|
|
|
}, function(err, encryptedPrivateKey) {
|
2014-06-13 06:33:30 -04:00
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.encryptedPrivateKey = encryptedPrivateKey;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.decryptAndStorePrivateKeyLocally = function() {
|
|
|
|
var inputCode = '' + $scope.code0 + $scope.code1 + $scope.code2 + $scope.code3 + $scope.code4 + $scope.code5;
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if (!inputCode) {
|
|
|
|
$scope.onError(new Error('Please enter the keychain code!'));
|
|
|
|
return;
|
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
var options = $scope.encryptedPrivateKey;
|
|
|
|
options.code = inputCode.toUpperCase();
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
keychain.decryptAndStorePrivateKeyLocally(options, function(err, privateKey) {
|
|
|
|
if (err) {
|
|
|
|
$scope.onError(err);
|
2014-06-13 06:33:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// add private key to cached keypair object
|
|
|
|
$scope.cachedKeypair.privateKey = privateKey;
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// try empty passphrase
|
|
|
|
emailDao.unlock({
|
|
|
|
keypair: $scope.cachedKeypair,
|
|
|
|
passphrase: undefined
|
|
|
|
}, function(err) {
|
2014-06-13 06:33:30 -04:00
|
|
|
if (err) {
|
2014-10-02 16:05:44 -04:00
|
|
|
// go to passphrase login screen
|
|
|
|
$scope.goTo('/login-existing');
|
2014-06-13 06:33:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
// passphrase is corrent ... go to main app
|
|
|
|
appController._auth.storeCredentials(function(err) {
|
2014-06-13 06:33:30 -04:00
|
|
|
if (err) {
|
2014-10-02 16:05:44 -04:00
|
|
|
return $scope.onError(err);
|
2014-06-13 06:33:30 -04:00
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.goTo('/desktop');
|
2014-06-13 06:33:30 -04:00
|
|
|
});
|
|
|
|
});
|
2014-10-02 16:05:44 -04:00
|
|
|
});
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.goForward = function() {
|
|
|
|
if ($scope.step === 1) {
|
|
|
|
$scope.verifyRecoveryToken(function() {
|
|
|
|
$scope.step++;
|
|
|
|
$scope.$apply();
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
if ($scope.step === 2) {
|
|
|
|
$scope.decryptAndStorePrivateKeyLocally();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.goTo = function(location) {
|
|
|
|
$location.path(location);
|
|
|
|
$scope.$apply();
|
2014-06-13 06:33:30 -04:00
|
|
|
};
|
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;
|