SettingsController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 33
dl 0
loc 91
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A startVerification() 0 11 2
A finishVerification() 0 14 3
A getVerificationState() 0 13 3
A revokeVerification() 0 8 2
A __construct() 0 9 1
1
<?php
2
3
/**
4
 * @copyright 2018 Christoph Wurst <[email protected]>
5
 *
6
 * @author 201 Christoph Wurst <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\TwoFactorGateway\Controller;
26
27
use OCA\TwoFactorGateway\Exception\VerificationException;
28
use OCA\TwoFactorGateway\Service\Gateway\Factory as GatewayFactory;
29
use OCA\TwoFactorGateway\Service\SetupService;
30
use OCP\AppFramework\Controller;
31
use OCP\AppFramework\Http;
32
use OCP\AppFramework\Http\JSONResponse;
33
use OCP\IRequest;
34
use OCP\IUserSession;
35
36
class SettingsController extends Controller {
37
38
	/** @var IUserSession */
39
	private $userSession;
40
41
	/** @var SetupService */
42
	private $setup;
43
44
	/** @var GatewayFactory */
45
	private $gatewayFactory;
46
47
	public function __construct(IRequest $request,
48
		IUserSession $userSession,
49
		SetupService $setup,
50
		GatewayFactory $gatewayFactory) {
51
		parent::__construct('twofactor_gateway', $request);
52
53
		$this->userSession = $userSession;
54
		$this->setup = $setup;
55
		$this->gatewayFactory = $gatewayFactory;
56
	}
57
58
	/**
59
	 * @NoAdminRequired
60
	 */
61
	public function getVerificationState(string $gateway): JSONResponse {
62
		$user = $this->userSession->getUser();
63
64
		if (is_null($user)) {
65
			return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
66
		}
67
68
		$gatewayConfig = $this->gatewayFactory->getGateway($gateway)->getConfig();
69
		if (!$gatewayConfig->isComplete()) {
70
			return new JSONResponse(null, Http::STATUS_SERVICE_UNAVAILABLE);
71
		}
72
73
		return new JSONResponse($this->setup->getState($user, $gateway));
74
	}
75
76
	/**
77
	 * @NoAdminRequired
78
	 *
79
	 * @param string $identification
80
	 *
81
	 * @return JSONResponse
82
	 */
83
	public function startVerification(string $gateway, string $identifier): JSONResponse {
84
		$user = $this->userSession->getUser();
85
86
		if (is_null($user)) {
87
			return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
88
		}
89
90
		$state = $this->setup->startSetup($user, $gateway, $identifier);
91
92
		return new JSONResponse([
93
			'phoneNumber' => $state->getIdentifier(),
94
		]);
95
	}
96
97
	/**
98
	 * @NoAdminRequired
99
	 */
100
	public function finishVerification(string $gateway, string $verificationCode): JSONResponse {
101
		$user = $this->userSession->getUser();
102
103
		if (is_null($user)) {
104
			return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
105
		}
106
107
		try {
108
			$this->setup->finishSetup($user, $gateway, $verificationCode);
109
		} catch (VerificationException $ex) {
110
			return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
111
		}
112
113
		return new JSONResponse([]);
114
	}
115
116
	/**
117
	 * @NoAdminRequired
118
	 */
119
	public function revokeVerification(string $gateway): JSONResponse {
120
		$user = $this->userSession->getUser();
121
122
		if (is_null($user)) {
123
			return new JSONResponse(null, Http::STATUS_BAD_REQUEST);
124
		}
125
126
		return new JSONResponse($this->setup->disable($user, $gateway));
127
	}
128
}
129