diff options
Diffstat (limited to 'apps/twofactor_backupcodes/lib')
24 files changed, 260 insertions, 728 deletions
diff --git a/apps/twofactor_backupcodes/lib/Activity/Provider.php b/apps/twofactor_backupcodes/lib/Activity/Provider.php index 12c71f68d1c..eba38147bd7 100644 --- a/apps/twofactor_backupcodes/lib/Activity/Provider.php +++ b/apps/twofactor_backupcodes/lib/Activity/Provider.php @@ -1,30 +1,14 @@ <?php + +declare(strict_types=1); + /** - * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Activity; -use InvalidArgumentException; +use OCP\Activity\Exceptions\UnknownActivityException; use OCP\Activity\IEvent; use OCP\Activity\IManager; use OCP\Activity\IProvider; @@ -33,29 +17,21 @@ use OCP\L10N\IFactory as L10nFactory; class Provider implements IProvider { - /** @var L10nFactory */ - private $l10n; - - /** @var IURLGenerator */ - private $urlGenerator; - - /** @var IManager */ - private $activityManager; - /** * @param L10nFactory $l10n * @param IURLGenerator $urlGenerator * @param IManager $activityManager */ - public function __construct(L10nFactory $l10n, IURLGenerator $urlGenerator, IManager $activityManager) { - $this->urlGenerator = $urlGenerator; - $this->activityManager = $activityManager; - $this->l10n = $l10n; + public function __construct( + private L10nFactory $l10n, + private IURLGenerator $urlGenerator, + private IManager $activityManager, + ) { } - public function parse($language, IEvent $event, IEvent $previousEvent = null) { + public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent { if ($event->getApp() !== 'twofactor_backupcodes') { - throw new InvalidArgumentException(); + throw new UnknownActivityException(); } $l = $this->l10n->get('twofactor_backupcodes', $language); @@ -71,7 +47,7 @@ class Provider implements IProvider { } break; default: - throw new InvalidArgumentException(); + throw new UnknownActivityException(); } return $event; } diff --git a/apps/twofactor_backupcodes/lib/AppInfo/Application.php b/apps/twofactor_backupcodes/lib/AppInfo/Application.php index 4ac5016f925..e9a1ec1c72c 100644 --- a/apps/twofactor_backupcodes/lib/AppInfo/Application.php +++ b/apps/twofactor_backupcodes/lib/AppInfo/Application.php @@ -3,47 +3,27 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\AppInfo; -use Closure; -use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCA\TwoFactorBackupCodes\Listener\ActivityPublisher; use OCA\TwoFactorBackupCodes\Listener\ClearNotifications; use OCA\TwoFactorBackupCodes\Listener\ProviderDisabled; use OCA\TwoFactorBackupCodes\Listener\ProviderEnabled; use OCA\TwoFactorBackupCodes\Listener\RegistryUpdater; +use OCA\TwoFactorBackupCodes\Listener\UserDeleted; use OCA\TwoFactorBackupCodes\Notifications\Notifier; +use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; -use OCP\Authentication\TwoFactorAuth\IRegistry; -use OCP\Notification\IManager; -use OCP\Util; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered; +use OCP\User\Events\UserDeletedEvent; class Application extends App implements IBootstrap { public const APP_ID = 'twofactor_backupcodes'; @@ -53,33 +33,19 @@ class Application extends App implements IBootstrap { } public function register(IRegistrationContext $context): void { - $this->registerHooksAndEvents($context); - } + $context->registerNotifierService(Notifier::class); - public function boot(IBootContext $context): void { - Util::connectHook('OC_User', 'post_deleteUser', $this, 'deleteUser'); - - $context->injectFn(Closure::fromCallable([$this, 'registerNotification'])); - } - - /** - * Register the hooks and events - */ - public function registerHooksAndEvents(IRegistrationContext $context) { $context->registerEventListener(CodesGenerated::class, ActivityPublisher::class); $context->registerEventListener(CodesGenerated::class, RegistryUpdater::class); $context->registerEventListener(CodesGenerated::class, ClearNotifications::class); - $context->registerEventListener(IRegistry::EVENT_PROVIDER_ENABLED, ProviderEnabled::class); - $context->registerEventListener(IRegistry::EVENT_PROVIDER_DISABLED, ProviderDisabled::class); - } + $context->registerEventListener(TwoFactorProviderForUserRegistered::class, ProviderEnabled::class); + $context->registerEventListener(TwoFactorProviderForUserUnregistered::class, ProviderDisabled::class); + $context->registerEventListener(UserDeletedEvent::class, UserDeleted::class); - private function registerNotification(IManager $manager) { - $manager->registerNotifierService(Notifier::class); + + $context->registerTwoFactorProvider(BackupCodesProvider::class); } - public function deleteUser($params) { - /** @var BackupCodeMapper $mapper */ - $mapper = $this->getContainer()->query(BackupCodeMapper::class); - $mapper->deleteCodesByUserId($params['uid']); + public function boot(IBootContext $context): void { } } diff --git a/apps/twofactor_backupcodes/lib/BackgroundJob/CheckBackupCodes.php b/apps/twofactor_backupcodes/lib/BackgroundJob/CheckBackupCodes.php index 277fff9e70c..bc26cb260f4 100644 --- a/apps/twofactor_backupcodes/lib/BackgroundJob/CheckBackupCodes.php +++ b/apps/twofactor_backupcodes/lib/BackgroundJob/CheckBackupCodes.php @@ -3,28 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\BackgroundJob; use OC\Authentication\TwoFactorAuth\Manager; @@ -37,28 +18,26 @@ use OCP\IUserManager; class CheckBackupCodes extends QueuedJob { - /** @var IUserManager */ - private $userManager; - - /** @var IJobList */ - private $jobList; - - /** @var Manager */ - private $registry; - /** @var Manager */ private $twofactorManager; - public function __construct(ITimeFactory $timeFactory, IUserManager $userManager, IJobList $jobList, Manager $twofactorManager, IRegistry $registry) { + public function __construct( + ITimeFactory $timeFactory, + private IUserManager $userManager, + private IJobList $jobList, + Manager $twofactorManager, + private IRegistry $registry, + ) { parent::__construct($timeFactory); - $this->userManager = $userManager; - $this->jobList = $jobList; $this->twofactorManager = $twofactorManager; - $this->registry = $registry; } protected function run($argument) { - $this->userManager->callForSeenUsers(function (IUser $user) { + $this->userManager->callForSeenUsers(function (IUser $user): void { + if (!$user->isEnabled()) { + return; + } + $providers = $this->registry->getProviderStates($user); $isTwoFactorAuthenticated = $this->twofactorManager->isTwoFactorAuthenticated($user); diff --git a/apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php b/apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php index ec45420299b..5e853479f0a 100644 --- a/apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php +++ b/apps/twofactor_backupcodes/lib/BackgroundJob/RememberBackupCodesJob.php @@ -3,28 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\BackgroundJob; use OCP\AppFramework\Utility\ITimeFactory; @@ -36,31 +17,18 @@ use OCP\Notification\IManager; class RememberBackupCodesJob extends TimedJob { - /** @var IRegistry */ - private $registry; - - /** @var IUserManager */ - private $userManager; - - /** @var IManager */ - private $notificationManager; - - /** @var IJobList */ - private $jobList; - - public function __construct(IRegistry $registry, - IUserManager $userManager, - ITimeFactory $timeFactory, - IManager $notificationManager, - IJobList $jobList) { + public function __construct( + private IRegistry $registry, + private IUserManager $userManager, + ITimeFactory $timeFactory, + private IManager $notificationManager, + private IJobList $jobList, + ) { parent::__construct($timeFactory); - $this->registry = $registry; - $this->userManager = $userManager; $this->time = $timeFactory; - $this->notificationManager = $notificationManager; - $this->jobList = $jobList; $this->setInterval(60 * 60 * 24 * 14); + $this->setTimeSensitivity(self::TIME_INSENSITIVE); } protected function run($argument) { @@ -69,6 +37,7 @@ class RememberBackupCodesJob extends TimedJob { if ($user === null) { // We can't run with an invalid user + $this->jobList->remove(self::class, $argument); return; } @@ -93,9 +62,16 @@ class RememberBackupCodesJob extends TimedJob { $notification = $this->notificationManager->createNotification(); $notification->setApp('twofactor_backupcodes') ->setUser($user->getUID()) - ->setDateTime($date) ->setObject('create', 'codes') ->setSubject('create_backupcodes'); + $this->notificationManager->markProcessed($notification); + + if (!$user->isEnabled()) { + // Don't recreate a notification for a user that can not read it + $this->jobList->remove(self::class, $argument); + return; + } + $notification->setDateTime($date); $this->notificationManager->notify($notification); } } diff --git a/apps/twofactor_backupcodes/lib/Controller/SettingsController.php b/apps/twofactor_backupcodes/lib/Controller/SettingsController.php index 04ec2e19e95..effc058e05c 100644 --- a/apps/twofactor_backupcodes/lib/Controller/SettingsController.php +++ b/apps/twofactor_backupcodes/lib/Controller/SettingsController.php @@ -1,62 +1,44 @@ <?php + +declare(strict_types=1); + /** - * - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Controller; use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\Attribute\NoAdminRequired; +use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; use OCP\IUserSession; class SettingsController extends Controller { - /** @var BackupCodeStorage */ - private $storage; - - /** @var IUserSession */ - private $userSession; - /** * @param string $appName * @param IRequest $request * @param BackupCodeStorage $storage * @param IUserSession $userSession */ - public function __construct($appName, IRequest $request, BackupCodeStorage $storage, IUserSession $userSession) { + public function __construct( + $appName, + IRequest $request, + private BackupCodeStorage $storage, + private IUserSession $userSession, + ) { parent::__construct($appName, $request); - $this->userSession = $userSession; - $this->storage = $storage; } /** - * @NoAdminRequired - * @PasswordConfirmationRequired - * * @return JSONResponse */ - public function createCodes() { + #[NoAdminRequired] + #[PasswordConfirmationRequired] + public function createCodes(): JSONResponse { $user = $this->userSession->getUser(); $codes = $this->storage->createCodes($user); return new JSONResponse([ diff --git a/apps/twofactor_backupcodes/lib/Db/BackupCode.php b/apps/twofactor_backupcodes/lib/Db/BackupCode.php index 8b173890e3c..252b9b77faa 100644 --- a/apps/twofactor_backupcodes/lib/Db/BackupCode.php +++ b/apps/twofactor_backupcodes/lib/Db/BackupCode.php @@ -1,26 +1,11 @@ <?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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Db; use OCP\AppFramework\Db\Entity; diff --git a/apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php b/apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php index 84972a5171a..fbbc3d0403c 100644 --- a/apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php +++ b/apps/twofactor_backupcodes/lib/Db/BackupCodeMapper.php @@ -1,28 +1,11 @@ <?php + +declare(strict_types=1); + /** - * - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Db; use OCP\AppFramework\Db\QBMapper; @@ -42,7 +25,7 @@ class BackupCodeMapper extends QBMapper { * @param IUser $user * @return BackupCode[] */ - public function getBackupCodes(IUser $user) { + public function getBackupCodes(IUser $user): array { /* @var IQueryBuilder $qb */ $qb = $this->db->getQueryBuilder(); @@ -56,14 +39,14 @@ class BackupCodeMapper extends QBMapper { /** * @param IUser $user */ - public function deleteCodes(IUser $user) { + public function deleteCodes(IUser $user): void { $this->deleteCodesByUserId($user->getUID()); } /** * @param string $uid */ - public function deleteCodesByUserId($uid) { + public function deleteCodesByUserId(string $uid): void { /* @var IQueryBuilder $qb */ $qb = $this->db->getQueryBuilder(); diff --git a/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php b/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php index d1e5e29d4b5..90dc6206f71 100644 --- a/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php +++ b/apps/twofactor_backupcodes/lib/Event/CodesGenerated.php @@ -3,27 +3,9 @@ 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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Event; use OCP\EventDispatcher\Event; @@ -31,12 +13,10 @@ use OCP\IUser; class CodesGenerated extends Event { - /** @var IUser */ - private $user; - - public function __construct(IUser $user) { + public function __construct( + private IUser $user, + ) { parent::__construct(); - $this->user = $user; } /** diff --git a/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php b/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php index d7240677b6c..b52f1f2c098 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php +++ b/apps/twofactor_backupcodes/lib/Listener/ActivityPublisher.php @@ -3,27 +3,9 @@ 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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Listener; use BadMethodCallException; @@ -31,20 +13,14 @@ use OCA\TwoFactorBackupCodes\Event\CodesGenerated; use OCP\Activity\IManager; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\ILogger; +use Psr\Log\LoggerInterface; +/** @template-implements IEventListener<CodesGenerated> */ class ActivityPublisher implements IEventListener { - - /** @var IManager */ - private $activityManager; - - /** @var ILogger */ - private $logger; - - public function __construct(IManager $activityManager, - ILogger $logger) { - $this->activityManager = $activityManager; - $this->logger = $logger; + public function __construct( + private IManager $activityManager, + private LoggerInterface $logger, + ) { } /** @@ -61,8 +37,7 @@ class ActivityPublisher implements IEventListener { 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->logger->error('Could not publish backup code creation activity', ['exception' => $e]); } } } diff --git a/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php b/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php index 84c94a7b0c8..46bb1583004 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php +++ b/apps/twofactor_backupcodes/lib/Listener/ClearNotifications.php @@ -3,28 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; @@ -32,13 +13,12 @@ use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\Notification\IManager; +/** @template-implements IEventListener<CodesGenerated> */ class ClearNotifications implements IEventListener { - /** @var IManager */ - private $manager; - - public function __construct(IManager $manager) { - $this->manager = $manager; + public function __construct( + private IManager $manager, + ) { } public function handle(Event $event): void { diff --git a/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php b/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php index f3242aa8c8a..a8d51e55c1b 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php +++ b/apps/twofactor_backupcodes/lib/Listener/ProviderDisabled.php @@ -3,53 +3,29 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob; use OCP\Authentication\TwoFactorAuth\IRegistry; -use OCP\Authentication\TwoFactorAuth\RegistryEvent; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserUnregistered; use OCP\BackgroundJob\IJobList; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; +/** @template-implements IEventListener<TwoFactorProviderForUserUnregistered> */ class ProviderDisabled implements IEventListener { - /** @var IRegistry */ - private $registry; - - /** @var IJobList */ - private $jobList; - - public function __construct(IRegistry $registry, - IJobList $jobList) { - $this->registry = $registry; - $this->jobList = $jobList; + public function __construct( + private IRegistry $registry, + private IJobList $jobList, + ) { } public function handle(Event $event): void { - if (!($event instanceof RegistryEvent)) { + if (!($event instanceof TwoFactorProviderForUserUnregistered)) { return; } diff --git a/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php b/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php index d43af2e4590..4ec510e7194 100644 --- a/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php +++ b/apps/twofactor_backupcodes/lib/Listener/ProviderEnabled.php @@ -3,53 +3,29 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\BackgroundJob\RememberBackupCodesJob; use OCP\Authentication\TwoFactorAuth\IRegistry; -use OCP\Authentication\TwoFactorAuth\RegistryEvent; +use OCP\Authentication\TwoFactorAuth\TwoFactorProviderForUserRegistered; use OCP\BackgroundJob\IJobList; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; +/** @template-implements IEventListener<TwoFactorProviderForUserRegistered> */ class ProviderEnabled implements IEventListener { - /** @var IRegistry */ - private $registry; - - /** @var IJobList */ - private $jobList; - - public function __construct(IRegistry $registry, - IJobList $jobList) { - $this->registry = $registry; - $this->jobList = $jobList; + public function __construct( + private IRegistry $registry, + private IJobList $jobList, + ) { } public function handle(Event $event): void { - if (!($event instanceof RegistryEvent)) { + if (!($event instanceof TwoFactorProviderForUserRegistered)) { return; } diff --git a/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php b/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php index 011950a0979..1cb07bd9805 100644 --- a/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php +++ b/apps/twofactor_backupcodes/lib/Listener/RegistryUpdater.php @@ -3,27 +3,9 @@ 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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Listener; use OCA\TwoFactorBackupCodes\Event\CodesGenerated; @@ -32,17 +14,13 @@ use OCP\Authentication\TwoFactorAuth\IRegistry; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; +/** @template-implements IEventListener<CodesGenerated> */ class RegistryUpdater implements IEventListener { - /** @var IRegistry */ - private $registry; - - /** @var BackupCodesProvider */ - private $provider; - - public function __construct(IRegistry $registry, BackupCodesProvider $provider) { - $this->registry = $registry; - $this->provider = $provider; + public function __construct( + private IRegistry $registry, + private BackupCodesProvider $provider, + ) { } public function handle(Event $event): void { diff --git a/apps/twofactor_backupcodes/lib/Listener/UserDeleted.php b/apps/twofactor_backupcodes/lib/Listener/UserDeleted.php new file mode 100644 index 00000000000..72fd504b338 --- /dev/null +++ b/apps/twofactor_backupcodes/lib/Listener/UserDeleted.php @@ -0,0 +1,31 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OCA\TwoFactorBackupCodes\Listener; + +use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\User\Events\UserDeletedEvent; + +/** @template-implements IEventListener<UserDeletedEvent> */ +class UserDeleted implements IEventListener { + + public function __construct( + private BackupCodeMapper $backupCodeMapper, + ) { + } + + public function handle(Event $event): void { + if (!($event instanceof UserDeletedEvent)) { + return; + } + + $this->backupCodeMapper->deleteCodes($event->getUser()); + } +} diff --git a/apps/twofactor_backupcodes/lib/Migration/CheckBackupCodes.php b/apps/twofactor_backupcodes/lib/Migration/CheckBackupCodes.php index e43a99b9e0a..9c0c676134b 100644 --- a/apps/twofactor_backupcodes/lib/Migration/CheckBackupCodes.php +++ b/apps/twofactor_backupcodes/lib/Migration/CheckBackupCodes.php @@ -3,27 +3,9 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Migration; use OCP\BackgroundJob\IJobList; @@ -32,11 +14,9 @@ use OCP\Migration\IRepairStep; class CheckBackupCodes implements IRepairStep { - /** @var IJobList */ - private $jobList; - - public function __construct(IJobList $jobList) { - $this->jobList = $jobList; + public function __construct( + private IJobList $jobList, + ) { } public function getName(): string { diff --git a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170607104347.php b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170607104347.php index 68f0d5b3421..ce752541bac 100644 --- a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170607104347.php +++ b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170607104347.php @@ -1,28 +1,11 @@ <?php + +declare(strict_types=1); + /** - * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com> - * - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Migration; use Doctrine\DBAL\Types\Types; diff --git a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170607113030.php b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170607113030.php index 771c74853d1..bed733cd413 100644 --- a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170607113030.php +++ b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170607113030.php @@ -1,27 +1,11 @@ <?php + +declare(strict_types=1); + /** - * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com> - * - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Migration; use OCP\DB\ISchemaWrapper; @@ -32,14 +16,12 @@ use OCP\Migration\SimpleMigrationStep; class Version1002Date20170607113030 extends SimpleMigrationStep { - /** @var IDBConnection */ - protected $connection; - /** * @param IDBConnection $connection */ - public function __construct(IDBConnection $connection) { - $this->connection = $connection; + public function __construct( + protected IDBConnection $connection, + ) { } /** diff --git a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170919123342.php b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170919123342.php index dc42ad2545c..2ca390e6edd 100644 --- a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170919123342.php +++ b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170919123342.php @@ -1,28 +1,11 @@ <?php + +declare(strict_types=1); + /** - * @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com> - * - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Migration; use Doctrine\DBAL\Types\Type; diff --git a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170926101419.php b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170926101419.php index f8df9e69844..d19fda49182 100644 --- a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170926101419.php +++ b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20170926101419.php @@ -1,25 +1,10 @@ <?php + +declare(strict_types=1); + /** - * - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\TwoFactorBackupCodes\Migration; @@ -32,7 +17,7 @@ class Version1002Date20170926101419 extends BigIntMigration { /** * @return array Returns an array with the following structure - * ['table1' => ['column1', 'column2'], ...] + * ['table1' => ['column1', 'column2'], ...] * @since 13.0.0 */ protected function getColumnsByTable() { diff --git a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20180821043638.php b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20180821043638.php index 1138bcd6bd2..04d6d58c783 100644 --- a/apps/twofactor_backupcodes/lib/Migration/Version1002Date20180821043638.php +++ b/apps/twofactor_backupcodes/lib/Migration/Version1002Date20180821043638.php @@ -3,28 +3,9 @@ declare(strict_types=1); /** - * - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Migration; use Closure; diff --git a/apps/twofactor_backupcodes/lib/Notifications/Notifier.php b/apps/twofactor_backupcodes/lib/Notifications/Notifier.php index bc5f69a1f98..e8144f52a56 100644 --- a/apps/twofactor_backupcodes/lib/Notifications/Notifier.php +++ b/apps/twofactor_backupcodes/lib/Notifications/Notifier.php @@ -3,47 +3,23 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Jan-Christoph Borchardt <hey@jancborchardt.net> - * @author Joas Schilling <coding@schilljs.com> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Notifications; use OCP\IURLGenerator; use OCP\L10N\IFactory; use OCP\Notification\INotification; use OCP\Notification\INotifier; +use OCP\Notification\UnknownNotificationException; class Notifier implements INotifier { - /** @var IFactory */ - private $factory; - - /** @var IURLGenerator */ - private $url; - - public function __construct(IFactory $factory, IURLGenerator $url) { - $this->factory = $factory; - $this->url = $url; + public function __construct( + private IFactory $factory, + private IURLGenerator $url, + ) { } /** @@ -69,7 +45,7 @@ class Notifier implements INotifier { public function prepare(INotification $notification, string $languageCode): INotification { if ($notification->getApp() !== 'twofactor_backupcodes') { // Not my app => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } // Read the language from the notification @@ -91,7 +67,7 @@ class Notifier implements INotifier { default: // Unknown subject => Unknown notification => throw - throw new \InvalidArgumentException(); + throw new UnknownNotificationException(); } } } diff --git a/apps/twofactor_backupcodes/lib/Provider/BackupCodesProvider.php b/apps/twofactor_backupcodes/lib/Provider/BackupCodesProvider.php index 9b7ce1ed7de..c521329e203 100644 --- a/apps/twofactor_backupcodes/lib/Provider/BackupCodesProvider.php +++ b/apps/twofactor_backupcodes/lib/Provider/BackupCodesProvider.php @@ -3,74 +3,32 @@ declare(strict_types=1); /** - * - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Provider; use OC\App\AppManager; use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage; use OCA\TwoFactorBackupCodes\Settings\Personal; +use OCP\AppFramework\Services\IInitialState; +use OCP\Authentication\TwoFactorAuth\IDeactivatableByAdmin; use OCP\Authentication\TwoFactorAuth\IPersonalProviderSettings; -use OCP\Authentication\TwoFactorAuth\IProvider; use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings; -use OCP\IInitialStateService; use OCP\IL10N; use OCP\IUser; -use OCP\Template; - -class BackupCodesProvider implements IProvider, IProvidesPersonalSettings { - - /** @var string */ - private $appName; - - /** @var BackupCodeStorage */ - private $storage; - - /** @var IL10N */ - private $l10n; - - /** @var AppManager */ - private $appManager; - /** @var IInitialStateService */ - private $initialStateService; - - /** - * @param string $appName - * @param BackupCodeStorage $storage - * @param IL10N $l10n - * @param AppManager $appManager - */ - public function __construct(string $appName, - BackupCodeStorage $storage, - IL10N $l10n, - AppManager $appManager, - IInitialStateService $initialStateService) { - $this->appName = $appName; - $this->l10n = $l10n; - $this->storage = $storage; - $this->appManager = $appManager; - $this->initialStateService = $initialStateService; +use OCP\Template\ITemplate; +use OCP\Template\ITemplateManager; + +class BackupCodesProvider implements IDeactivatableByAdmin, IProvidesPersonalSettings { + public function __construct( + private string $appName, + private BackupCodeStorage $storage, + private IL10N $l10n, + private AppManager $appManager, + private IInitialState $initialState, + private ITemplateManager $templateManager, + ) { } /** @@ -104,10 +62,10 @@ class BackupCodesProvider implements IProvider, IProvidesPersonalSettings { * Get the template for rending the 2FA provider view * * @param IUser $user - * @return Template + * @return ITemplate */ - public function getTemplate(IUser $user): Template { - return new Template('twofactor_backupcodes', 'challenge'); + public function getTemplate(IUser $user): ITemplate { + return $this->templateManager->getTemplate('twofactor_backupcodes', 'challenge'); } /** @@ -162,7 +120,11 @@ class BackupCodesProvider implements IProvider, IProvidesPersonalSettings { */ public function getPersonalSettings(IUser $user): IPersonalProviderSettings { $state = $this->storage->getBackupCodesState($user); - $this->initialStateService->provideInitialState($this->appName, 'state', $state); + $this->initialState->provideInitialState('state', $state); return new Personal(); } + + public function disableFor(IUser $user): void { + $this->storage->deleteCodes($user); + } } diff --git a/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php b/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php index d85e00c0a8c..7dd6b3949e2 100644 --- a/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php +++ b/apps/twofactor_backupcodes/lib/Service/BackupCodeStorage.php @@ -1,28 +1,11 @@ <?php + +declare(strict_types=1); + /** - * - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ - namespace OCA\TwoFactorBackupCodes\Service; use OCA\TwoFactorBackupCodes\Db\BackupCode; @@ -36,33 +19,20 @@ use OCP\Security\ISecureRandom; class BackupCodeStorage { private static $CODE_LENGTH = 16; - /** @var BackupCodeMapper */ - private $mapper; - - /** @var IHasher */ - private $hasher; - - /** @var ISecureRandom */ - private $random; - - /** @var IEventDispatcher */ - private $eventDispatcher; - - public function __construct(BackupCodeMapper $mapper, - ISecureRandom $random, - IHasher $hasher, - IEventDispatcher $eventDispatcher) { - $this->mapper = $mapper; - $this->hasher = $hasher; - $this->random = $random; - $this->eventDispatcher = $eventDispatcher; + public function __construct( + private BackupCodeMapper $mapper, + private ISecureRandom $random, + private IHasher $hasher, + private IEventDispatcher $eventDispatcher, + ) { } /** * @param IUser $user + * @param int $number * @return string[] */ - public function createCodes(IUser $user, $number = 10) { + public function createCodes(IUser $user, int $number = 10): array { $result = []; // Delete existing ones @@ -90,7 +60,7 @@ class BackupCodeStorage { * @param IUser $user * @return bool */ - public function hasBackupCodes(IUser $user) { + public function hasBackupCodes(IUser $user): bool { $codes = $this->mapper->getBackupCodes($user); return count($codes) > 0; } @@ -99,12 +69,12 @@ class BackupCodeStorage { * @param IUser $user * @return array */ - public function getBackupCodesState(IUser $user) { + public function getBackupCodesState(IUser $user): array { $codes = $this->mapper->getBackupCodes($user); $total = count($codes); $used = 0; - array_walk($codes, function (BackupCode $code) use (&$used) { - if (1 === (int)$code->getUsed()) { + array_walk($codes, function (BackupCode $code) use (&$used): void { + if ((int)$code->getUsed() === 1) { $used++; } }); @@ -120,11 +90,11 @@ class BackupCodeStorage { * @param string $code * @return bool */ - public function validateCode(IUser $user, $code) { + public function validateCode(IUser $user, string $code): bool { $dbCodes = $this->mapper->getBackupCodes($user); foreach ($dbCodes as $dbCode) { - if (0 === (int)$dbCode->getUsed() && $this->hasher->verify($code, $dbCode->getCode())) { + if ((int)$dbCode->getUsed() === 0 && $this->hasher->verify($code, $dbCode->getCode())) { $dbCode->setUsed(1); $this->mapper->update($dbCode); return true; @@ -132,4 +102,8 @@ class BackupCodeStorage { } return false; } + + public function deleteCodes(IUser $user): void { + $this->mapper->deleteCodes($user); + } } diff --git a/apps/twofactor_backupcodes/lib/Settings/Personal.php b/apps/twofactor_backupcodes/lib/Settings/Personal.php index 062016d0d05..e03c3d303db 100644 --- a/apps/twofactor_backupcodes/lib/Settings/Personal.php +++ b/apps/twofactor_backupcodes/lib/Settings/Personal.php @@ -3,36 +3,19 @@ declare(strict_types=1); /** - * - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OCA\TwoFactorBackupCodes\Settings; use OCP\Authentication\TwoFactorAuth\IPersonalProviderSettings; -use OCP\Template; +use OCP\Server; +use OCP\Template\ITemplate; +use OCP\Template\ITemplateManager; class Personal implements IPersonalProviderSettings { - public function getBody(): Template { - return new Template('twofactor_backupcodes', 'personal'); + public function getBody(): ITemplate { + return Server::get(ITemplateManager::class)->getTemplate('twofactor_backupcodes', 'personal'); } } |