aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Authentication/TwoFactorAuth/ProviderSet.php
blob: 15b82be6decad2d616afde4ef4a29db2930add77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

declare(strict_types=1);

/**
 * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\Authentication\TwoFactorAuth;

use OCA\TwoFactorBackupCodes\Provider\BackupCodesProvider;
use OCP\Authentication\TwoFactorAuth\IProvider;
use function array_filter;

/**
 * Contains all two-factor provider information for the two-factor login challenge
 */
class ProviderSet {
	/** @var IProvider */
	private $providers;

	/** @var bool */
	private $providerMissing;

	/**
	 * @param IProvider[] $providers
	 * @param bool $providerMissing
	 */
	public function __construct(array $providers, bool $providerMissing) {
		$this->providers = [];
		foreach ($providers as $provider) {
			$this->providers[$provider->getId()] = $provider;
		}
		$this->providerMissing = $providerMissing;
	}

	/**
	 * @param string $providerId
	 * @return IProvider|null
	 */
	public function getProvider(string $providerId) {
		return $this->providers[$providerId] ?? null;
	}

	/**
	 * @return IProvider[]
	 */
	public function getProviders(): array {
		return $this->providers;
	}

	/**
	 * @return IProvider[]
	 */
	public function getPrimaryProviders(): array {
		return array_filter($this->providers, function (IProvider $provider) {
			return !($provider instanceof BackupCodesProvider);
		});
	}

	public function isProviderMissing(): bool {
		return $this->providerMissing;
	}
}