1
0

ProviderLoader.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Authentication\TwoFactorAuth;
  8. use Exception;
  9. use OC;
  10. use OC_App;
  11. use OCP\App\IAppManager;
  12. use OCP\AppFramework\QueryException;
  13. use OCP\Authentication\TwoFactorAuth\IProvider;
  14. use OCP\IUser;
  15. class ProviderLoader {
  16. public const BACKUP_CODES_APP_ID = 'twofactor_backupcodes';
  17. /** @var IAppManager */
  18. private $appManager;
  19. /** @var OC\AppFramework\Bootstrap\Coordinator */
  20. private $coordinator;
  21. public function __construct(IAppManager $appManager, OC\AppFramework\Bootstrap\Coordinator $coordinator) {
  22. $this->appManager = $appManager;
  23. $this->coordinator = $coordinator;
  24. }
  25. /**
  26. * Get the list of 2FA providers for the given user
  27. *
  28. * @return IProvider[]
  29. * @throws Exception
  30. */
  31. public function getProviders(IUser $user): array {
  32. $allApps = $this->appManager->getEnabledAppsForUser($user);
  33. $providers = [];
  34. foreach ($allApps as $appId) {
  35. $info = $this->appManager->getAppInfo($appId);
  36. if (isset($info['two-factor-providers'])) {
  37. /** @var string[] $providerClasses */
  38. $providerClasses = $info['two-factor-providers'];
  39. foreach ($providerClasses as $class) {
  40. try {
  41. $this->loadTwoFactorApp($appId);
  42. $provider = \OCP\Server::get($class);
  43. $providers[$provider->getId()] = $provider;
  44. } catch (QueryException $exc) {
  45. // Provider class can not be resolved
  46. throw new Exception("Could not load two-factor auth provider $class");
  47. }
  48. }
  49. }
  50. }
  51. $registeredProviders = $this->coordinator->getRegistrationContext()->getTwoFactorProviders();
  52. foreach ($registeredProviders as $provider) {
  53. try {
  54. $this->loadTwoFactorApp($provider->getAppId());
  55. $provider = \OCP\Server::get($provider->getService());
  56. $providers[$provider->getId()] = $provider;
  57. } catch (QueryException $exc) {
  58. // Provider class can not be resolved
  59. throw new Exception('Could not load two-factor auth provider ' . $provider->getService());
  60. }
  61. }
  62. return $providers;
  63. }
  64. /**
  65. * Load an app by ID if it has not been loaded yet
  66. *
  67. * @param string $appId
  68. */
  69. protected function loadTwoFactorApp(string $appId) {
  70. if (!OC_App::isAppLoaded($appId)) {
  71. OC_App::loadApp($appId);
  72. }
  73. }
  74. }