/
TwoFactorAdminLogin.php
513 lines (450 loc) · 16.8 KB
/
TwoFactorAdminLogin.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<?php
/**
* Plugin to enable two factor authentication for LimeSurvey Admin Backend
* @author LimeSurvey GmbH <info@limesurvey.org>
* @license GPL 2.0 or later
*/
//Get necessary libraries and component plugins
require_once(__DIR__ . '/vendor/autoload.php');
spl_autoload_register(function ($class_name) {
if (preg_match("/^TFA.*/", $class_name)) {
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . $class_name . '.php')) {
include __DIR__ . DIRECTORY_SEPARATOR . 'models' . DIRECTORY_SEPARATOR . $class_name . '.php';
} elseif (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'helper' . DIRECTORY_SEPARATOR . $class_name . '.php')) {
include __DIR__ . DIRECTORY_SEPARATOR . 'helper' . DIRECTORY_SEPARATOR . $class_name . '.php';
} elseif (file_exists(__DIR__ . DIRECTORY_SEPARATOR . 'installer' . DIRECTORY_SEPARATOR . $class_name . '.php')) {
include __DIR__ . DIRECTORY_SEPARATOR . 'installer' . DIRECTORY_SEPARATOR . $class_name . '.php';
}
}
});
class TwoFactorAdminLogin extends AuthPluginBase
{
protected static $description = 'Add two-factor authentication to your admin login';
protected static $name = 'TwoFactorAdminLogin';
private $o2FA = null;
protected $storage = 'DbStorage';
protected $settings = array(
'issuer' => array(
'type' => 'string',
'label' => 'Issuer',
'default' => 'LimeSurvey Survey Software',
'help' => 'This will be displayed in the 2FA app as issuer name.'
),
'digits' => array(
'type' => 'string',
'label' => 'Code length',
'default' => '6',
'help' => 'The number of digits the resulting codes will be. Leave it at 6 for best compatibility.'
),
'period' => array(
'type' => 'string',
'label' => 'Time period',
'default' => '30',
'help' => 'The number of seconds a code will be valid. Please leave it at 30 for best compatibility.'
),
'leeway' => array(
'type' => 'string',
'label' => 'Discrepancy',
'default' => '5',
'help' => 'What amount of discrepancy in seconds is allowed for the client.'
),
'algorithm' => array(
'type' => 'select',
'label' => 'Algorithm',
'default' => 'sha1',
'options' => [
'sha1' => 'SHA1 (Default)',
'sha256 ' => 'SHA256',
'md5' => 'MD5',
],
'help' => 'Please keep in mind, that most tools only work with SHA1 hashing.'
),
'force2fa' => array(
'type' => 'select',
'label' => 'Prompt to activate 2FA on login',
'default' => '0',
'options' => [
'0 ' => 'No',
'1' => 'Yes',
],
'help' => 'Upon login, users who have not enabled two-factor authentication (2FA) will be prompted to set it up.'
),
);
public function init()
{
//System events
$this->subscribe('direct');
$this->subscribe('newDirectRequest');
$this->subscribe('beforeActivate');
$this->subscribe('beforeDeactivate');
$this->subscribe('beforeAdminMenuRender');
$this->subscribe('afterSuccessfulLogin');
//Login events
$this->subscribe('newLoginForm');
$this->subscribe('afterLoginFormSubmit');
$this->subscribe('newUserSession');
}
//############## Plugin event handlers ##############//
/**
* Listen to direct requests
* Necessary for the getMetadata function
*
* @return void
*/
public function newDirectRequest()
{
$request = $this->api->getRequest();
$oEvent = $this->getEvent();
if ($oEvent->get('target') != 'TwoFactorAdminLogin') {
return;
}
$action = $oEvent->get('function');
if (method_exists($this, $action)) {
call_user_func([$this, $action], $oEvent, $request);
}
}
/**
* Event direct happen
* Usage : index --target=value [--function=] [--option=]
* @return @void
*/
public function direct()
{
$oEvent = $this->getEvent();
if ($oEvent->get('target') != 'TwoFactorAdminLogin') {
return;
}
$option = $this->event->get("option");
$action = $oEvent->get('function');
if (method_exists($this, $action)) {
call_user_func([$this, $action], $oEvent, $option);
}
}
/**
* Register new table and populate it.
*
* @return void
*/
public function beforeActivate()
{
TFAPluginInstaller::instance()->install();
}
/**
* Delete the created tables again
*
* @return void
*/
public function beforeDeactivate()
{
TFAPluginInstaller::instance()->uninstall();
}
/**
* Add Two-Factor field to login page.
*
* @return void
*/
public function newLoginForm()
{
$oEvent = $this->getEvent();
$extraLine = ""
. "<span>"
. "<label for='twofactor'>" . gT("2FA key (optional)") . "</label>
<input class='form-control' name='twofactor' id='twofactor' type='text' size='" . $this->get('digits', null, null, 6) . "' maxlength='" . $this->get('digits', null, null, 6) . "' value='' />"
. "</span>";
$oEvent->getContent('Authdb')->addContent($extraLine, 'append');
}
/**
* Control if login is successful by checking the transmitted 2FA-token value
*
* @return void
*/
public function newUserSession()
{
$oEvent = $this->getEvent();
$onepass = App()->request->getParam('onepass');
// skip 2fa when theres an active and verified onetimepassword used (verification is allready done before getting here)
if (App()->getConfig('use_one_time_passwords') && isset($onepass)) {
return;
}
$oIdentity = $oEvent->get('identity');
$oTFAModel = TFAUserKey::model()->findByPk($oIdentity->id);
if ($oTFAModel != null) {
$authenticationKey = Yii::app()->getRequest()->getPost('twofactor', false);
if (!$authenticationKey || !$this->confirmKey($oTFAModel->secretKey, $authenticationKey)) {
$this->setAuthFailure(null, 'Authentication key invalid');
}
}
return;
}
/**
* Add menue to the top bar
* @return void
*/
public function beforeAdminMenuRender()
{
$oEvent = $this->getEvent();
$aMenuItems = [];
$aMenuItemUserOptions = [
'isDivider' => false,
'isSmallText' => false,
'label' => gT('General'),
'href' => $this->api->createUrl('admin/pluginhelper/sa/fullpagewrapper/plugin/TwoFactorAdminLogin/method/userindex', []),
'iconClass' => 'ri-spy-fill',
];
$aMenuItems[] = (new \LimeSurvey\Menu\MenuItem($aMenuItemUserOptions));
if (Permission::model()->hasGlobalPermission('users', 'update')) {
$aMenuItemAdminOptions = [
'isDivider' => false,
'isSmallText' => false,
'label' => gT('Administration'),
'href' => $this->api->createUrl('admin/pluginhelper/sa/fullpagewrapper/plugin/TwoFactorAdminLogin/method/index', []),
'iconClass' => 'ri-group-fill',
];
$aMenuItems[] = (new \LimeSurvey\Menu\MenuItem($aMenuItemAdminOptions));
}
$aNewMenuOptions = [
'isDropDown' => true,
'label' => gT('2FA settings'),
'href' => '#',
'menuItems' => $aMenuItems,
'iconClass' => 'ri-lock-fill',
'isInMiddleSection' => true,
'isPrepended' => false,
];
$oNewMenu = new TFAMenuClass($aNewMenuOptions);
//enable menu only if plugin is active
if (TFAHelper::isPluginActive()) {
$oEvent->append('extraMenus', [$oNewMenu]);
}
}
/**
* If force 2FA login is enabled, redirect to the 2FA page
* @return void
*/
public function afterSuccessfulLogin()
{
$oEvent = $this->getEvent();
$oTFAModel = TFAUserKey::model()->findByPk(App()->user->id);
if ($oTFAModel == null && $this->get('force2fa', null, null, 0) == 1) {
Yii::app()->getController()->redirect($this->api->createUrl('admin/pluginhelper/sa/fullpagewrapper/plugin/TwoFactorAdminLogin/method/userindex', []));
}
}
//################# View rendering ###################
/**
* Renders a list of users including their 2FA settings
* To be called by fullpagewrapper
*
* @return string
*/
public function index()
{
if (Yii::app()->getRequest()->getQuery('pageSize')) {
Yii::app()->user->setState('pageSize', (int) Yii::app()->getRequest()->getQuery('pageSize'));
}
$model = new TFAUser('search');
$model->setAttributes(Yii::app()->getRequest()->getParam('TFAUser'));
$iPageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);
$aData = [
'model' => $model,
'pageSize' => $iPageSize,
];
$this->pageScripts();
return $this->renderPartial('index', $aData, true);
}
/**
* Renders the user configuration page
* To be called by fullpagewrapper
*
* @return string
*/
public function userindex()
{
$iUserId = Yii::app()->getRequest()->getPost('iUserId', Yii::app()->user->id);
$oTFAModel = TFAUserKey::model()->findByPk($iUserId);
$aData = [
'oTFAModel' => $oTFAModel,
'force2FA' => $this->get('force2fa', null, null, 0) == 1
];
$this->pageScripts();
return $this->renderPartial('userindex', $aData, true);
}
//################ Direct access methods ###############
/**
* Renders the content of the modal to create a 2FA key registration
*
* @param PluginEvent $oEvent
* @param CHttpRequest $oRequest
* @return string
*/
public function directCallCreateNewKey($oEvent, $oRequest)
{
$iUserId = Yii::app()->getRequest()->getParam('uid', Yii::app()->user->id);
if (!Permission::model()->hasGlobalPermission('users', 'update') && $iUserId !== Yii::app()->user->id) {
return $this->renderPartial('_partial.error', [
'errors' => ["No permission"]
]);
}
$oTFAModel = TFAUserKey::model()->findByPk($iUserId);
if ($oTFAModel == null) {
$oTFAModel = new TFAUserKey();
}
$o2FA = $this->get2FAObject();
$oTFAModel->uid = $iUserId;
$oTFAModel->secretKey = $o2FA->createSecret();
$sQRCodeContent = '<img src="' . $o2FA->getQRCodeImageAsDataUri('LimeSurvey - User ID: ' . Yii::app()->user->id, $oTFAModel->secretKey) . '">';
return $this->renderPartial('_partial/create', [
'model' => $oTFAModel,
'sQRCodeContent' => $sQRCodeContent,
]);
}
/**
* Checks a submitted authentication code and stores the underlaying secret key into the Database.
* Returns a JSON document
*
* @param PluginEvent $oEvent
* @param CHttpRequest $oRequest
* @return boolean
*/
public function directCallConfirmKey($oEvent, $oRequest)
{
$aTFAUserKey = Yii::app()->getRequest()->getPost('TFAUserKey', []);
$uid = $aTFAUserKey['uid'];
if (!(Permission::model()->hasGlobalPermission('users', 'update') || $uid == Yii::app()->user->id)) {
return $this->createJSONResponse(false, "No permission");
}
$o2FA = $this->get2FAObject();
$sConfirmationKey = Yii::app()->getRequest()->getPost('confirmationKey', '');
if ($sConfirmationKey == '') {
return $this->createJSONResponse(false, gT("Please enter a confirmation key"));
}
$result = $o2FA->verifyCode($aTFAUserKey['secretKey'], $sConfirmationKey);
if (!$result) {
return $this->createJSONResponse(false, gT("The confirmation key is not correct."));
}
$oTFAModel = new TFAUserKey();
$oTFAModel->setAttributes($aTFAUserKey, false);
$oTFAModel->firstLogin = 0;
if (!$oTFAModel->save()) {
return $this->createJSONResponse(false, gT("The two-factor authentication key could not be stored."));
}
return $this->createJSONResponse(true, "Two-factor method successfully stored", ['reload' => true]);
}
/**
* Deletes a users secret, effectively ending the 2FA login mechanism for that user.
* Returns a JSON document
*
* @param PluginEvent $oEvent
* @param CHttpRequest $oRequest
* @return boolean
*/
public function directCallDeleteKey($oEvent, $oRequest)
{
$uid = $oRequest->getPost('uid', null);
if (!Permission::model()->hasGlobalPermission('users', 'update') && $uid !== Yii::app()->user->id) {
return $this->createJSONResponse(false, gT('No permission'));
}
$oTFAModel = TFAUserKey::model()->findByPk($uid);
$success = $oTFAModel->delete();
return $this->createJSONResponse($success, ($success ? gT('Successfully deleted') : gT('Deletion failed')));
}
/**
* Deletes a users secret, effectively ending the 2FA login mechanism for that user CLI version.
* Returns a String
*
* @param PluginEvent $oEvent
* @param string $sOption
* @return string
*/
public function deleteKeyForUserId($oEvent, $iUserId)
{
$uid = (int) $iUserId;
$oTFAModel = TFAUserKey::model()->findByPk($uid);
if ($oTFAModel == null) {
printf(gT("No 2FA key set for user ID %s"), $iUserId);
return;
}
$success = $oTFAModel->delete();
echo ($success ? gT('Successfully deleted') : gT('Deletion failed'));
}
/**
* Deletes a users secret, effectively ending the 2FA login mechanism for that user CLI version.
* Returns a String
*
* @param PluginEvent $oEvent
* @param string $sOption
* @return string
*/
public function deleteKeyForUserName($oEvent, $sUserName)
{
$oUser = User::model()->findByAttributes(['users_name' => $sUserName]);
$oTFAModel = TFAUserKey::model()->findByPk($oUser->uid);
if ($oTFAModel == null) {
echo "No 2FA key set for user " . $sUserName;
return;
}
$success = $oTFAModel->delete();
echo ($success ? 'Successfully deleted' : 'Deleting failed');
}
//################### Utility methods ##################
/**
* Checks a 2FA OTP authentication code against the stored secret
*
* @param string $secretKey
* @param string $authenticationCode
* @return boolean true on authentication code matching stored secret key
*/
private function confirmKey($secretKey, $authenticationCode)
{
$o2FA = $this->get2FAObject();
return $o2FA->verifyCode($secretKey, $authenticationCode);
}
/**
* Creates or returns a 2FA-library object
* Using Rob Janssen TwoFactorAuth - Library https://github.com/RobThree/TwoFactorAuth
*
* @return TwoFactorAuth
*/
private function get2FAObject()
{
if ($this->o2FA == null) {
$mp = new TFAQrCodeGenerator();
$this->o2FA = new RobThree\Auth\TwoFactorAuth(
$this->get('issuer', null, null, 'LimeSurvey - survey software'),
((int) $this->get('digits', null, null, 6)),
((int) $this->get('period', null, null, 30)),
$this->get('algorithm', null, null, 'sha1'),
$mp
);
}
return $this->o2FA;
}
/**
* Generates a printed JSON-Response
*
* @param bool $success
* @param string $message
* @param array $data
* @return boolean $success
*/
protected function createJSONResponse($success, $message, $data = [])
{
header('Content-Type: application/json');
echo json_encode([
'success' => $success,
'message' => $message,
'data' => $data
]);
return $success;
}
/**
* Applies the necessary page scripts to the page through CClientScript derivate
*
* @return void
*/
protected function pageScripts()
{
$assetsUrl = Yii::app()->assetManager->publish(dirname(__FILE__) . '/assets');
Yii::app()->clientScript->registerScriptFile($assetsUrl . '/tfaScripts.js', LSYii_ClientScript::POS_HEAD);
Yii::app()->clientScript->registerCssFile($assetsUrl . '/tfaStyles.css');
}
}