Completed
Push — master ( 14d226...3a0e06 )
by Joas
10:50
created

BackupCodesProvider::getTemplate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
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 OCA\TwoFactorBackupCodes\Service\BackupCodeStorage;
26
use OCP\Authentication\TwoFactorAuth\IProvider;
27
use OCP\IL10N;
28
use OCP\IUser;
29
use OCP\Template;
30
31
class BackupCodesProvider implements IProvider {
32
33
	/** @var BackupCodeStorage */
34
	private $storage;
35
36
	/** @var IL10N */
37
	private $l10n;
38
39
	public function __construct(BackupCodeStorage $storage, IL10N $l10n) {
40
		$this->l10n = $l10n;
41
		$this->storage = $storage;
42
	}
43
44
	/**
45
	 * Get unique identifier of this 2FA provider
46
	 *
47
	 * @return string
48
	 */
49
	public function getId() {
50
		return 'backup_codes';
51
	}
52
53
	/**
54
	 * Get the display name for selecting the 2FA provider
55
	 *
56
	 * @return string
57
	 */
58
	public function getDisplayName() {
59
		return $this->l10n->t('Backup code');
60
	}
61
62
	/**
63
	 * Get the description for selecting the 2FA provider
64
	 *
65
	 * @return string
66
	 */
67
	public function getDescription() {
68
		return $this->l10n->t('Use backup code');
69
	}
70
71
	/**
72
	 * Get the template for rending the 2FA provider view
73
	 *
74
	 * @param IUser $user
75
	 * @return Template
76
	 */
77
	public function getTemplate(IUser $user) {
78
		$tmpl = new Template('twofactor_backupcodes', 'challenge');
79
		return $tmpl;
80
	}
81
82
	/**
83
	 * Verify the given challenge
84
	 *
85
	 * @param IUser $user
86
	 * @param string $challenge
87
	 */
88
	public function verifyChallenge(IUser $user, $challenge) {
89
		return $this->storage->validateCode($user, $challenge);
90
	}
91
92
	/**
93
	 * Decides whether 2FA is enabled for the given user
94
	 *
95
	 * @param IUser $user
96
	 * @return boolean
97
	 */
98
	public function isTwoFactorAuthEnabledForUser(IUser $user) {
99
		return $this->storage->hasBackupCodes($user);
100
	}
101
102
}
103