Completed
Push — master ( cd631e...db337b )
by Lukas
21:56 queued 06:01
created

BackupCodesProvider::isActive()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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