Skip to content

Commit a9a5c26

Browse files
committed
* [ADD] Trigger events on enable/disable 2FA. Thanks to @Altes for the feedback. Closes #21
* [MOD] Minor code tweaks
1 parent c8f814f commit a9a5c26

12 files changed

+165
-116
lines changed

base.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* sysPass
44
*
5-
* @author nuxsmin
6-
* @link https://syspass.org
5+
* @author nuxsmin
6+
* @link https://syspass.org
77
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
88
*
99
* This file is part of sysPass.
@@ -22,20 +22,25 @@
2222
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
2323
*/
2424

25+
use Composer\Autoload\ClassLoader;
26+
use SP\Modules\Web\Controllers\AuthenticatorController;
27+
use SP\Modules\Web\Controllers\AuthenticatorLoginController;
28+
use SP\Modules\Web\Plugins\Authenticator\Plugin;
29+
2530
$lib = __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . 'lib';
2631

2732
$base = [
2833
'namespace' => 'SP\Modules\\Web\\Plugins\\Authenticator\\',
2934
'dir' => $lib
3035
];
3136

32-
if (!class_exists(\SP\Modules\Web\Plugins\Authenticator\Plugin::class)) {
33-
/** @var \Composer\Autoload\ClassLoader $loader */
37+
if (!class_exists(Plugin::class)) {
38+
/** @var ClassLoader $loader */
3439
$loader = require APP_ROOT . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
3540
$loader->addPsr4($base['namespace'], $base['dir']);
3641
$loader->addClassMap([
37-
\SP\Modules\Web\Controllers\AuthenticatorController::class => $lib . DIRECTORY_SEPARATOR . 'Controllers' . DIRECTORY_SEPARATOR . 'AuthenticatorController.php',
38-
\SP\Modules\Web\Controllers\AuthenticatorLoginController::class => $lib . DIRECTORY_SEPARATOR . 'Controllers' . DIRECTORY_SEPARATOR . 'AuthenticatorLoginController.php'
42+
AuthenticatorController::class => $lib . DIRECTORY_SEPARATOR . 'Controllers' . DIRECTORY_SEPARATOR . 'AuthenticatorController.php',
43+
AuthenticatorLoginController::class => $lib . DIRECTORY_SEPARATOR . 'Controllers' . DIRECTORY_SEPARATOR . 'AuthenticatorLoginController.php'
3944
]);
4045
}
4146

src/lib/Controllers/AuthenticatorController.php

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* sysPass
44
*
5-
* @author nuxsmin
6-
* @link https://syspass.org
5+
* @author nuxsmin
6+
* @link https://syspass.org
77
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
88
*
99
* This file is part of sysPass.
@@ -24,8 +24,17 @@
2424

2525
namespace SP\Modules\Web\Controllers;
2626

27+
use Defuse\Crypto\Exception\CryptoException;
28+
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
29+
use DI\DependencyException;
30+
use DI\NotFoundException;
31+
use Exception;
2732
use SP\Core\Events\Event;
2833
use SP\Core\Events\EventMessage;
34+
use SP\Core\Exceptions\ConstraintException;
35+
use SP\Core\Exceptions\InvalidArgumentException;
36+
use SP\Core\Exceptions\NoSuchPropertyException;
37+
use SP\Core\Exceptions\QueryException;
2938
use SP\Core\Messages\MailMessage;
3039
use SP\Http\JsonResponse;
3140
use SP\Modules\Web\Controllers\Traits\JsonTrait;
@@ -35,8 +44,10 @@
3544
use SP\Modules\Web\Plugins\Authenticator\Services\AuthenticatorService;
3645
use SP\Modules\Web\Plugins\Authenticator\Util\PluginContext;
3746
use SP\Plugin\PluginManager;
47+
use SP\Repositories\NoSuchItemException;
3848
use SP\Repositories\Track\TrackRequest;
3949
use SP\Services\Mail\MailService;
50+
use SP\Services\ServiceException;
4051
use SP\Services\Track\TrackService;
4152
use SP\Services\User\UserLoginResponse;
4253

@@ -119,7 +130,7 @@ public function saveAction()
119130
JsonResponse::JSON_ERROR,
120131
$e->getMessage()
121132
);
122-
} catch (\Exception $e) {
133+
} catch (Exception $e) {
123134
processException($e);
124135

125136
$this->eventDispatcher->notifyEvent('exception', new Event($e));
@@ -139,7 +150,7 @@ private function addTracking()
139150
{
140151
try {
141152
$this->trackService->add($this->trackRequest);
142-
} catch (\Exception $e) {
153+
} catch (Exception $e) {
143154
processException($e);
144155
}
145156
}
@@ -149,7 +160,7 @@ private function addTracking()
149160
* @param AuthenticatorData $authenticatorData
150161
*
151162
* @return bool
152-
* @throws \Exception
163+
* @throws Exception
153164
*/
154165
private function checkRecoveryCode($pin, AuthenticatorData $authenticatorData)
155166
{
@@ -172,7 +183,7 @@ private function checkRecoveryCode($pin, AuthenticatorData $authenticatorData)
172183
* @param AuthenticatorData $authenticatorData
173184
*
174185
* @return bool
175-
* @throws \Exception
186+
* @throws Exception
176187
*/
177188
private function checkPin($pin, AuthenticatorData $authenticatorData)
178189
{
@@ -193,13 +204,13 @@ private function checkPin($pin, AuthenticatorData $authenticatorData)
193204
* @param AuthenticatorData $authenticatorData
194205
*
195206
* @return bool
196-
* @throws \Defuse\Crypto\Exception\CryptoException
197-
* @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException
198-
* @throws \SP\Core\Exceptions\ConstraintException
199-
* @throws \SP\Core\Exceptions\NoSuchPropertyException
200-
* @throws \SP\Core\Exceptions\QueryException
201-
* @throws \SP\Repositories\NoSuchItemException
202-
* @throws \SP\Services\ServiceException
207+
* @throws CryptoException
208+
* @throws EnvironmentIsBrokenException
209+
* @throws ConstraintException
210+
* @throws NoSuchPropertyException
211+
* @throws QueryException
212+
* @throws NoSuchItemException
213+
* @throws ServiceException
203214
*/
204215
private function save2FAStatus(AuthenticatorData $authenticatorData)
205216
{
@@ -216,6 +227,15 @@ private function save2FAStatus(AuthenticatorData $authenticatorData)
216227

217228
$this->plugin->saveData($this->userData->getId(), $authenticatorData);
218229

230+
$this->eventDispatcher->notifyEvent('authenticator.edit.enable',
231+
new Event($this, EventMessage::factory()
232+
->addDescription(_t('authenticator', '2FA Enabled'))
233+
->addDetail(__('User'), $this->userData->getLogin())
234+
->addExtra('userId', $this->userData->getId())
235+
->addExtra('expireDays', $authenticatorData->getExpireDays())
236+
)
237+
);
238+
219239
return $this->returnJsonResponse(
220240
JsonResponse::JSON_SUCCESS,
221241
_t('authenticator', '2FA Enabled')
@@ -227,6 +247,14 @@ private function save2FAStatus(AuthenticatorData $authenticatorData)
227247
) {
228248
$this->authenticatorService->deletePluginUserData($this->userData->getId());
229249

250+
$this->eventDispatcher->notifyEvent('authenticator.edit.disable',
251+
new Event($this, EventMessage::factory()
252+
->addDescription(_t('authenticator', '2FA Disabled'))
253+
->addDetail(__('User'), $this->userData->getLogin())
254+
->addExtra('userId', $this->userData->getId())
255+
)
256+
);
257+
230258
return $this->returnJsonResponse(
231259
JsonResponse::JSON_SUCCESS,
232260
_t('authenticator', '2FA Disabled')
@@ -298,7 +326,7 @@ public function checkCodeAction()
298326
JsonResponse::JSON_ERROR,
299327
$e->getMessage()
300328
);
301-
} catch (\Exception $e) {
329+
} catch (Exception $e) {
302330
processException($e);
303331

304332
$this->eventDispatcher->notifyEvent('exception', new Event($e));
@@ -335,13 +363,21 @@ private function sendResetEmail(AuthenticatorData $authenticatorData)
335363
$this->userData->getEmail(),
336364
$message);
337365

366+
$this->eventDispatcher->notifyEvent('authenticator.send.recoverycode',
367+
new Event($this, EventMessage::factory()
368+
->addDescription(_t('authenticator', '2FA Code Recovery'))
369+
->addDetail(__('User'), $this->userData->getLogin())
370+
->addExtra('userId', $this->userData->getId())
371+
)
372+
);
373+
338374
return true;
339375
}
340376

341377
return false;
342378
} catch (AuthenticatorException $e) {
343379
throw $e;
344-
} catch (\Exception $e) {
380+
} catch (Exception $e) {
345381
processException($e);
346382

347383
$this->eventDispatcher->notifyEvent('exception', new Event($e));
@@ -367,7 +403,10 @@ public function showRecoveryCodesAction()
367403
if (count($codes) > 0) {
368404
$this->eventDispatcher->notifyEvent('authenticator.show.recoverycode',
369405
new Event($this, EventMessage::factory()
370-
->addDescription(_t('authenticator', 'Recovery codes displayed')))
406+
->addDescription(_t('authenticator', 'Recovery codes displayed'))
407+
->addDetail(__('User'), $this->userData->getLogin())
408+
->addExtra('userId', $this->userData->getId())
409+
)
371410
);
372411

373412
return $this->returnJsonResponseData($codes);
@@ -377,7 +416,7 @@ public function showRecoveryCodesAction()
377416
_t('authenticator', 'There aren\'t any recovery codes available')
378417
);
379418
}
380-
} catch (\Exception $e) {
419+
} catch (Exception $e) {
381420
processException($e);
382421

383422
$this->eventDispatcher->notifyEvent('exception', new Event($e));
@@ -398,9 +437,9 @@ public function checkVersionAction()
398437
}
399438

