Completed
Push — master ( 3d31b2...c9ad65 )
by Sander
8s
created

js/app/controllers/vault.js   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 164

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 164
rs 8.2857
cc 1
nc 2
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.controller(ꞌVaultCtrlꞌ) 0 152 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
	/**
27
	 * @ngdoc function
28
	 * @name passmanApp.controller:MainCtrl
29
	 * @description
30
	 * # MainCtrl
31
	 * Controller of the passmanApp
32
	 */
33
	angular.module('passmanApp')
34
		.controller('VaultCtrl', ['$scope', 'VaultService', 'SettingsService', 'CredentialService', '$location', 'ShareService', 'EncryptService', '$translate', '$rootScope', function ($scope, VaultService, SettingsService, CredentialService, $location, ShareService, EncryptService, $translate, $rootScope) {
35
			VaultService.getVaults().then(function (vaults) {
36
				$scope.vaults = vaults;
37
				if (SettingsService.getSetting('defaultVault') != null) {
0 ignored issues
show
Coding Style introduced by
It is recommended to use !== to compare with null.

Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.

Read more about comparison operations.

Loading history...
38
					var default_vault = SettingsService.getSetting('defaultVault');
39
40
					/**
41
					 * Using a native for loop for preformance reasons.
42
					 * More info see http://stackoverflow.com/questions/13843972/angular-js-break-foreach
43
					 */
44
					for (var i = 0; i < vaults.length; i++) {
45
						var vault = vaults[i];
46
						if (vault.guid === default_vault.guid) {
47
							$scope.default_vault = true;
48
							$scope.list_selected_vault = vault;
49
							SettingsService.setSetting('defaultVault', vault);
50
							if (SettingsService.getSetting('defaultVaultPass')) {
51
								$location.path('/vault/' + vault.guid);
52
							}
53
							break;
54
						}
55
					}
56
				}
57
			});
58
59
60
			var key_strengths = [
61
				'password.poor',
62
				'password.poor',
63
				'password.weak',
64
				'password.good',
65
				'password.strong'
66
			];
67
68
			$scope.default_vault = false;
69
			$scope.remember_vault_password = false;
70
			$scope.list_selected_vault = false;
71
			$scope.minimal_value_key_strength = 3;
72
73
			$rootScope.$on('settings_loaded', function () {
74
				$scope.minimal_value_key_strength = SettingsService.getSetting('vault_key_strength');
75
				$translate(key_strengths[SettingsService.getSetting('vault_key_strength')]).then(function(translation){
76
					$scope.required_score = {'strength': translation};
77
				});
78
79
			});
80
81
			$scope.toggleDefaultVault = function () {
82
				$scope.default_vault = !$scope.default_vault;
83
				if ($scope.default_vault === true) {
84
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
85
				} else {
86
					SettingsService.setSetting('defaultVault', null);
87
				}
88
			};
89
90
			$scope.toggleRememberPassword = function () {
91
				$scope.remember_vault_password = !$scope.remember_vault_password;
92
				if ($scope.remember_vault_password) {
93
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
94
					$scope.default_vault = true;
95
				}
96
				if ($scope.remember_vault_password !== true) {
97
					SettingsService.setSetting('defaultVault', null);
98
				}
99
			};
100
101
			$scope.clearState = function () {
102
				$scope.list_selected_vault = false;
103
				$scope.creating_vault = false;
104
				$scope.error = false;
105
			};
106
107
			$scope.selectVault = function (vault) {
108
				$scope.list_selected_vault = vault;
109
			};
110
			$scope.sharing_keys = {};
111
			$scope.newVault = function () {
112
				$scope.creating_vault = true;
113
				var key_size = 1024;
114
				ShareService.generateRSAKeys(key_size).progress(function (progress) {
115
					var p = progress > 0 ? 2 : 1;
116
					var msg = $translate.instant('generating.sharing.keys');
117
					msg = msg.replace('%step', p);
118
					$scope.creating_keys = msg;
119
					$scope.$digest();
120
				}).then(function (kp) {
121
					var pem = ShareService.rsaKeyPairToPEM(kp);
122
					$scope.creating_keys = false;
123
					$scope.sharing_keys.private_sharing_key = pem.privateKey;
124
					$scope.sharing_keys.public_sharing_key = pem.publicKey;
125
					$scope.$digest();
126
				});
127
128
			};
129
130
			var _loginToVault = function (vault, vault_key) {
131
				var _vault = angular.copy(vault);
132
				_vault.vaultKey = angular.copy(vault_key);
133
				delete _vault.credentials;
134
				VaultService.setActiveVault(_vault);
135
				$location.path('/vault/' + vault.guid);
136
			};
137
138
			$scope.vaultDecryptionKey = '';
139
			$scope.loginToVault = function (vault, vault_key) {
140
				$scope.error = false;
141
				var _vault = angular.copy(vault);
142
				_vault.vaultKey = angular.copy(vault_key);
143
144
				VaultService.setActiveVault(_vault);
145
				try {
146
					EncryptService.decryptString(vault.challenge_password);
147
					if ($scope.remember_vault_password) {
148
						SettingsService.setSetting('defaultVaultPass', vault_key);
149
					}
150
					_loginToVault(vault, vault_key);
151
152
				} catch (e) {
153
					$scope.error = $translate.instant('invalid.vault.key');
154
				}
155
156
			};
157
158
159
			$scope.createVault = function (vault_name, vault_key, vault_key2) {
160
				if (vault_key !== vault_key2) {
161
					$scope.error = $translate.instant('password.do.not.match');
162
					return;
163
				}
164
				VaultService.createVault(vault_name).then(function (vault) {
165
					$scope.vaults.push(vault);
166
					var _vault = angular.copy(vault);
167
					_vault.vaultKey = angular.copy(vault_key);
168
					VaultService.setActiveVault(_vault);
169
					SettingsService.setSetting('defaultVaultPass', null);
170
					SettingsService.setSetting('defaultVault', null);
171
					var test_credential = CredentialService.newCredential();
172
					test_credential.label = 'Test key for vault ' + vault_name;
173
					test_credential.hidden = true;
174
					test_credential.vault_id = vault.vault_id;
175
					test_credential.password = 'lorum ipsum';
176
					CredentialService.createCredential(test_credential).then(function () {
177
						_vault.public_sharing_key = angular.copy($scope.sharing_keys.public_sharing_key);
178
						_vault.private_sharing_key = EncryptService.encryptString(angular.copy($scope.sharing_keys.private_sharing_key));
179
						VaultService.updateSharingKeys(_vault).then(function () {
180
							_loginToVault(vault, vault_key);
181
						});
182
					});
183
				});
184
			};
185
		}]);
186
}());