-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix state propragation of the backup codes provider
Starting with Nextcloud 14, the server knows the enabled/disabled state of 2fa providers. While it will query that information if it's unknown (on first use), it won't notice any changes. Thus, providers have to propagate that information themselves. Ref nextcloud/twofactor_totp#263 Ref nextcloud/twofactor_u2f#210 Signed-off-by: Christoph Wurst <[email protected]>
- Loading branch information
1 parent
eedfb0d
commit 6afacad
Showing
12 changed files
with
410 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> | ||
* | ||
* @author Joas Schilling <[email protected]> | ||
* @author Christoph Wurst <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
|
@@ -24,11 +26,16 @@ | |
namespace OCA\TwoFactorBackupCodes\AppInfo; | ||
|
||
use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; | ||
use OCA\TwoFactorBackupCodes\Event\CodesGenerated; | ||
use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher; | ||
use OCA\TwoFactorBackupCodes\Listener\IListener; | ||
use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater; | ||
use OCP\AppFramework\App; | ||
use OCP\Util; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
|
||
class Application extends App { | ||
public function __construct () { | ||
public function __construct() { | ||
parent::__construct('twofactor_backupcodes'); | ||
} | ||
|
||
|
@@ -44,6 +51,21 @@ public function register() { | |
*/ | ||
public function registerHooksAndEvents() { | ||
Util::connectHook('OC_User', 'post_deleteUser', $this, 'deleteUser'); | ||
|
||
$container = $this->getContainer(); | ||
/** @var EventDispatcherInterface $eventDispatcher */ | ||
$eventDispatcher = $container->query(EventDispatcherInterface::class); | ||
$eventDispatcher->addListener(CodesGenerated::class, function (CodesGenerated $event) use ($container) { | ||
/** @var IListener[] $listeners */ | ||
$listeners = [ | ||
$container->query(ActivityPublisher::class), | ||
$container->query(RegistryUpdater::class), | ||
]; | ||
|
||
foreach ($listeners as $listener) { | ||
$listener->handle($event); | ||
} | ||
}); | ||
} | ||
|
||
public function deleteUser($params) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Christoph Wurst <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\TwoFactorBackupCodes\Event; | ||
|
||
use OCP\IUser; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class CodesGenerated extends Event { | ||
|
||
/** @var IUser */ | ||
private $user; | ||
|
||
public function __construct(IUser $user) { | ||
$this->user = $user; | ||
} | ||
|
||
/** | ||
* @return IUser | ||
*/ | ||
public function getUser(): IUser { | ||
return $this->user; | ||
} | ||
|
||
} |
49 changes: 49 additions & 0 deletions
49
apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: christoph | ||
* Date: 31.07.18 | ||
* Time: 09:41 | ||
*/ | ||
|
||
namespace OCA\TwoFactorBackupCodes\Listener; | ||
|
||
use OCA\TwoFactorBackupCodes\Event\CodesGenerated; | ||
use OCP\Activity\IManager; | ||
use OCP\ILogger; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class ActivityPublisher implements IListener { | ||
|
||
/** @var IManager */ | ||
private $activityManager; | ||
|
||
/** @var ILogger */ | ||
private $logger; | ||
|
||
public function __construct(IManager $activityManager, ILogger $logger) { | ||
$this->activityManager = $activityManager; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Push an event to the user's activity stream | ||
*/ | ||
public function handle(Event $event) { | ||
if ($event instanceof CodesGenerated) { | ||
$activity = $this->activityManager->generateEvent(); | ||
$activity->setApp('twofactor_backupcodes') | ||
->setType('security') | ||
->setAuthor($event->getUser()->getUID()) | ||
->setAffectedUser($event->getUser()->getUID()) | ||
->setSubject('codes_generated'); | ||
try { | ||
$this->activityManager->publish($activity); | ||
} catch (BadMethodCallException $e) { | ||
$this->logger->warning('could not publish backup code creation activity', ['app' => 'twofactor_backupcodes']); | ||
$this->logger->logException($e, ['app' => 'twofactor_backupcodes']); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Christoph Wurst <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\TwoFactorBackupCodes\Listener; | ||
|
||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
interface IListener { | ||
|
||
public function handle(Event $event); | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Christoph Wurst <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\TwoFactorBackupCodes\Listener; | ||
|
||
use OCA\TwoFactorBackupCodes\Event\CodesGenerated; | ||
use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider; | ||
use OCP\Authentication\TwoFactorAuth\IRegistry; | ||
use Symfony\Component\EventDispatcher\Event; | ||
|
||
class RegistryUpdater implements IListener { | ||
|
||
/** @var IRegistry */ | ||
private $registry; | ||
|
||
/** @var BackupCodesProvider */ | ||
private $provider; | ||
|
||
public function __construct(IRegistry $registry, BackupCodesProvider $provider) { | ||
$this->registry = $registry; | ||
$this->provider = $provider; | ||
} | ||
|
||
public function handle(Event $event) { | ||
if ($event instanceof CodesGenerated) { | ||
$this->registry->enableProviderFor($this->provider, $event->getUser()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
apps/twofactor_backupcodes/tests/Unit/Event/CodesGeneratedTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Christoph Wurst <[email protected]> | ||
* | ||
* @license GNU AGPL version 3 or any later version | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
namespace OCA\TwoFactorBackupCodes\Tests\Unit\Event; | ||
|
||
use OCA\TwoFactorBackupCodes\Event\CodesGenerated; | ||
use OCP\IUser; | ||
use Test\TestCase; | ||
|
||
class CodesGeneratedTest extends TestCase { | ||
|
||
public function testCodeGeneratedEvent() { | ||
$user = $this->createMock(IUser::class); | ||
|
||
$event = new CodesGenerated($user); | ||
|
||
$this->assertSame($user, $event->getUser()); | ||
} | ||
} |
Oops, something went wrong.