You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BackupCodesProvider.php 3.5KB

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