Completed
Push — master ( 8ede3f...f9e201 )
by Roeland
14:09 queued 10s
created

BackupCodesProvider   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 119
rs 10
c 0
b 0
f 0
wmc 12
lcom 3
cbo 5

9 Methods

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