Completed
Branch v2.1.0 (2d458f)
by Sander
06:16
created

js/app/controllers/vault.js   B

Complexity

Total Complexity 42
Complexity/F 1.83

Size

Lines of Code 225
Function Count 23

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 4
dl 0
loc 225
rs 8.295
wmc 42
mnd 4
bc 43
fnc 23
bpm 1.8695
cpm 1.826
noi 1

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.controller(ꞌVaultCtrlꞌ) 0 212 2

How to fix   Complexity   

Complexity

Complex classes like js/app/controllers/vault.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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', '$interval',
35
			function ($scope, VaultService, SettingsService, CredentialService, $location, ShareService, EncryptService, $translate, $rootScope, $interval) {
36
			VaultService.getVaults().then(function (vaults) {
37
				$scope.vaults = vaults;
38
				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...
39
					var default_vault = SettingsService.getSetting('defaultVault');
40
41
					/**
42
					 * Using a native for loop for preformance reasons.
43
					 * More info see http://stackoverflow.com/questions/13843972/angular-js-break-foreach
44
					 */
45
					for (var i = 0; i < vaults.length; i++) {
46
						var vault = vaults[i];
47
						if (vault.guid === default_vault.guid) {
48
							$scope.default_vault = true;
49
							$scope.list_selected_vault = vault;
50
							SettingsService.setSetting('defaultVault', vault);
51
							if (SettingsService.getSetting('defaultVaultPass')) {
52
								$location.path('/vault/' + vault.guid);
53
							}
54
							$scope.vault_tries[vault.guid] = {
55
								tries: 0,
56
								timeout: 0
57
							};
58
							break;
59
						}
60
					}
61
				}
62
			});
63
64
65
			var key_strengths = [
66
				'password.poor',
67
				'password.poor',
68
				'password.weak',
69
				'password.good',
70
				'password.strong'
71
			];
72
73
			$scope.default_vault = false;
74
			$scope.remember_vault_password = false;
75
			$scope.auto_logout_timer = false;
76
			$scope.logout_timer = '0';
77
			$scope.list_selected_vault = false;
78
			$scope.minimal_value_key_strength = 3;
79
80
			var settingsLoaded = function () {
81
				$scope.minimal_value_key_strength = SettingsService.getSetting('vault_key_strength');
82
				$translate(key_strengths[SettingsService.getSetting('vault_key_strength')]).then(function (translation) {
83
					$scope.required_score = {'strength': translation};
84
				});
85
			};
86
87
			if (!SettingsService.getSetting('settings_loaded')) {
88
				$rootScope.$on('settings_loaded', function () {
89
					settingsLoaded();
90
				});
91
			} else {
92
				settingsLoaded();
93
			}
94
95
			$scope.toggleDefaultVault = function () {
96
				$scope.default_vault = !$scope.default_vault;
97
				if ($scope.default_vault === true) {
98
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
99
				} else {
100
					SettingsService.setSetting('defaultVault', null);
101
				}
102
			};
103
104
			$scope.toggleRememberPassword = function () {
105
				$scope.remember_vault_password = !$scope.remember_vault_password;
106
				if ($scope.remember_vault_password) {
107
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
108
					$scope.default_vault = true;
109
				}
110
				if ($scope.remember_vault_password !== true) {
111
					SettingsService.setSetting('defaultVault', null);
112
				}
113
			};
114
115
			$scope.toggleAutoLogout = function () {
116
				$scope.auto_logout_timer = !$scope.auto_logout_timer;
117
			};
118
119
			$scope.clearState = function () {
120
				$scope.list_selected_vault = false;
121
				$scope.creating_vault = false;
122
				$scope.error = false;
123
			};
124
125
			$scope.selectVault = function (vault) {
126
				$scope.list_selected_vault = vault;
127
				if(!$scope.vault_tries[vault.guid]) {
128
					$scope.vault_tries[vault.guid] = {
129
						tries: 0,
130
						timeout: 0
131
					};
132
				}
133
			};
134
			$scope.sharing_keys = {};
