passman/js/app/controllers/vault.js

143 lines
4.6 KiB
JavaScript
Raw Normal View History

2016-09-11 05:30:17 +08:00
'use strict';
/**
* @ngdoc function
* @name passmanApp.controller:MainCtrl
* @description
* # MainCtrl
* Controller of the passmanApp
*/
angular.module('passmanApp')
2016-09-26 04:29:27 +08:00
.controller('VaultCtrl', ['$scope', 'VaultService', 'SettingsService', 'CredentialService', '$location', 'ShareService', 'EncryptService', function ($scope, VaultService, SettingsService, CredentialService, $location, ShareService, EncryptService) {
2016-09-11 05:30:17 +08:00
VaultService.getVaults().then(function (vaults) {
$scope.vaults = vaults;
2016-09-26 04:29:27 +08:00
if (SettingsService.getSetting('defaultVault') != null) {
var default_vault = SettingsService.getSetting('defaultVault');
2016-09-12 01:45:20 +08:00
/**
* Using a native for loop for preformance reasons.
* More info see http://stackoverflow.com/questions/13843972/angular-js-break-foreach
*/
2016-09-26 04:29:27 +08:00
for (var i = 0; i < vaults.length; i++) {
2016-09-12 01:45:20 +08:00
var vault = vaults[i];
2016-09-26 04:29:27 +08:00
if (vault.guid == default_vault.guid) {
$scope.default_vault = true;
$scope.list_selected_vault = vault;
SettingsService.setSetting('defaultVault', vault);
2016-09-28 03:23:43 +08:00
if (SettingsService.getSetting('defaultVaultPass')) {
2016-09-26 04:29:27 +08:00
$location.path('/vault/' + vault.vault_id);
2016-09-12 04:57:55 +08:00
}
2016-09-12 01:45:20 +08:00
break;
}
2016-09-12 01:45:20 +08:00
}
}
2016-09-11 05:30:17 +08:00
});
$scope.default_vault = false;
$scope.remember_vault_password = false;
$scope.list_selected_vault = false;
2016-09-26 04:29:27 +08:00
$scope.toggleDefaultVault = function () {
$scope.default_vault = !$scope.default_vault;
2016-09-26 04:29:27 +08:00
if ($scope.default_vault == true) {
SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
} else {
SettingsService.setSetting('defaultVault', null);
}
};
2016-09-26 04:29:27 +08:00
$scope.toggleRememberPassword = function () {
2016-09-12 02:47:29 +08:00
$scope.remember_vault_password = !$scope.remember_vault_password;
2016-09-26 04:29:27 +08:00
if ($scope.remember_vault_password) {
SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
$scope.default_vault = true;
}
2016-09-26 04:29:27 +08:00
if ($scope.remember_vault_password != true) {
2016-09-12 02:47:29 +08:00
SettingsService.setSetting('defaultVault', null);
}
};
2016-09-11 05:30:17 +08:00
$scope.clearState = function () {
$scope.list_selected_vault = false;
$scope.creating_vault = false;
2016-09-12 05:40:06 +08:00
$scope.error = false;
2016-09-11 05:30:17 +08:00
};
2016-09-26 04:29:27 +08:00
$scope.selectVault = function (vault) {
2016-09-11 05:30:17 +08:00
$scope.list_selected_vault = vault;
};
$scope.sharing_keys = {};
2016-09-26 04:29:27 +08:00
$scope.newVault = function () {
2016-09-11 05:30:17 +08:00
$scope.creating_vault = true;
var _vault = {};
2016-09-26 04:29:27 +08:00
var key_size = 1024;
2016-09-27 02:49:30 +08:00
ShareService.generateRSAKeys(key_size).progress(function (progress) {
2016-09-26 04:29:27 +08:00
var p = progress > 0 ? 2 : 1;
$scope.creating_keys = 'Generating sharing keys (' + p + ' / 2)';
2016-09-27 02:56:41 +08:00
$scope.$apply();
2016-09-27 02:49:30 +08:00
}).then(function (kp) {
2016-09-26 04:29:27 +08:00
var pem = ShareService.rsaKeyPairToPEM(kp);
$scope.creating_keys = false;
$scope.sharing_keys.private_sharing_key = pem.privateKey;
$scope.sharing_keys.public_sharing_key = pem.publicKey;
2016-09-27 02:56:41 +08:00
$scope.$apply();
2016-09-26 04:29:27 +08:00
});
};
2016-09-12 02:47:29 +08:00
var _loginToVault = function (vault, vault_key) {
2016-09-26 04:29:27 +08:00
var _vault = angular.copy(vault);
2016-09-12 02:47:29 +08:00
_vault.vaultKey = angular.copy(vault_key);
VaultService.setActiveVault(_vault);
2016-09-26 04:29:27 +08:00
$location.path('/vault/' + vault.vault_id);
};
2016-09-12 01:45:20 +08:00
$scope.vaultDecryptionKey = '';
2016-09-12 02:47:29 +08:00
$scope.loginToVault = function (vault, vault_key) {
$scope.error = false;
2016-09-26 04:29:27 +08:00
var _vault = angular.copy(vault);
2016-09-12 02:47:29 +08:00
_vault.vaultKey = angular.copy(vault_key);
VaultService.setActiveVault(_vault);
2016-09-26 04:29:27 +08:00
VaultService.getVault(vault).then(function (credentials) {
var credential = credentials[0];
try {
var c = CredentialService.decryptCredential(credential);
if ($scope.remember_vault_password) {
SettingsService.setSetting('defaultVaultPass', vault_key);
2016-09-12 01:45:20 +08:00
}
_loginToVault(vault, vault_key);
} catch (e) {
$scope.error = 'Incorrect vault password!'
2016-09-12 01:45:20 +08:00
}
})
};
2016-09-12 01:45:20 +08:00
2016-09-26 04:29:27 +08:00
$scope.createVault = function (vault_name, vault_key, vault_key2) {
if (vault_key != vault_key2) {
2016-09-12 05:40:06 +08:00
$scope.error = 'Passwords do not match';
2016-09-12 01:45:20 +08:00
return;
}
VaultService.createVault(vault_name).then(function (vault) {
2016-09-12 02:47:29 +08:00
$scope.vaults.push(vault);
var _vault = angular.copy(vault);
_vault.vaultKey = angular.copy(vault_key);
2016-09-12 01:45:20 +08:00
VaultService.setActiveVault(_vault);
var test_credential = CredentialService.newCredential();
2016-09-26 04:29:27 +08:00
test_credential.label = 'Test key for vault ' + vault_name;
2016-09-12 01:45:20 +08:00
test_credential.hidden = true;
test_credential.vault_id = vault.vault_id;
test_credential.password = 'lorum ipsum';
CredentialService.createCredential(test_credential).then(function (result) {
2016-09-26 04:29:27 +08:00
_vault.public_sharing_key = angular.copy($scope.sharing_keys.public_sharing_key);
_vault.private_sharing_key = EncryptService.encryptString(angular.copy($scope.sharing_keys.private_sharing_key));
VaultService.updateSharingKeys(_vault).then(function (result) {
_loginToVault(vault, vault_key);
})
2016-09-12 01:45:20 +08:00
})
});
};
2016-09-11 05:30:17 +08:00
}]);