Welcome to mirror list, hosted at ThFree Co, Russian Federation.

SetupService.php « Service « lib - github.com/nextcloud/twofactor_gateway.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dd287a311898a8ea8dbe0cb2846d78b6ea269e1c (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php

declare(strict_types=1);

/**
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\TwoFactorGateway\Service;

use Exception;
use OCA\TwoFactorGateway\Exception\IdentifierMissingException;
use OCA\TwoFactorGateway\Exception\SmsTransmissionException;
use OCA\TwoFactorGateway\Exception\VerificationException;
use OCA\TwoFactorGateway\Exception\VerificationTransmissionException;
use OCA\TwoFactorGateway\Provider\Factory;
use OCA\TwoFactorGateway\Service\Gateway\Factory as GatewayFactory;
use OCA\TwoFactorGateway\Provider\State;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use OCP\IUser;
use OCP\Security\ISecureRandom;

class SetupService {

	/** @var StateStorage */
	private $stateStorage;

	/** @var GatewayFactory */
	private $gatewayFactory;

	/** @var Factory */
	private $providerFactory;

	/** @var ISecureRandom */
	private $random;

	/** @var IRegistry */
	private $providerRegistry;

	public function __construct(StateStorage $stateStorage,
								GatewayFactory $gatewayFactory,
								Factory $providerFactory,
								ISecureRandom $random,
								IRegistry $providerRegistry) {
		$this->stateStorage = $stateStorage;
		$this->gatewayFactory = $gatewayFactory;
		$this->providerFactory = $providerFactory;
		$this->random = $random;
		$this->providerRegistry = $providerRegistry;
	}

	public function getState(IUser $user, string $gatewayName): State {
		return $this->stateStorage->get($user, $gatewayName);
	}

	/**
	 * @throws IdentifierMissingException
	 */
	public function getChallengePhoneNumber(IUser $user, string $gatewayName): string {
		$state = $this->stateStorage->get($user, $gatewayName);
		$identifier = $state->getIdentifier();
		if (is_null($identifier)) {
			throw new IdentifierMissingException("verified identifier for $gatewayName is missing");
		}

		return $identifier;
	}

	/**
	 * Send out confirmation message and save current identifier in user settings
	 */
	public function startSetup(IUser $user, string $gatewayName, string $identifier): State {
		$verificationNumber = $this->random->generate(6, ISecureRandom::CHAR_DIGITS);
		$gateway = $this->gatewayFactory->getGateway($gatewayName);
		try {
			$gateway->send($user, $identifier, "$verificationNumber is your Nextcloud verification code.");
		} catch (SmsTransmissionException $ex) {
			throw new VerificationTransmissionException('could not send verification code');
		}

		return $this->stateStorage->persist(
			State::verifying($user, $gatewayName, $identifier, $verificationNumber)
		);
	}

	public function finishSetup(IUser $user, string $gatewayName, string $token): State {
		$state = $this->stateStorage->get($user, $gatewayName);
		if (is_null($state->getVerificationCode())) {
			throw new Exception('no verification code set');
		}

		if ($state->getVerificationCode() !== $token) {
			throw new VerificationException('verification token mismatch');
		}

		$provider = $this->providerFactory->getProvider($gatewayName);
		$this->providerRegistry->enableProviderFor($provider, $user);

		return $this->stateStorage->persist(
			$state->verify()
		);
	}

	public function disable(IUser $user, string $gatewayName): State {
		$provider = $this->providerFactory->getProvider($gatewayName);
		$this->providerRegistry->enableProviderFor($provider, $user);
		$this->providerRegistry->disableProviderFor($provider, $user);


		return $this->stateStorage->persist(
			State::disabled($user, $gatewayName)
		);
	}
}