2016-08-29 13:19:44 -04:00
|
|
|
<?php
|
2019-12-03 13:57:53 -05:00
|
|
|
|
2018-03-26 05:21:47 -04:00
|
|
|
declare(strict_types=1);
|
2016-08-29 13:19:44 -04:00
|
|
|
|
|
|
|
|
/**
|
2024-05-30 14:13:41 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-08-29 13:19:44 -04:00
|
|
|
*/
|
2016-10-03 15:47:33 -04:00
|
|
|
namespace OCA\TwoFactorBackupCodes\Provider;
|
2016-08-29 13:19:44 -04:00
|
|
|
|
2017-05-02 08:40:44 -04:00
|
|
|
use OC\App\AppManager;
|
2016-10-03 15:47:33 -04:00
|
|
|
use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
|
2018-09-26 10:25:18 -04:00
|
|
|
use OCA\TwoFactorBackupCodes\Settings\Personal;
|
2025-08-08 10:30:58 -04:00
|
|
|
use OCP\AppFramework\Services\IInitialState;
|
2021-08-13 09:28:56 -04:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IDeactivatableByAdmin;
|
2018-09-26 10:25:18 -04:00
|
|
|
use OCP\Authentication\TwoFactorAuth\IPersonalProviderSettings;
|
|
|
|
|
use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings;
|
2016-08-29 13:19:44 -04:00
|
|
|
use OCP\IL10N;
|
|
|
|
|
use OCP\IUser;
|
2025-03-03 05:38:39 -05:00
|
|
|
use OCP\Template\ITemplate;
|
|
|
|
|
use OCP\Template\ITemplateManager;
|
2016-08-29 13:19:44 -04:00
|
|
|
|
2021-08-13 09:28:56 -04:00
|
|
|
class BackupCodesProvider implements IDeactivatableByAdmin, IProvidesPersonalSettings {
|
2024-10-18 06:04:22 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private string $appName,
|
|
|
|
|
private BackupCodeStorage $storage,
|
|
|
|
|
private IL10N $l10n,
|
2025-08-08 10:30:58 -04:00
|
|
|
private AppManager $appManager,
|
|
|
|
|
private IInitialState $initialState,
|
2025-03-03 05:38:39 -05:00
|
|
|
private ITemplateManager $templateManager,
|
2024-10-18 06:04:22 -04:00
|
|
|
) {
|
2016-08-29 13:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get unique identifier of this 2FA provider
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2018-03-26 05:21:47 -04:00
|
|
|
public function getId(): string {
|
2016-08-29 13:19:44 -04:00
|
|
|
return 'backup_codes';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the display name for selecting the 2FA provider
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2018-03-26 05:21:47 -04:00
|
|
|
public function getDisplayName(): string {
|
2016-08-29 13:19:44 -04:00
|
|
|
return $this->l10n->t('Backup code');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the description for selecting the 2FA provider
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2018-03-26 05:21:47 -04:00
|
|
|
public function getDescription(): string {
|
2016-08-29 13:19:44 -04:00
|
|
|
return $this->l10n->t('Use backup code');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the template for rending the 2FA provider view
|
|
|
|
|
*
|
|
|
|
|
* @param IUser $user
|
2025-03-03 05:38:39 -05:00
|
|
|
* @return ITemplate
|
2016-08-29 13:19:44 -04:00
|
|
|
*/
|
2025-03-03 05:38:39 -05:00
|
|
|
public function getTemplate(IUser $user): ITemplate {
|
|
|
|
|
return $this->templateManager->getTemplate('twofactor_backupcodes', 'challenge');
|
2016-08-29 13:19:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Verify the given challenge
|
|
|
|
|
*
|
|
|
|
|
* @param IUser $user
|
|
|
|
|
* @param string $challenge
|
2018-03-26 05:21:47 -04:00
|
|
|
* @return bool
|
2016-08-29 13:19:44 -04:00
|
|
|
*/
|
2018-03-26 05:21:47 -04:00
|
|
|
public function verifyChallenge(IUser $user, string $challenge): bool {
|
2016-08-29 13:19:44 -04:00
|
|
|
return $this->storage->validateCode($user, $challenge);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Decides whether 2FA is enabled for the given user
|
|
|
|
|
*
|
|
|
|
|
* @param IUser $user
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2018-03-26 05:21:47 -04:00
|
|
|
public function isTwoFactorAuthEnabledForUser(IUser $user): bool {
|
2016-08-29 13:19:44 -04:00
|
|
|
return $this->storage->hasBackupCodes($user);
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 08:40:44 -04:00
|
|
|
/**
|
|
|
|
|
* Determine whether backup codes should be active or not
|
|
|
|
|
*
|
|
|
|
|
* Backup codes only make sense if at least one 2FA provider is active,
|
|
|
|
|
* hence this method checks all enabled apps on whether they provide 2FA
|
|
|
|
|
* functionality or not. If there's at least one app, backup codes are
|
|
|
|
|
* enabled on the personal settings page.
|
|
|
|
|
*
|
|
|
|
|
* @param IUser $user
|
|
|
|
|
* @return boolean
|
|
|
|
|
*/
|
2018-03-26 05:21:47 -04:00
|
|
|
public function isActive(IUser $user): bool {
|
2020-04-09 07:53:40 -04:00
|
|
|
$appIds = array_filter($this->appManager->getEnabledAppsForUser($user), function ($appId) {
|
2017-05-02 08:40:44 -04:00
|
|
|
return $appId !== $this->appName;
|
|
|
|
|
});
|
|
|
|
|
foreach ($appIds as $appId) {
|
|
|
|
|
$info = $this->appManager->getAppInfo($appId);
|
|
|
|
|
if (isset($info['two-factor-providers']) && count($info['two-factor-providers']) > 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 10:25:18 -04:00
|
|
|
/**
|
|
|
|
|
* @param IUser $user
|
|
|
|
|
*
|
|
|
|
|
* @return IPersonalProviderSettings
|
|
|
|
|
*/
|
|
|
|
|
public function getPersonalSettings(IUser $user): IPersonalProviderSettings {
|
2019-01-09 17:11:19 -05:00
|
|
|
$state = $this->storage->getBackupCodesState($user);
|
2025-08-08 10:30:58 -04:00
|
|
|
$this->initialState->provideInitialState('state', $state);
|
2019-01-17 06:30:47 -05:00
|
|
|
return new Personal();
|
2018-09-26 10:25:18 -04:00
|
|
|
}
|
2021-08-13 09:28:56 -04:00
|
|
|
|
2025-08-08 10:30:58 -04:00
|
|
|
public function disableFor(IUser $user): void {
|
2021-08-13 09:28:56 -04:00
|
|
|
$this->storage->deleteCodes($user);
|
|
|
|
|
}
|
2016-08-29 13:19:44 -04:00
|
|
|
}
|