Completed
Pull Request — master (#92)
by Christoph
02:02
created

AProvider::isTwoFactorAuthEnabledForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @author Christoph Wurst <[email protected]>
7
 *
8
 * Nextcloud - Two-factor Gateway
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\TwoFactorGateway\Provider;
25
26
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
27
use OCA\TwoFactorGateway\Service\StateStorage;
28
use OCP\Authentication\TwoFactorAuth\IProvider;
29
use OCP\IL10N;
30
use OCP\ISession;
31
use OCP\IUser;
32
use OCP\Security\ISecureRandom;
33
use OCP\Template;
34
35
abstract class AProvider implements IProvider {
36
37
	const STATE_DISABLED = 0;
38
	const STATE_START_VERIFICATION = 1;
39
	const STATE_VERIFYING = 2;
40
	const STATE_ENABLED = 3;
41
42
	/** @var string */
43
	protected $gatewayId;
44
45
	/** @var IGateway */
46
	protected $gateway;
47
48
	/** @var StateStorage */
49
	protected $stateStorage;
50
51
	/** @var ISession */
52
	protected $session;
53
54
	/** @var ISecureRandom */
55
	protected $secureRandom;
56
57
	/** @var IL10N */
58
	protected $l10n;
59
60
	private function getSessionKey() {
61
		return "twofactor_gateway_$this->gatewayId_secret";
0 ignored issues
show
Bug introduced by
The property gatewayId_secret does not exist on OCA\TwoFactorGateway\Provider\AProvider. Did you mean gatewayId?
Loading history...
62
	}
63
64
	public function __construct(string $gatewayId,
65
								IGateway $gateway,
66
								StateStorage $stateStorage,
67
								ISession $session,
68
								ISecureRandom $secureRandom,
69
								IL10N $l10n) {
70
		$this->gateway = $gateway;
71
		$this->gatewayId = $gatewayId;
72
		$this->stateStorage = $stateStorage;
73
		$this->session = $session;
74
		$this->secureRandom = $secureRandom;
75
		$this->l10n = $l10n;
76
	}
77
78
	/**
79
	 * Get unique identifier of this 2FA provider
80
	 */
81
	public function getId(): string {
82
		return "gateway_$this->gatewayId";
83
	}
84
85
	private function getSecret(): string {
86
		if ($this->session->exists($this->getSessionKey())) {
87
			return $this->session->get($this->getSessionKey());
88
		}
89
90
		$secret = $this->secureRandom->generate(6, ISecureRandom::CHAR_DIGITS);
91
		$this->session->set($this->getSessionKey(), $secret);
92
93
		return $secret;
94
	}
95
96
	/**
97
	 * Get the template for rending the 2FA provider view
98
	 */
99
	public function getTemplate(IUser $user): Template {
100
		$secret = $this->getSecret();
101
102
		try {
103
			$identifier = $this->stateStorage->get($user)->getIdentifier();
104
			$this->gateway->send(
105
				$user,
106
				$identifier,
107
				$this->l10n->t('%s is your Nextcloud authentication code', [
108
					$secret
109
				])
110
			);
111
		} catch (SmsTransmissionException $ex) {
0 ignored issues
show
Bug introduced by
The type OCA\TwoFactorGateway\Pro...msTransmissionException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
112
			return new Template('twofactor_gateway', 'error');
113
		}
114
115
		$tmpl = new Template('twofactor_gateway', 'challenge');
116
		$tmpl->assign('phone', PhoneNumberMask::maskNumber($identifier));
0 ignored issues
show
Bug introduced by
The type OCA\TwoFactorGateway\Provider\PhoneNumberMask was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
117
		return $tmpl;
118
	}
119
120
	/**
121
	 * Verify the given challenge
122
	 */
123
	public function verifyChallenge(IUser $user, string $challenge): bool {
124
		$valid = $this->session->exists($this->getSessionKey())
125
			&& $this->session->get($this->getSessionKey()) === $challenge;
126
127
		if ($valid) {
128
			$this->session->remove($this->getSessionKey());
129
		}
130
131
		return $valid;
132
	}
133
134
	/**
135
	 * Decides whether 2FA is enabled for the given user
136
	 */
137
	public function isTwoFactorAuthEnabledForUser(IUser $user): bool {
138
		return $this->stateStorage->get($user)->getState() === self::STATE_ENABLED;
139
	}
140
141
}
142