Completed
Push — master ( fc4810...b3f176 )
by Paul
04:33
created

OTP::show_ucp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 0
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 OTPAuthenticate\OTPAuthenticate;
15
use OTPAuthenticate\OTPHelper;
16
use phpbb\db\driver\driver_interface;
17
use phpbb\request\request_interface;
18
use phpbb\template\template;
19
use phpbb\user;
20
21
class OTP implements module_interface
22
{
23
	/**
24
	 * @var \OTPAuthenticate\OTPHelper
25
	 */
26
	private $otp_helper;
27
28
	/**
29
	 * @var \OTPAuthenticate\OTPAuthenticate
30
	 */
31
	private $otp;
32
	/**
33
	 * @var \phpbb\db\driver\driver_interface
34
	 */
35
	private $db;
36
	/**
37
	 * @var \phpbb\user
38
	 */
39
	private $user;
40
	/**
41
	 * @var \phpbb\request\request_interface
42
	 */
43
	private $request;
44
	/**
45
	 * @var \phpbb\template\template
46
	 */
47
	private $template;
48
49
	/**
50
	 * OTP constructor.
51
	 *
52
	 * @param \phpbb\db\driver\driver_interface $db
53
	 * @param \phpbb\user                       $user
54
	 * @param \phpbb\request\request_interface  $request
55
	 * @param \phpbb\template\template          $template
56
	 */
57
	public function __construct(driver_interface $db, user $user, request_interface $request, template $template)
58
	{
59
		$this->otp_helper = new OTPHelper();
60
		$this->otp = new OTPAuthenticate();
61
		$this->db = $db;
62
		$this->user = $user;
63
		$this->request = $request;
64
		$this->template = $template;
65
	}
66
67
	/**
68
	 * Get a language key for this specific module.
69
	 * @return string
70
	 */
71
	public function get_translatable_name()
72
	{
73
		return 'OTP';
74
	}
75
76
	/**
77
	 * Return the name of the current module
78
	 * This is for internal use only
79
	 * @return string
80
	 */
81
	public function get_name()
82
	{
83
		return 'OTP';
84
	}
85
86
	/**
87
	 * Return if this module is enabled by the admin
88
	 * (And all server requirements are met).
89
	 *
90
	 * Do not return false in case a specific user disabled this module,
91
	 * OR if the user is unable to use this specific module,
92
	 * OR if a browser specific item is missing/incorrect.
93
	 * @return boolean
94
	 */
95
	public function is_enabled()
96
	{
97
		return true;
98
	}
99
100
	/**
101
	 * Check if the current user is able to use this module.
102
	 *
103
	 * This means that the user enabled it in the UCP,
104
	 * And has it setup up correctly.
105
	 * This method will be called during login, not during registration/
106
	 *
107
	 * @param int $user_id
108
	 *
109
	 * @return bool
110
	 */
111
	public function is_usable($user_id)
112
	{
113
		return true;
114
	}
115
116
	/**
117
	 * Check if the user can potentially use this.
118
	 * This method is called at registration page.
119
	 *
120
	 * You can, for example, check if the current browser is suitable.
121
	 *
122
	 * @param int|boolean $user_id Use false to ignore user
123
	 *
124
	 * @return bool
125
	 */
126
	public function is_potentially_usable($user_id = false)
127
	{
128
		return true;
129
	}
130
131
	/**
132
	 * Get the priority for this module.
133
	 * A lower priority means more chance it gets selected as default option
134
	 *
135
	 * There can be only one module with a specific priority!
136
	 * If there is already a module registered with this priority,
137
	 * a Exception might be thrown
138
	 *
139
	 * @return int
140
	 */
141
	public function get_priority()
142
	{
143
		return 15;
144
	}
145
146
	/**
147
	 * Start of the login procedure.
148
	 *
149
	 * @param int $user_id
150
	 *
151
	 * @return int
152
	 */
153
	public function login_start($user_id)
154
	{
155
		// TODO: Implement login_start() method.
156
	}
157
158
	/**
159
	 * Actual login procedure
160
	 *
161
	 * @param int $user_id
162
	 */
163
	public function login($user_id)
164
	{
165
		// TODO: Implement login() method.
166
	}
167
168
	/**
169
	 * If this module can add new keys (Or other things)
170
	 *
171
	 * @return boolean
172
	 */
173
	public function can_register()
174
	{
175
		return true;
176
	}
177
178
	/**
179
	 * Start with the registration of a new security key.
180
	 * This page should return a name of a template, and
181
	 * it should assign the required variables for this template.
182
	 *
183
	 * @return string
184
	 */
185
	public function register_start()
186
	{
187
		$secret = $this->otp->generateSecret();
188
		$QR = $this->otp_helper->generateKeyURI('sha1', $secret, $this->user->data['user_id']);
189
		$this->template->assign_vars(array(
190
			'TFA_QR_CODE'				=> 'https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=' . $QR,
191
			'TFA_SECRET'				=> $secret,
192
			'TFA_ADD_OTP_KEY_EXPLAIN'	=> $this->user->lang('TFA_ADD_OTP_KEY_EXPLAIN', $secret),
193
			'S_HIDDEN_FIELDS'			=> build_hidden_fields(array(
194
				'secret'	=> $secret,
195
			)),
196
		));
197
198
		return 'tfa_otp_ucp_new.html';
199
	}
200
201
	/**
202
	 * Do the actual registration of a new security key.
203
	 *
204
	 * @return boolean Result of the registration.
205
	 * @throws BadRequestHttpException
206
	 */
207
	public function register()
208
	{
209
		// TODO: Implement register() method.
210
	}
211
212
	/**
213
	 * This method is called to show the UCP page.
214
	 * You can assign template variables to the template, or do anything else here.
215
	 */
216
	public function show_ucp()
217
	{
218
		// TODO: Implement show_ucp() method.
219
	}
220
221
	/**
222
	 * Delete a specific row from the UCP.
223
	 * The data is based on the data provided in show_ucp.
224
	 *
225
	 * @param int $key
226
	 *
227
	 * @return void
228
	 */
229
	public function delete($key)
230
	{
231
		// TODO: Implement delete() method.
232
}}