otp   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 283
Duplicated Lines 14.13 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 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
A login() 13 27 4
A can_register() 0 4 1
A register_start() 0 15 1
A register() 0 20 2
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 OTPAuthenticate\OTPAuthenticate;
14
use OTPAuthenticate\OTPHelper;
15
use phpbb\db\driver\driver_interface;
16
use phpbb\exception\http_exception;
17
use phpbb\request\request_interface;
18
use phpbb\template\template;
19
use phpbb\user;
20
21
class otp extends abstract_module
22
{
23
	/**
24
	 * @var OTPHelper
25
	 */
26
	private $otp_helper;
27
28
	/**
29
	 * @var OTPAuthenticate
30
	 */
31
	private $otp;
32
33
	/**
34
	 * @var request_interface
35
	 */
36
	private $request;
37
38
	/**
39
	 * @var string
40
	 */
41
	private $otp_registration_table;
42
43
	/**
44
	 * OTP constructor.
45
	 *
46
	 * @param driver_interface $db
47
	 * @param user $user
48
	 * @param request_interface $request
49
	 * @param template $template
50
	 * @param string                            $otp_registration_table
51
	 */
52 View Code Duplication
	public function __construct(driver_interface $db, user $user, request_interface $request, template $template, $otp_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->otp_helper = new OTPHelper();
55
		$this->otp = new OTPAuthenticate();
56
		$this->db = $db;
57
		$this->user = $user;
58
		$this->request = $request;
59
		$this->template = $template;
60
		$this->otp_registration_table = $otp_registration_table;
61
	}
62
63
	/**
64
	 * Get a language key for this specific module.
65
	 * @return string
66
	 */
67
	public function get_translatable_name()
68
	{
69
		return 'TFA_OTP';
70
	}
71
72
	/**
73
	 * Return the name of the current module
74
	 * This is for internal use only
75
	 * @return string
76
	 */
77
	public function get_name()
78
	{
79
		return 'otp';
80
	}
81
82
	/**
83
	 * Return if this module is enabled by the admin
84
	 * (And all server requirements are met).
85
	 *
86
	 * Do not return false in case a specific user disabled this module,
87
	 * OR if the user is unable to use this specific module,
88
	 * OR if a browser specific item is missing/incorrect.
89
	 * @return boolean
90
	 */
91
	public function is_enabled()
92
	{
93
		return true;
94
	}
95
96
	/**
97
	 * Check if the current user is able to use this module.
98
	 *
99
	 * This means that the user enabled it in the UCP,
100
	 * And has it setup up correctly.
101
	 * This method will be called during login, not during registration/
102
	 *
103
	 * @param int $user_id
104
	 *
105
	 * @return bool
106
	 */
107
	public function is_usable($user_id)
108
	{
109
		return $this->check_table_for_user($this->otp_registration_table, $user_id);
110
	}
111
112
	/**
113
	 * Check if the user can potentially use this.
114
	 * This method is called at registration page.
115
	 *
116
	 * You can, for example, check if the current browser is suitable.
117
	 *
118
	 * @param int|boolean $user_id Use false to ignore user
119
	 *
120
	 * @return bool
121
	 */
122
	public function is_potentially_usable($user_id = false)
123
	{
124
		return true;
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->otp_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 15;
153
	}
154
155
	/**
156
	 * Start of the login procedure.
157
	 *
158
	 * @param int $user_id
159
	 *
160
	 * @return array
161
	 */
162
	public function login_start($user_id)
163
	{
164
		return array(
165
			'S_TFA_INCLUDE_HTML'	=> '@paul999_tfa/tfa_otp_authenticate.html',
166
		);
167
	}
168
169
	/**
170
	 * Actual login procedure
171
	 *
172
	 * @param int $user_id
173
	 *
174
	 * @return bool
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 View Code Duplication
			if ($this->otp->checkTOTP($registration['secret'], $key, 'sha1'))
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...
188
			{
189
				// We found a valid key.
190
				$sql_ary = array(
191
					'last_used' => time(),
192
				);
193
				$sql = 'UPDATE ' . $this->otp_registration_table . ' 
194
					SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . ' 
195
					WHERE 
196
						registration_id = ' . (int) $registration['registration_id'];
197
				$this->db->sql_query($sql);
198
				return true;
199
			}
200
		}
201
		return false;
202
	}
203
204
	/**
205
	 * If this module can add new keys (Or other things)
206
	 *
207
	 * @return boolean
208
	 */
209
	public function can_register()
210
	{
211
		return true;
212
	}
213
214
	/**
215
	 * Start with the registration of a new security key.
216
	 * This page should return a name of a template, and
217
	 * it should assign the required variables for this template.
218
	 *
219
	 * @return string
220
	 */
221
	public function register_start()
222
	{
223
		$secret = $this->otp->generateSecret();
224
		$QR = $this->otp_helper->generateKeyURI('totp', $secret, $this->user->data['username'], generate_board_url(), 0, 'sha1');
225
		$this->template->assign_vars(array(
226
			'TFA_QR_CODE'				=> 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=' . $QR,
227
			'TFA_SECRET'				=> $secret,
228
			'L_TFA_ADD_OTP_KEY_EXPLAIN'	=> $this->user->lang('TFA_ADD_OTP_KEY_EXPLAIN', $secret),
229
			'S_HIDDEN_FIELDS_MODULE'	=> build_hidden_fields(array(
230
				'secret'	=> $secret,
231
			)),
232
		));
233
234
		return 'tfa_otp_ucp_new';
235
	}
236
237
	/**
238
	 * Do the actual registration of a new security key.
239
	 *
240
	 * @throws http_exception
241
	 */
242
	public function register()
243
	{
244
		$secret = $this->request->variable('secret', '');
245
		$otp	= $this->request->variable('register', '');
246
247
		if (!$this->otp->checkTOTP($secret, $otp, 'sha1'))
248
		{
249
			throw new http_exception(400, 'TFA_OTP_INVALID_KEY');
250
		}
251
252
		$sql_ary = array(
253
			'user_id' 		=> $this->user->data['user_id'],
254
			'secret'		=> $secret,
255
			'registered' 	=> time(),
256
			'last_used' 	=> time(),
257
		);
258
259
		$sql = 'INSERT INTO ' . $this->otp_registration_table . ' ' . $this->db->sql_build_array('INSERT', $sql_ary);
260
		$this->db->sql_query($sql);
261
	}
262
263
	/**
264
	 * This method is called to show the UCP page.
265
	 * You can assign template variables to the template, or do anything else here.
266
	 */
267
	public function show_ucp()
268
	{
269
		$this->show_ucp_complete($this->otp_registration_table);
270
	}
271
272
	/**
273
	 * Delete a specific row from the UCP.
274
	 * The data is based on the data provided in show_ucp.
275
	 *
276
	 * @param int $key
277
	 *
278
	 * @return void
279
	 */
280 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...
281
	{
282
		$sql = 'DELETE FROM ' . $this->otp_registration_table . '
283
			WHERE user_id = ' . (int) $this->user->data['user_id'] . '
284
			AND registration_id =' . (int) $key;
285
286
		$this->db->sql_query($sql);
287
	}
288
289
	/**
290
	 * Select all registration objects from the database
291
	 * @param integer $user_id
292
	 * @return array
293
	 */
294 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...
295
	{
296
		$sql = 'SELECT * FROM ' . $this->otp_registration_table . ' WHERE user_id = ' . (int) $user_id;
297
		$result = $this->db->sql_query($sql);
298
		$rows = $this->db->sql_fetchrowset($result);
299
300
		$this->db->sql_freeresult($result);
301
		return $rows;
302
	}
303
}
304