ProviderLoader.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Authentication\TwoFactorAuth;
  26. use Exception;
  27. use OC;
  28. use OC_App;
  29. use OCP\App\IAppManager;
  30. use OCP\AppFramework\QueryException;
  31. use OCP\Authentication\TwoFactorAuth\IProvider;
  32. use OCP\IUser;
  33. class ProviderLoader {
  34. public const BACKUP_CODES_APP_ID = 'twofactor_backupcodes';
  35. /** @var IAppManager */
  36. private $appManager;
  37. /** @var OC\AppFramework\Bootstrap\Coordinator */
  38. private $coordinator;
  39. public function __construct(IAppManager $appManager, OC\AppFramework\Bootstrap\Coordinator $coordinator) {
  40. $this->appManager = $appManager;
  41. $this->coordinator = $coordinator;
  42. }
  43. /**
  44. * Get the list of 2FA providers for the given user
  45. *
  46. * @return IProvider[]
  47. * @throws Exception
  48. */
  49. public function getProviders(IUser $user): array {
  50. $allApps = $this->appManager->getEnabledAppsForUser($user);
  51. $providers = [];
  52. foreach ($allApps as $appId) {
  53. $info = $this->appManager->getAppInfo($appId);
  54. if (isset($info['two-factor-providers'])) {
  55. /** @var string[] $providerClasses */
  56. $providerClasses = $info['two-factor-providers'];
  57. foreach ($providerClasses as $class) {
  58. try {
  59. $this->loadTwoFactorApp($appId);
  60. $provider = OC::$server->query($class);
  61. $providers[$provider->getId()] = $provider;
  62. } catch (QueryException $exc) {
  63. // Provider class can not be resolved
  64. throw new Exception("Could not load two-factor auth provider $class");
  65. }
  66. }
  67. }
  68. }
  69. $registeredProviders = $this->coordinator->getRegistrationContext()->getTwoFactorProviders();
  70. foreach ($registeredProviders as $provider) {
  71. try {
  72. $this->loadTwoFactorApp($provider->getAppId());
  73. $provider = OC::$server->query($provider->getService());
  74. $providers[$provider->getId()] = $provider;
  75. } catch (QueryException $exc) {
  76. // Provider class can not be resolved
  77. throw new Exception('Could not load two-factor auth provider ' . $provider->getService());
  78. }
  79. }
  80. return $providers;
  81. }
  82. /**
  83. * Load an app by ID if it has not been loaded yet
  84. *
  85. * @param string $appId
  86. */
  87. protected function loadTwoFactorApp(string $appId) {
  88. if (!OC_App::isAppLoaded($appId)) {
  89. OC_App::loadApp($appId);
  90. }
  91. }
  92. }