backup_key   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 283
Duplicated Lines 14.13 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 1
dl 40
loc 283
rs 10
c 0
b 0
f 0

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 9 9 1
A get_translatable_name() 0 4 1
A get_name() 0 4 1
A is_enabled() 0 4 1
A is_usable() 0 4 1
A is_potentially_usable() 0 4 1
A key_registered() 0 4 1
A get_priority() 0 4 1
A login_start() 0 6 1
B login() 14 32 6
A can_register() 0 4 1
A register_start() 0 23 2
A register() 0 5 1
A show_ucp() 0 4 1
A delete() 8 8 1
A getRegistrations() 9 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 *
4
 * 2FA extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015 Paul Sohier
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace paul999\tfa\modules;
12
13
use phpbb\db\driver\driver_interface;
14
use phpbb\exception\http_exception;
15
use phpbb\passwords\manager;
16
use phpbb\request\request_interface;
17
use phpbb\template\template;
18
use phpbb\user;
19
20
class backup_key extends abstract_module
21
{
22
	/**
23
	 * @var request_interface
24
	 */
25
	private $request;
26
27
	/**
28
	 * @var string
29
	 */
30
	private $backup_registration_table;
31
32
	/**
33
	 * Number of keys that is generated
34
	 */
35
	const NUMBER_OF_KEYS = 6;
36
37
	/**
38
	 * @var manager
39
	 */
40
	private $password_manager;
41
42
	/**
43
	 * backup_key constructor.
44
	 *
45
	 * @param driver_interface $db
46
	 * @param user $user
47
	 * @param request_interface $request
48
	 * @param template $template
49
	 * @param manager $password_manager
50
	 * @param string $backup_registration_table
51
	 */
52 View Code Duplication
	public function __construct(driver_interface $db, user $user, request_interface $request, template $template, manager $password_manager, $backup_registration_table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
	{
54
		$this->db = $db;
55
		$this->user = $user;
56
		$this->request = $request;
57
		$this->template = $template;
58
		$this->backup_registration_table = $backup_registration_table;
59
		$this->password_manager = $password_manager;
60
	}
61
62
	/**
63
	 * Get a language key for this specific module.
64
	 * @return string
65
	 */
66
	public function get_translatable_name()
67
	{
68
		return 'TFA_BACKUP_KEY';
69
	}
70
71
	/**
72
	 * Return the name of the current module
73
	 * This is for internal use only
74
	 * @return string
75
	 */
76
	public function get_name()
77
	{
78
		return 'backup_key';
79
	}
80
81
	/**
82
	 * Return if this module is enabled by the admin
83
	 * (And all server requirements are met).
84
	 *
85
	 * Do not return false in case a specific user disabled this module,
86
	 * OR if the user is unable to use this specific module,
87
	 * OR if a browser specific item is missing/incorrect.
88
	 * @return boolean
89
	 */
90
	public function is_enabled()
91
	{
92
		return true;
93
	}
94
95
	/**
96
	 * Check if the current user is able to use this module.
97
	 *
98
	 * This means that the user enabled it in the UCP,
99
	 * And has it setup up correctly.
100
	 * This method will be called during login, not during registration/
101
	 *
102
	 * @param int $user_id
103
	 *
104
	 * @return bool
105
	 */
106
	public function is_usable($user_id)
107
	{
108
		return $this->check_table_for_user($this->backup_registration_table, $user_id, ' AND valid = 1');
109
	}
110
111
	/**
112
	 * Check if the user can potentially use this.
113
	 * This method is called at registration page.
114
	 *
115
	 * You can, for example, check if the current browser is suitable.
116
	 *
117
	 * @param int|boolean $user_id Use false to ignore user
118
	 *
119
	 * @return bool
120
	 */
121
	public function is_potentially_usable($user_id = false)
122
	{
123
		return true;
124
	}
125
126
127
	/**
128
	 * Check if the user has any key registered with this module.
129
	 * There should be no check done if the key is usable, it should
130
	 * only return if a key is registered.
131
	 *
132
	 * @param $user_id
133
	 * @return bool
134
	 */
135
	public function key_registered($user_id)
136
	{
137
		return $this->check_table_for_user($this->backup_registration_table, $user_id);
138
	}
139
140
	/**
141
	 * Get the priority for this module.
142
	 * A lower priority means more chance it gets selected as default option
143
	 *
144
	 * There can be only one module with a specific priority!
145
	 * If there is already a module registered with this priority,
146
	 * a Exception might be thrown
147
	 *
148
	 * @return int
149
	 */
150
	public function get_priority()
151
	{
152
		return 1337; // We want the backup keys as priority as low as possible, because they are a backup.
153
	}
154
155
	/**
156
	 * Start of the login procedure.
157
	 *
158
	 * @param int $user_id
159
	 *
160
	 * @return array with data to be assign to the template.
161
	 */
162
	public function login_start($user_id)
163
	{
164
		return array(
165
			'S_TFA_INCLUDE_HTML'	=> '@paul999_tfa/tfa_backup_authenticate.html',
166
		);
167
	}
168
169
	/**
170
	 * Actual login procedure
171
	 *
172
	 * @param int $user_id
173
	 *
174
	 * @return boolean
175
	 */
176
	public function login($user_id)
177
	{
178
		$key = $this->request->variable('authenticate', '');
179
180
		if (empty($key))
181
		{
182
			throw new http_exception(400, 'TFA_NO_KEY_PROVIDED');
183
		}
184
185
		foreach ($this->getRegistrations($user_id) as $registration)
186
		{
187
			if (!$registration['valid'] || $registration['last_used'])
188
			{
189
				continue;
190
			}
191 View Code Duplication
			if ($this->password_manager->check($key, $registration['secret']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
192
			{
193
				// We found a valid key.
194
				$sql_ary = array(
195
					'last_used' => time(),
196
					'valid'		=> false,
197
				);
198
				$sql = 'UPDATE ' . $this->backup_registration_table . ' 
199
					SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . ' 
200
					WHERE 
201
						registration_id = ' . (int) $registration['registration_id'];
202
				$this->db->sql_query($sql);
203
				return true;
204
			}
205
		}
206
		return false;
207
	}
208
209
	/**
210
	 * If this module can add new keys (Or other things)
211
	 *
212
	 * @return boolean
213
	 */
214
	public function can_register()
215
	{
216
		return !$this->check_table_for_user($this->backup_registration_table, $this->user->data['user_id'], ' AND valid = 1');
217
	}
218
219
	/**
220
	 * Start with the registration of a new security key.
221
	 * This page should return a name of a template, and
222
	 * it should assign the required variables for this template.
223
	 *
224
	 * @return string
225
	 * @throws \Exception
226
	 */
227
	public function register_start()
228
	{
229
		$sql = array();
230
231
		for ($i = 0; $i < self::NUMBER_OF_KEYS; $i++)
232
		{
233
			$time = time();
234
			$key = bin2hex(random_bytes(16));
235
			$sql[] = array(
236
				'user_id' 		=> $this->user->data['user_id'],
237
				'valid'			=> true,
238
				'secret'		=> $this->password_manager->hash($key),
239
				'registered' 	=> $time,
240
			);
241
			$this->template->assign_block_vars('backup', [
242
				'KEY'	=> $key,
243
				'DATE'	=> $this->user->format_date($time),
244
			]);
245
		}
246
		$this->db->sql_multi_insert($this->backup_registration_table, $sql);
247
248
		return 'tfa_backup_ucp_new';
249
	}
250
251
	/**
252
	 * Do the actual registration of a new security key.
253
	 *
254
	 * @return boolean Result of the registration.
255
	 */
256
	public function register()
257
	{
258
		// We don't need to do anything here.
259
		return true;
260
	}
261
262
	/**
263
	 * This method is called to show the UCP page.
264
	 * You can assign template variables to the template, or do anything else here.
265
	 */
266
	public function show_ucp()
267
	{
268
		$this->show_ucp_complete($this->backup_registration_table);
269
	}
270
271
	/**
272
	 * Delete a specific row from the UCP.
273
	 * The data is based on the data provided in show_ucp.
274
	 *
275
	 * @param int $key
276
	 *
277
	 * @return void
278
	 */
279 View Code Duplication
	public function delete($key)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
280
	{
281
		$sql = 'DELETE FROM ' . $this->backup_registration_table . '
282
			WHERE user_id = ' . (int) $this->user->data['user_id'] . '
283
			AND registration_id =' . (int) $key;
284
285
		$this->db->sql_query($sql);
286
	}
287
288
	/**
289
	 * Select all registration objects from the database
290
	 * @param integer $user_id
291
	 * @return array
292
	 */
293 View Code Duplication
	private function getRegistrations($user_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
294
	{
295
		$sql = 'SELECT * FROM ' . $this->backup_registration_table . ' WHERE user_id = ' . (int) $user_id;
296
		$result = $this->db->sql_query($sql);
297
		$rows = $this->db->sql_fetchrowset($result);
298
299
		$this->db->sql_freeresult($result);
300
		return $rows;
301
	}
302
}
303