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: 15f5945fab1132d52f2dad2f6d432ef133fa85da (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
<?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\SmsProvider;
use OCA\TwoFactorGateway\Provider\State;
use OCA\TwoFactorGateway\Service\Gateway\IGateway;
use OCP\Authentication\TwoFactorAuth\IRegistry;
use OCP\IUser;
use OCP\Security\ISecureRandom;

class SetupService {

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

	/** @var IGateway */
	private $smsService;

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

	/** @var SmsProvider */
	private $provider;

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

	public function __construct(StateStorage $stateStorage,
								IGateway $smsService,
								ISecureRandom $random,
								SmsProvider $provider,
								IRegistry $providerRegistry) {
		$this->stateStorage = $stateStorage;
		$this->smsService = $smsService;
		$this->random = $random;
		$this->provider = $provider;
		$this->providerRegistry = $providerRegistry;
	}

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

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

		return $identifier;
	}

	/**
	 * Send out confirmation message and save current identifier in user settings
	 */
	public function startSetup(IUser $user, string $identifier): State {
		$verificationNumber = $this->random->generate(6, ISecureRandom::CHAR_DIGITS);
		try {
			$this->smsService->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, $this->smsService->getShortName(), $identifier, $verificationNumber)
		);
	}

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

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

		$this->providerRegistry->enableProviderFor($this->provider, $user);

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

	public function disable(IUser $user): State {
		$this->providerRegistry->disableProviderFor($this->provider, $user);

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

}