Completed
Push — master ( 7cd105...68bc07 )
by Paul
03:36 queued 01:16
created

backup_key::login_start()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
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
14
use phpbb\db\driver\driver_interface;
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 \phpbb\request\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 \phpbb\passwords\manager
39
	 */
40
	private $password_manager;
41
42
	/**
43
	 * backup_key constructor.
44
	 *
45
	 * @param \phpbb\db\driver\driver_interface $db
46
	 * @param \phpbb\user                       $user
47
	 * @param \phpbb\request\request_interface  $request
48
	 * @param \phpbb\template\template          $template
49
	 * @param \phpbb\passwords\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
	 * Get the priority for this module.
128
	 * A lower priority means more chance it gets selected as default option
129
	 *
130
	 * There can be only one module with a specific priority!
131
	 * If there is already a module registered with this priority,
132
	 * a Exception might be thrown
133
	 *
134
	 * @return int
135
	 */
136
	public function get_priority()
137
	{
138
		return 1337; // We want the backup keys as priority as low as possible, because they are a backup.
139
	}
140
141
	/**
142
	 * Start of the login procedure.
143
	 *
144
	 * @param int $user_id
145
	 *
146
	 * @return array with data to be assign to the template.
147
	 */
148
	public function login_start($user_id)
149
	{
150
		// TODO: Implement login_start() method.
151
	}
152
153
	/**
154
	 * Actual login procedure
155
	 *
156
	 * @param int $user_id
157
	 *
158
	 * @return boolean
159
	 */
160
	public function login($user_id)
161
	{
162
		// TODO: Implement login() method.
163
	}
164
165
	/**
166
	 * If this module can add new keys (Or other things)
167
	 *
168
	 * @return boolean
169
	 */
170
	public function can_register()
171
	{
172
		return !$this->check_table_for_user($this->backup_registration_table, $this->user->data['user_id'], ' AND valid = 1');
173
	}
174
175
	/**
176
	 * Start with the registration of a new security key.
177
	 * This page should return a name of a template, and
178
	 * it should assign the required variables for this template.
179
	 *
180
	 * @return string
181
	 */
182
	public function register_start()
183
	{
184
		$sql = [];
185
186
		for ($i = 0; $i <= self::NUMBER_OF_KEYS; $i++)
187
		{
188
			$key = bin2hex(random_bytes(8));
189
			$sql[] = array(
190
				'user_id' 		=> $this->user->data['user_id'],
191
				'valid'			=> true,
192
				'secret'		=> $this->password_manager->hash($key),
193
				'registered' 	=> time(),
194
			);
195
			$this->template->assign_block_vars('backup', [
196
				'KEY'	=> $key,
197
			]);
198
		}
199
		$this->db->sql_multi_insert($this->backup_registration_table, $sql);
200
201
		return 'tfa_backup_ucp_new';
202
	}
203
204
	/**
205
	 * Do the actual registration of a new security key.
206
	 *
207
	 * @return boolean Result of the registration.
208
	 * @throws BadRequestHttpException
209
	 */
210
	public function register()
211
	{
212
		// We don't need to do anything here.
213
		return true;
214
	}
215
216
	/**
217
	 * This method is called to show the UCP page.
218
	 * You can assign template variables to the template, or do anything else here.
219
	 */
220
	public function show_ucp()
221
	{
222
		$this->show_ucp_complete($this->backup_registration_table);
223
	}
224
225
	/**
226
	 * Delete a specific row from the UCP.
227
	 * The data is based on the data provided in show_ucp.
228
	 *
229
	 * @param int $key
230
	 *
231
	 * @return void
232
	 */
233 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...
234
	{
235
		$sql = 'DELETE FROM ' . $this->backup_registration_table . '
236
					WHERE user_id = ' . (int) $this->user->data['user_id'] . '
237
					AND registration_id =' . (int) $key;
238
239
		$this->db->sql_query($sql);
240
	}
241
}