Passed
Push — master ( 25406e...6c0bb2 )
by Morris
64:29 queued 52:03
created

ProviderLoader::getProviders()   B

Complexity

Conditions 7
Paths 23

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
dl 0
loc 35
rs 8.6346
c 0
b 0
f 0
eloc 22
nc 23
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright 2018 Christoph Wurst <[email protected]>
7
 *
8
 * @author Christoph Wurst <[email protected]>
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
27
namespace OC\Authentication\TwoFactorAuth;
28
29
use Exception;
30
use OC;
31
use OC_App;
32
use OCP\App\IAppManager;
33
use OCP\AppFramework\QueryException;
34
use OCP\Authentication\TwoFactorAuth\IProvider;
35
use OCP\IUser;
36
37
class ProviderLoader {
38
	public const BACKUP_CODES_APP_ID = 'twofactor_backupcodes';
39
40
	/** @var IAppManager */
41
	private $appManager;
42
43
	/** @var OC\AppFramework\Bootstrap\Coordinator */
44
	private $coordinator;
45
46
	public function __construct(IAppManager $appManager, OC\AppFramework\Bootstrap\Coordinator $coordinator) {
47
		$this->appManager = $appManager;
48
		$this->coordinator = $coordinator;
49
	}
50
51
	/**
52
	 * Get the list of 2FA providers for the given user
53
	 *
54
	 * @return IProvider[]
55
	 * @throws Exception
56
	 */
57
	public function getProviders(IUser $user): array {
58
		$allApps = $this->appManager->getEnabledAppsForUser($user);
59
		$providers = [];
60
61
		foreach ($allApps as $appId) {
62
			$info = $this->appManager->getAppInfo($appId);
63
			if (isset($info['two-factor-providers'])) {
64
				/** @var string[] $providerClasses */
65
				$providerClasses = $info['two-factor-providers'];
66
				foreach ($providerClasses as $class) {
67
					try {
68
						$this->loadTwoFactorApp($appId);
69
						$provider = OC::$server->query($class);
70
						$providers[$provider->getId()] = $provider;
71
					} catch (QueryException $exc) {
72
						// Provider class can not be resolved
73
						throw new Exception("Could not load two-factor auth provider $class");
74
					}
75
				}
76
			}
77
		}
78
79
		$registeredProviders = $this->coordinator->getRegistrationContext()->getTwoFactorProviders();
80
		foreach ($registeredProviders as $provider) {
81
			try {
82
				$this->loadTwoFactorApp($provider->getAppId());
83
				$provider = OC::$server->query($provider->getService());
84
				$providers[$provider->getId()] = $provider;
85
			} catch (QueryException $exc) {
86
				// Provider class can not be resolved
87
				throw new Exception('Could not load two-factor auth provider ' . $provider->getService());
88
			}
89
		}
90
91
		return $providers;
92
	}
93
94
	/**
95
	 * Load an app by ID if it has not been loaded yet
96
	 *
97
	 * @param string $appId
98
	 */
99
	protected function loadTwoFactorApp(string $appId) {
100
		if (!OC_App::isAppLoaded($appId)) {
101
			OC_App::loadApp($appId);
102
		}
103
	}
104
}
105