135
			$scope.newVault = function () {
136
				$scope.creating_vault = true;
137
				var key_size = 1024;
138
				ShareService.generateRSAKeys(key_size).progress(function (progress) {
139
					var p = progress > 0 ? 2 : 1;
140
					var msg = $translate.instant('generating.sharing.keys');
141
					msg = msg.replace('%step', p);
142
					$scope.creating_keys = msg;
143
					$scope.$digest();
144
				}).then(function (kp) {
145
					var pem = ShareService.rsaKeyPairToPEM(kp);
146
					$scope.creating_keys = false;
147
					$scope.sharing_keys.private_sharing_key = pem.privateKey;
148
					$scope.sharing_keys.public_sharing_key = pem.publicKey;
149
					$scope.$digest();
150
				});
151
152
			};
153
154
			var _loginToVault = function (vault, vault_key) {
155
				var _vault = angular.copy(vault);
156
				_vault.vaultKey = angular.copy(vault_key);
157
				delete _vault.credentials;
158
				var timer = parseInt($scope.logout_timer);
159
				if ($scope.auto_logout_timer && timer > 0) {
160
					$rootScope.$broadcast('logout_timer_set', timer * 60);
161
				}
162
163
				VaultService.setActiveVault(_vault);
164
				$location.path('/vault/' + vault.guid);
165
			};
166
167
			$scope.selectLogoutTimer = function (time) {
168
				$scope.auto_logout_timer = true;
169
				$scope.logout_timer = time;
170
			};
171
172
			var tickLockTimer = function (guid) {
173
				$scope.vault_tries[guid].timeout = $scope.vault_tries[guid].timeout - 1;
174
				if($scope.vault_tries[guid].timeout === 0){
175
					$interval.cancel($scope.vault_tries[guid].timer);
176
				}
177
			};
178
179
			$scope.vault_tries = {};
180
181
			$scope.vaultDecryptionKey = '';
182
			$scope.loginToVault = function (vault, vault_key) {
183
				$scope.error = false;
184
				var _vault = angular.copy(vault);
185
				_vault.vaultKey = angular.copy(vault_key);
186
187
				VaultService.setActiveVault(_vault);
188
				try {
189
					EncryptService.decryptString(vault.challenge_password);
190
					if ($scope.remember_vault_password) {
191
						SettingsService.setSetting('defaultVaultPass', vault_key);
192
					}
193
					_loginToVault(vault, vault_key);
194
195
				} catch (e) {
196
					$scope.error = $translate.instant('invalid.vault.key');
197
198
					$scope.vault_tries[vault.guid].tries = $scope.vault_tries[vault.guid].tries + 1;
199
200
					if($scope.vault_tries[vault.guid].tries >= 3){
201
						var to = $scope.vault_tries[vault.guid].tries * $scope.vault_tries[vault.guid].tries * 2;
202
						if(to < 30){
203
							to = 30;
204
						}
205
						$scope.vault_tries[vault.guid].timeout = to;
206
207
						if($scope.vault_tries[vault.guid].hasOwnProperty('timer')){
208
							$interval.cancel($scope.vault_tries[vault.guid].timer);
209
						}
210
211
						$scope.vault_tries[vault.guid].timer = $interval(function () {
212
							tickLockTimer(vault.guid);
213
						} ,1000);
214
					}
215
216
				}
217
			};
218
219
220
			$scope.createVault = function (vault_name, vault_key, vault_key2) {
221
				if (vault_key !== vault_key2) {
222
					$scope.error = $translate.instant('password.do.not.match');
223
					return;
224
				}
225
				VaultService.createVault(vault_name).then(function (vault) {
226
					$scope.vaults.push(vault);
227
					var _vault = angular.copy(vault);
228
					_vault.vaultKey = angular.copy(vault_key);
229
					VaultService.setActiveVault(_vault);
230
					SettingsService.setSetting('defaultVaultPass', null);
231
					SettingsService.setSetting('defaultVault', null);
232
					var test_credential = CredentialService.newCredential();
233
					test_credential.label = 'Test key for vault ' + vault_name;
234
					test_credential.hidden = true;
235
					test_credential.vault_id = vault.vault_id;
236
					test_credential.password = 'lorum ipsum';
237
					CredentialService.createCredential(test_credential).then(function () {
238
						_vault.public_sharing_key = angular.copy($scope.sharing_keys.public_sharing_key);
239
						_vault.private_sharing_key = EncryptService.encryptString(angular.copy($scope.sharing_keys.private_sharing_key));
240
						VaultService.updateSharingKeys(_vault).then(function () {
241
							_loginToVault(vault, vault_key);
242
						});
243
					});
244
				});
245
			};
246
		}]);
247
}());