-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpecialLatch.php
427 lines (389 loc) · 15.2 KB
/
SpecialLatch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<?php
# Including Latch SDK
require_once("SDK/Latch.php");
require_once("SDK/LatchResponse.php");
require_once("SDK/Error.php");
class SpecialLatch extends SpecialPage {
function __construct() { parent::__construct( 'LatchConf', 'editinterface' ); } # Adding editinterface allows only admins to see Latch in Special pages.
################################################################################################################################################
######################################################### DATABASE FUNCTIONS #################################################################
################################################################################################################################################
/* This function will be called when an update is made (mandatory for the installation of this plugin).
It will create the tables that we are going to use in our plugin. */
function fnMyHook( DatabaseUpdater $updater ) {
if ( is_null( $updater ) ) {
throw new MWException( "Latch extension requires Mediawiki 1.18 or above" );
}
$type = $updater->getDB()->getType();
if ( $type == "mysql" ) {
$dir = __DIR__ ;
$updater->addExtensionTable('latch_conf', $dir . '/create_table_latchConf.sql');
$updater->addExtensionTable('latch', $dir . '/create_table_latch.sql');
}
return true;
}
# Function to get user_id and account_id from DB. Notice that there will be only one row for each user.
static function accDB_useraccid ($user, &$user_id, &$acc_id, &$otp = null, &$att = 0) {
$dbr = wfGetDB( DB_SLAVE );
$dbr->begin();
$res=$dbr->select('latch',
array( 'user_id', 'acc_id', 'otp', 'attempts' ),
array ('user_id' => $user));
foreach( $res as $row ) {
$user_id = $row->user_id;
$acc_id = $row->acc_id;
$otp = $row->otp;
$att = $row->attempts;
}
}
# Function to insert user_id and account_id in DB.
static function insDB_useraccid ($user, $accountId) {
$dbw = wfGetDB( DB_MASTER );
$dbw->begin();
$dbw->insert('latch', array( 'user_id' => $user->getId() , 'acc_id' => $accountId));
$dbw->commit();
}
# Function to delete user_id and account_id of the DB.
static function delDB_useraccid ($user) {
$dbw = wfGetDB( DB_MASTER );
$dbw->begin();
$dbw->delete('latch', array( 'user_id' => $user->getId()));
$dbw->commit();
}
# Function to update user_id and account_id in DB.
static function updDB_useraccid ($user, $accountId, $otp = "", $attempts = 0) {
$dbw = wfGetDB( DB_MASTER );
$dbw->begin();
$dbw->update('latch', #Table
array( 'user_id' => $user , 'acc_id' => $accountId, 'otp' => $otp, 'attempts' => $attempts), # Set
array( 'user_id' => $user, 'acc_id' => $accountId) # Where
);
$dbw->commit();
}
# Function to get app_id and secret from DB. Notice that there will be only one row on that table.
static function accDB_appsecret (&$app_id, &$secret) {
$dbr = wfGetDB( DB_SLAVE );
$dbr->begin();
$res=$dbr->select('latch_conf',
array( 'app_id', 'secret' ));
foreach( $res as $row ) {
$app_id = $row->app_id;
$secret = $row->secret;
}
}
# Function to insert app_id and secret in DB.
function insDB_appsecret ($par_appid, $par_secret) {
$dbw = wfGetDB( DB_MASTER );
$dbw->begin();
$dbw->insert('latch_conf', array( 'app_id' => $par_appid , 'secret' => $par_secret));
$dbw->commit();
}
# Function to update app_id and secret in DB.
function updDB_appsecret ($app_id, $secret, $par_appid, $par_secret) {
$dbw = wfGetDB( DB_MASTER );
$dbw->begin();
$dbw->update('latch_conf', #Table
array( 'app_id' => $par_appid , 'secret' => $par_secret), # Set
array( 'app_id' => $app_id, 'secret' => $secret) # Where
);
$dbw->commit();
}
################################################################################################################################################
######################################################### INTERFACE FUNCTIONS ################################################################
################################################################################################################################################
# Function to print a form in order to show the app_id/secret or ask for them
function draw_cnfig ($app_id, $secret, $wgRequest, $msg) {
global $wgOut, $wgUser;
$form = Xml::openElement(
'form', array( 'method' => 'post',
'action' => $this->getTitle()->getLocalUrl( 'action=submit' ) ) );
$form .= Xml::inputLabel( wfMsg( 'latch-app_id' ) , 'txt_appid', 'txt_appid', 20, $app_id ) . '<BR><BR> ';
$form .= Xml::inputLabel( wfMsg( 'latch-secret' ) , 'txt_secret', 'txt_secret', 40, $secret ) . '<BR><BR>';
$form .= Xml::submitButton( wfMsg( 'latch-enter' ),
array( 'name' => 'clickBotEnviar' ) );
# Adding protection against CSRF
$form .= Html::hidden('token', $this->getUser()->getEditToken(), array( 'id' => 'token' ));
$form .= Xml::closeElement( 'form' );
$wgOut->addHTML($form);
# Print a confirmation box
if (!empty($msg))
$wgOut->addWikiText($msg);
}
# Function used to print the modified user preferences
static function drawUserPreferences ($acc_id, $user, &$preferences) {
# If the user doesn't have already an account_id we ask for it
if (empty($acc_id)) {
# A textBox to ask for the Latch token
$preferences['latchTok'] = array(
'type' => 'text',
'label' => wfMsg('latch-token'),
'section' => 'personal/info',
'name' => 'latchTok',
'maxlength' => 6,
'size' => 5
);
# A submit button to insert the Latch token
$preferences['latchTokBot'] = array(
'type' => 'submit',
'default' => wfMsg('latchpair'),
'section' => 'personal/info',
'name' => 'latchTokBot',
'maxlength' => 6,
'size' => 5
);
}
# If the user has an account_id we only show him the option to unpair
else {
$preferences['latchUnpair'] = array(
'type' => 'submit',
'label' => wfMsg('latchunpair').':',
'default' => wfMsg('latchunpair'),
'section' => 'personal/info',
'name' => 'latchUnpair',
'maxlength' => 6,
'size' => 5
);
}
# Additional field for CSRF protection
$preferences['hiddToken'] = array(
'type' => 'hidden',
'default' => $user->getEditToken(),
'section' => 'personal/info',
'name' => 'hiddToken',
'maxlength' => 0,
'size' => 0
);
}
# Function to set the user in the session and redirect to the main page
static function putUserInSession() {
global $wgUser, $wgRequest, $wgOut;
$wgRequest->setSessionData( 'wsUserName', $wgUser->whoIs($wgUser->getId()) );
$fullURL = $wgRequest->getRequestURL();
$urlMainPage = explode("?", $fullURL);
$wgOut->redirect($urlMainPage[0]);
}
################################################################################################################################################
######################################################## MAIN HOOK FUNCTIONS #################################################################
################################################################################################################################################
# Main function to configure Latch (only for admins)
function execute( $par ) {
# Limit access to non-admin users via url
if (!$this->userCanExecute($this->getUser())) {
$this->displayRestrictionError();
return;
}
global $wgRequest, $wgUser;
$app_id = "";
$secret = "";
$msg = "";
# If app_id and secret are already in the DB, we take them
$this->accDB_appsecret($app_id, $secret);
# Get request data
if ( $wgRequest->getCheck( 'clickBotEnviar') ) {
# CSRF protection
if (!$wgUser->matchEditToken($wgRequest->getVal('token'))) {
return;
}
else {
$par_appid = $wgRequest->getText( 'txt_appid' );
$par_secret = $wgRequest->getText( 'txt_secret' );
# App_id or secret can't be null or have extrange characters
if ((empty($par_appid)) || (preg_match('/\.([^\.]*$)/', $par_appid))) {
$msg = wfMsg('latch-error-appid');
}
else if ((empty($par_secret)) || (preg_match('/\.([^\.]*$)/', $par_secret))) {
$msg = wfMsg('latch-error-secret');
}
else {
# We create a new Latch object from the Latch SDK
$api = new Latch($par_appid, $par_secret);
# We try to make a call to the status function with the new values
$statusResponse = $api->status("");
$responseError = $statusResponse->getError();
# If a 102 code is returned, then the app_id or the secret are not correct
if ($responseError->getCode() == 102) {
$msg = wfMsg( 'latch-error-appIdSecret');
$this->draw_cnfig($app_id, $secret, $wgRequest, $msg);
return;
}
# If app_id/secret weren't in the DB and we insert them
if (empty($app_id) and empty($secret)) {
$this->insDB_appsecret($par_appid,$par_secret);
}
# Otherwise we update those values
else {
$this->updDB_appsecret($app_id, $secret, $par_appid,$par_secret);
}
# A message of the successful changes is printed
$msg = wfMsg( 'latch-parameter1-entered');
}
}
$this->draw_cnfig($par_appid, $par_secret, $wgRequest, $msg);
}
# If the user didn't press any button, we print the form anyway
else {
$this->draw_cnfig($app_id, $secret, $wgRequest, $msg);
}
}
# Function to include Latch in user's preferences page
static function wfPrefHook($user, &$preferences) {
global $wgUser, $wgRequest, $wgOut;
$user_id = "";
$acc_id = "";
$app_id = "";
$secret = "";
$error_msg = "";
$pairResponse=null;
# If app_id, secret, user_id and the account_id are already in the DB, we take them
SpecialLatch::accDB_appsecret ($app_id, $secret);
SpecialLatch::accDB_useraccid ($wgUser->getId(), $user_id, $acc_id);
# We create a new Latch object from the Latch SDK
$api = new Latch($app_id, $secret);
# We print the Latch preferences
SpecialLatch::drawUserPreferences($acc_id, $wgUser, $preferences);
# If the Pair button is pressed, we try to pair the account
if ( $wgRequest->getCheck( 'latchTokBot') ) {
# CSRF protection
if (!$wgUser->matchEditToken($wgRequest->getVal('hiddToken'))) {
return;
}
else {
$pair_token = $wgRequest->getText('latchTok');
# Not empty or extrange characters
if ((empty($pair_token)) || (preg_match('/\.([^\.]*$)/', $pair_token))) {
throw new DBExpectedError( null, wfMsg('latch-error-pair'));
}
else {
$pairResponse = $api->pair($pair_token);
$responseData = $pairResponse->getData();
if (!empty($responseData)) {
$accountId = $responseData->{"accountId"};
}
# If everything is OK, we insert the data in the DB
if (!empty($accountId)) {
SpecialLatch::insDB_useraccid($wgUser, $accountId);
}
# If Account ID is empty and no error fields are found, there are problems with the connection to the server
elseif ($pairResponse->getError() == NULL) {
throw new DBExpectedError( null, wfMsg('default-error-pair'));
}
# Controlled errors
else {
switch ($pairResponse->getError()->getCode()) {
case 205:
$error_msg = wfMsg('205-pair');
break;
case 206:
$error_msg = wfMsg('206-pair');
break;
case 401:
$error_msg = wfMsg('error-401');
break;
default:
$error_msg = wfMsg('default-error-pair');
break;
}
throw new DBExpectedError( null, $pairResponse->getError()->getCode() . " - " . $error_msg);
}
}
}
}
# If the Unpair button is pressed, we try to unpair the account
if ( $wgRequest->getCheck('latchUnpair')) {
SpecialLatch::accDB_useraccid ($wgUser->getId(), $user_id, $acc_id);
# CSRF protection
if (!$wgUser->matchEditToken($wgRequest->getVal('hiddToken'))) {
return;
}
else {
$pairResponse = $api->unpair($acc_id);
# If Account ID is empty and no error fields are found, there are problems with the connection to the server
if ($pairResponse->getError() == NULL) {
SpecialLatch::delDB_useraccid($wgUser);
}
# Controlled errors
else {
switch ($pairResponse->getError()->getCode()) {
case 201:
$error_msg = wfMsg('201-unpair');
break;
case 401:
$error_msg = wfMsg('error-401');
break;
default:
$error_msg = wfMsg('error-unpair');
break;
}
throw new DBExpectedError( null, $pairResponse->getError()->getCode() . " - " . $error_msg);
}
}
}
# We print the Latch preferences again to make sure that nothing strange happens
SpecialLatch::drawUserPreferences($acc_id, $wgUser, $preferences);
# Required return value of a hook function.
return true;
}
# Hook that is going to run after a successful login
public static function wfLoginHook( &$returnTo, &$returnToQuery, &$type ) {
global $wgUser, $wgOut, $wgRequest, $wgTitle;
$acc_id = "";
$msg = "";
$app_id = "";
$secret = "";
$type = 'error';
$two_factor_token = "";
$user_id = "";
# We remove the user's name to "freeze" the session
$wgRequest->setSessionData( 'wsUserName', "" );
# If app_id, secret, user_id and the account_id are already in the DB, we take them
SpecialLatch::accDB_appsecret ($app_id, $secret);
SpecialLatch::accDB_useraccid ($wgUser->getId(), $user_id, $acc_id);
# If the user doesn't have Latch configured we redirect him to Main Page without checking anything
if (!empty($user_id) && !empty($acc_id)) {
# We call the Status function from the Latch SDK
$api = new Latch($app_id, $secret);
$statusResponse = $api->status($acc_id);
$responseData = $statusResponse->getData();
$responseError = $statusResponse->getError();
if (empty($statusResponse) || (empty($responseData) && empty($responseError))) {
return false;
}
else {
# If everything is OK and the status is on, we redirect the user to the main page and set the user's name again
if (!empty($responseData) && $responseData->{"operations"}->{$app_id}->{"status"} === "on") {
if (!empty($responseData->{"operations"}->{$app_id}->{"two_factor"})) {
$two_factor_token = $responseData->{"operations"}->{$app_id}->{"two_factor"}->{"token"};
# We have another special page for the OTP page. We insert the OTP token on DB and we redirect to that page
if (!empty($two_factor_token)) {
SpecialLatch::updDB_useraccid ($user_id, $acc_id, $two_factor_token);
$wgOut->redirect(
SpecialPage::getTitleFor( 'LatchOTP' )
->getFullURL( '', false, PROTO_CURRENT )
);
}
}
# If the status is on and there's no two factor, we redirect to the main page and set the correct user name.
else {
SpecialLatch::putUserInSession();
}
}
# If the status is off, we logout the user and we show the same message that when a wrong password is used
else if (!empty($responseData) && $responseData->{"operations"}->{$app_id}->{"status"} === "off") {
$wgUser->logout();
$specialUserlogin = new LoginForm();
$specialUserlogin->load();
$error = $specialUserlogin->mAbortLoginErrorMsg ?: 'wrongpassword';
$specialUserlogin->mainLoginForm( $specialUserlogin->msg( $error )->text() );
}
# Otherwise we login normally
else {
SpecialLatch::putUserInSession();
}
}
}
else {
SpecialLatch::putUserInSession();
}
return true;
}
}