-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix state propragation of the backup codes provider #10465
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @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; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @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 BadMethodCallException; | ||
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']); | ||
} | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since the interface can not be used by other apps drop it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree that it's not necessary to have an interface here. But having an interface is not only helpful when types are shared across apps. They make sure that the listeners implement a certain interface. This is purely used to keep the code clean and maintainable. Again, I can remove it if you prefer to have it simpler. We're back at duck typing then, though. |
||
|
||
public function handle(Event $event); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @author Christoph Wurst <christoph@winzerhof-wurst.at> | ||
* | ||
* @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()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer to use the
GenericEvent
instead, because that allows other apps to also listen to the event. Otherwise they would need to cast to this specific class of another app to get the user info.new GenericEvent($user)
And access with
$event->getSubject()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First, my intention is not to share this event with other apps. It's meant to be used internally only. Second, I like this custom event class better because it offers a type-safe way of transporting the user/subject. The
GenericEvent
is, well, generic 😉.These are minor issues. If you insist on the change, I'll happily do so. I just wanted to give you my point of view.