BackupCodesProvider.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\TwoFactorBackupCodes\Provider;
  27. use OC\App\AppManager;
  28. use OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
  29. use OCA\TwoFactorBackupCodes\Settings\Personal;
  30. use OCP\Authentication\TwoFactorAuth\IDeactivatableByAdmin;
  31. use OCP\Authentication\TwoFactorAuth\IPersonalProviderSettings;
  32. use OCP\Authentication\TwoFactorAuth\IProvidesPersonalSettings;
  33. use OCP\IInitialStateService;
  34. use OCP\IL10N;
  35. use OCP\IUser;
  36. use OCP\Template;
  37. class BackupCodesProvider implements IDeactivatableByAdmin, IProvidesPersonalSettings {
  38. /** @var string */
  39. private $appName;
  40. /** @var BackupCodeStorage */
  41. private $storage;
  42. /** @var IL10N */
  43. private $l10n;
  44. /** @var AppManager */
  45. private $appManager;
  46. /** @var IInitialStateService */
  47. private $initialStateService;
  48. /**
  49. * @param string $appName
  50. * @param BackupCodeStorage $storage
  51. * @param IL10N $l10n
  52. * @param AppManager $appManager
  53. */
  54. public function __construct(string $appName,
  55. BackupCodeStorage $storage,
  56. IL10N $l10n,
  57. AppManager $appManager,
  58. IInitialStateService $initialStateService) {
  59. $this->appName = $appName;
  60. $this->l10n = $l10n;
  61. $this->storage = $storage;
  62. $this->appManager = $appManager;
  63. $this->initialStateService = $initialStateService;
  64. }
  65. /**
  66. * Get unique identifier of this 2FA provider
  67. *
  68. * @return string
  69. */
  70. public function getId(): string {
  71. return 'backup_codes';
  72. }
  73. /**
  74. * Get the display name for selecting the 2FA provider
  75. *
  76. * @return string
  77. */
  78. public function getDisplayName(): string {
  79. return $this->l10n->t('Backup code');
  80. }
  81. /**
  82. * Get the description for selecting the 2FA provider
  83. *
  84. * @return string
  85. */
  86. public function getDescription(): string {
  87. return $this->l10n->t('Use backup code');
  88. }
  89. /**
  90. * Get the template for rending the 2FA provider view
  91. *
  92. * @param IUser $user
  93. * @return Template
  94. */
  95. public function getTemplate(IUser $user): Template {
  96. return new Template('twofactor_backupcodes', 'challenge');
  97. }
  98. /**
  99. * Verify the given challenge
  100. *
  101. * @param IUser $user
  102. * @param string $challenge
  103. * @return bool
  104. */
  105. public function verifyChallenge(IUser $user, string $challenge): bool {
  106. return $this->storage->validateCode($user, $challenge);
  107. }
  108. /**
  109. * Decides whether 2FA is enabled for the given user
  110. *
  111. * @param IUser $user
  112. * @return boolean
  113. */
  114. public function isTwoFactorAuthEnabledForUser(IUser $user): bool {
  115. return $this->storage->hasBackupCodes($user);
  116. }
  117. /**
  118. * Determine whether backup codes should be active or not
  119. *
  120. * Backup codes only make sense if at least one 2FA provider is active,
  121. * hence this method checks all enabled apps on whether they provide 2FA
  122. * functionality or not. If there's at least one app, backup codes are
  123. * enabled on the personal settings page.
  124. *
  125. * @param IUser $user
  126. * @return boolean
  127. */
  128. public function isActive(IUser $user): bool {
  129. $appIds = array_filter($this->appManager->getEnabledAppsForUser($user), function ($appId) {
  130. return $appId !== $this->appName;
  131. });
  132. foreach ($appIds as $appId) {
  133. $info = $this->appManager->getAppInfo($appId);
  134. if (isset($info['two-factor-providers']) && count($info['two-factor-providers']) > 0) {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * @param IUser $user
  142. *
  143. * @return IPersonalProviderSettings
  144. */
  145. public function getPersonalSettings(IUser $user): IPersonalProviderSettings {
  146. $state = $this->storage->getBackupCodesState($user);
  147. $this->initialStateService->provideInitialState($this->appName, 'state', $state);
  148. return new Personal();
  149. }
  150. public function disableFor(IUser $user) {
  151. $this->storage->deleteCodes($user);
  152. }
  153. }