400439
/**
401-
* @throws \DI\DependencyException
402-
* @throws \DI\NotFoundException
403-
* @throws \SP\Core\Exceptions\InvalidArgumentException
440+
* @throws DependencyException
441+
* @throws NotFoundException
442+
* @throws InvalidArgumentException
404443
*/
405444
protected function initialize()
406445
{

src/lib/Controllers/AuthenticatorLoginController.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* sysPass
44
*
5-
* @author nuxsmin
6-
* @link https://syspass.org
5+
* @author nuxsmin
6+
* @link https://syspass.org
77
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
88
*
99
* This file is part of sysPass.
@@ -24,12 +24,16 @@
2424

2525
namespace SP\Modules\Web\Controllers;
2626

27+
use DI\DependencyException;
28+
use DI\NotFoundException;
2729
use SP\Core\Events\Event;
2830
use SP\Core\Events\EventMessage;
31+
use SP\Core\Exceptions\SessionTimeout;
2932
use SP\Modules\Web\Controllers\Helpers\LayoutHelper;
3033
use SP\Modules\Web\Controllers\Traits\JsonTrait;
3134
use SP\Modules\Web\Plugins\Authenticator\Plugin;
3235
use SP\Plugin\PluginManager;
36+
use SP\Services\Auth\AuthException;
3337

3438
/**
3539
* Class LoginController
@@ -50,11 +54,11 @@ final class AuthenticatorLoginController extends ControllerBase
5054
/**
5155
* Obtener los datos para el interface de autentificación en 2 pasos
5256
*
53-
* @throws \DI\DependencyException
54-
* @throws \DI\NotFoundException
55-
* @throws \SP\Core\Exceptions\SessionTimeout
56-
* @throws \SP\Services\Auth\AuthException
57-
* @throws \SP\Core\Exceptions\SessionTimeout
57+
* @throws DependencyException
58+
* @throws NotFoundException
59+
* @throws SessionTimeout
60+
* @throws AuthException
61+
* @throws SessionTimeout
5862
*/
5963
public function indexAction()
6064
{
@@ -96,23 +100,31 @@ protected function checkExpireTime()
96100
$timeRemaining = $expireTime - time();
97101

98102
if ($timeRemaining <= self::WARNING_TIME) {
99-
$this->eventDispatcher->notifyEvent('authenticator.expiry.notice',
103+
$this->eventDispatcher->notifyEvent('authenticator.expiry.warn',
100104
new Event($this, EventMessage::factory()
101105
->addDescription(_t('authenticator', 'Expire Notice'))
102-
->addDescription(sprintf(_t('authenticator', 'The 2FA code will need to be reset within %d days'), $timeRemaining / 86400)))
106+
->addDescription(sprintf(_t('authenticator',
107+
'The 2FA code will need to be reset within %d days'), $timeRemaining / 86400))
108+
->addDetail(__('User'), $this->userData->getLogin())
109+
->addExtra('userId', $this->userData->getId())
110+
)
103111
);
104112
} elseif (time() > $expireTime) {
105-
$this->eventDispatcher->notifyEvent('authenticator.expiry.notice',
113+
$this->eventDispatcher->notifyEvent('authenticator.expiry.expired',
106114
new Event($this, EventMessage::factory()
107115
->addDescription(_t('authenticator', 'Expire Notice'))
108-
->addDescription(_t('authenticator', 'The 2FA code is expired. You need to reset it on preferences tab')))
116+
->addDescription(_t('authenticator',
117+
'The 2FA code is expired. You need to reset it on preferences tab'))
118+
->addDetail(__('User'), $this->userData->getLogin())
119+
->addExtra('userId', $this->userData->getId())
120+
)
109121
);
110122
}
111123
}
112124

113125
/**
114-
* @throws \DI\DependencyException
115-
* @throws \DI\NotFoundException
126+
* @throws DependencyException
127+
* @throws NotFoundException
116128
*/
117129
protected function initialize()
118130
{

src/lib/Controllers/PreferencesController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* sysPass
44
*
5-
* @author nuxsmin
6-
* @link https://syspass.org
5+
* @author nuxsmin
6+
* @link https://syspass.org
77
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
88
*
99
* This file is part of sysPass.

src/lib/Models/AuthenticatorData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
/**
33
* sysPass
44
*
5-
* @author nuxsmin
6-
* @link https://syspass.org
5+
* @author nuxsmin
6+
* @link https://syspass.org
77
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
88
*
99
* This file is part of sysPass.

0 commit comments

Comments
 (0)