2014-10-02 16:05:44 -04:00
|
|
|
'use strict';
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-19 14:54:59 -05:00
|
|
|
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;
|
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
//
|
|
|
|
// Token
|
|
|
|
//
|
2014-08-13 10:16:28 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
$scope.checkToken = function() {
|
|
|
|
if ($scope.tokenForm.$invalid) {
|
|
|
|
$scope.errMsg = 'Please enter a valid recovery token!';
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
$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-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.verifyRecoveryToken = function(callback) {
|
2014-11-19 14:54:59 -05:00
|
|
|
var userId = auth.emailAddress;
|
2014-10-02 16:05:44 -04:00
|
|
|
keychain.getUserKeyPair(userId, function(err, keypair) {
|
|
|
|
if (err) {
|
2014-11-12 10:12:26 -05:00
|
|
|
displayError(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) {
|
2014-11-12 10:12:26 -05:00
|
|
|
displayError(err);
|
2014-06-13 06:33:30 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
$scope.encryptedPrivateKey = encryptedPrivateKey;
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
//
|
|
|
|
// Keychain code
|
|
|
|
//
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
$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-06-13 06:33:30 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
$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-06-13 06:33:30 -04:00
|
|
|
|
2014-10-02 16:05:44 -04:00
|
|
|
keychain.decryptAndStorePrivateKeyLocally(options, function(err, privateKey) {
|
|
|
|
if (err) {
|
2014-11-12 10:12:26 -05:00
|
|
|
displayError(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
|
2014-11-19 14:54:59 -05:00
|
|
|
email.unlock({
|
2014-10-02 16:05:44 -04:00
|
|
|
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
|
2014-11-19 14:54:59 -05:00
|
|
|
auth.storeCredentials(function(err) {
|
2014-06-13 06:33:30 -04:00
|
|
|
if (err) {
|
2014-11-12 10:12:26 -05:00
|
|
|
displayError(err);
|
|
|
|
return;
|
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-11-12 10:12:26 -05:00
|
|
|
$scope.handlePaste = function(event) {
|
|
|
|
var evt = event;
|
|
|
|
if (evt.originalEvent) {
|
|
|
|
evt = evt.originalEvent;
|
2014-10-02 16:05:44 -04:00
|
|
|
}
|
2014-06-13 06:33:30 -04:00
|
|
|
|
2014-11-12 10:12:26 -05:00
|
|
|
var value = evt.clipboardData.getData('text/plain');
|
|
|
|
if (!value) {
|
2014-10-02 16:05:44 -04:00
|
|
|
return;
|
|
|
|
}
|
2014-11-12 10:12:26 -05: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-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);
|
|
|
|
$scope.$apply();
|
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;
|
|
|
|
$scope.$apply();
|
|
|
|
}
|
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;
|