|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* SPDX-FileCopyrightText: 2018 Christoph Wurst <[email protected]> |
|
5
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace OCA\TwoFactorGateway\Controller; |
|
9
|
|
|
|
|
10
|
|
|
use OCA\TwoFactorGateway\Exception\VerificationException; |
|
11
|
|
|
use OCA\TwoFactorGateway\Provider\Gateway\Factory as GatewayFactory; |
|
12
|
|
|
use OCA\TwoFactorGateway\ResponseDefinitions; |
|
13
|
|
|
use OCA\TwoFactorGateway\Service\SetupService; |
|
14
|
|
|
use OCP\AppFramework\Http; |
|
15
|
|
|
use OCP\AppFramework\Http\Attribute\ApiRoute; |
|
16
|
|
|
use OCP\AppFramework\Http\Attribute\NoAdminRequired; |
|
17
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
18
|
|
|
use OCP\AppFramework\OCSController; |
|
19
|
|
|
use OCP\IRequest; |
|
20
|
|
|
use OCP\IUserSession; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @psalm-import-type TwoFactorGatewayState from ResponseDefinitions |
|
24
|
|
|
*/ |
|
25
|
|
|
class SettingsController extends OCSController { |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
IRequest $request, |
|
29
|
|
|
private IUserSession $userSession, |
|
30
|
|
|
private SetupService $setup, |
|
31
|
|
|
private GatewayFactory $gatewayFactory, |
|
32
|
|
|
) { |
|
33
|
|
|
parent::__construct('twofactor_gateway', $request); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Check if the gateway was configured |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $gateway The gateway name |
|
40
|
|
|
* @return JSONResponse<Http::STATUS_OK, TwoFactorGatewayState, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}>|JSONResponse<Http::STATUS_SERVICE_UNAVAILABLE, array{}, array{}> |
|
41
|
|
|
* |
|
42
|
|
|
* 200: OK |
|
43
|
|
|
* 400: User not found |
|
44
|
|
|
* 503: Gateway wasn't configured yed |
|
45
|
|
|
*/ |
|
46
|
|
|
#[NoAdminRequired] |
|
47
|
|
|
#[ApiRoute(verb: 'GET', url: '/settings/{gateway}/verification')] |
|
48
|
|
|
public function getVerificationState(string $gateway): JSONResponse { |
|
49
|
|
|
$user = $this->userSession->getUser(); |
|
50
|
|
|
|
|
51
|
|
|
if (is_null($user)) { |
|
52
|
|
|
return new JSONResponse(['message' => 'User not found'], Http::STATUS_BAD_REQUEST); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (!$this->gatewayFactory->get($gateway)->isComplete()) { |
|
56
|
|
|
return new JSONResponse([], Http::STATUS_SERVICE_UNAVAILABLE); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return new JSONResponse($this->setup->getState($user, $gateway)->jsonSerialize()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Send out confirmation message and save current identifier in user settings |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $gateway The gateway type |
|
66
|
|
|
* @param string $identifier The identifier to use this gateway |
|
67
|
|
|
* |
|
68
|
|
|
* @return JSONResponse<Http::STATUS_OK, array{phoneNumber: ?string}, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}> |
|
69
|
|
|
* |
|
70
|
|
|
* 200: OK |
|
71
|
|
|
* 400: User not found |
|
72
|
|
|
*/ |
|
73
|
|
|
#[NoAdminRequired] |
|
74
|
|
|
#[ApiRoute(verb: 'POST', url: '/settings/{gateway}/verification/start')] |
|
75
|
|
|
public function startVerification(string $gateway, string $identifier): JSONResponse { |
|
76
|
|
|
$user = $this->userSession->getUser(); |
|
77
|
|
|
|
|
78
|
|
|
if (is_null($user)) { |
|
79
|
|
|
return new JSONResponse(['message' => 'User not found'], Http::STATUS_BAD_REQUEST); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
$state = $this->setup->startSetup($user, $gateway, $identifier); |
|
84
|
|
|
} catch (VerificationException $e) { |
|
85
|
|
|
return new JSONResponse(['message' => $e->getMessage()], Http::STATUS_BAD_REQUEST); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return new JSONResponse([ |
|
89
|
|
|
'phoneNumber' => $state->getIdentifier(), |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Send out confirmation message and save current identifier in user settings |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $gateway The gateway type |
|
97
|
|
|
* @param string $verificationCode Verification code |
|
98
|
|
|
* |
|
99
|
|
|
* @return JSONResponse<Http::STATUS_OK, array{}, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}> |
|
100
|
|
|
* |
|
101
|
|
|
* 200: OK |
|
102
|
|
|
* 400: User not found |
|
103
|
|
|
*/ |
|
104
|
|
|
#[NoAdminRequired] |
|
105
|
|
|
#[ApiRoute(verb: 'POST', url: '/settings/{gateway}/verification/finish')] |
|
106
|
|
|
public function finishVerification(string $gateway, string $verificationCode): JSONResponse { |
|
107
|
|
|
$user = $this->userSession->getUser(); |
|
108
|
|
|
|
|
109
|
|
|
if (is_null($user)) { |
|
110
|
|
|
return new JSONResponse(['message' => 'User not found'], Http::STATUS_BAD_REQUEST); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
try { |
|
114
|
|
|
$this->setup->finishSetup($user, $gateway, $verificationCode); |
|
115
|
|
|
} catch (VerificationException) { |
|
116
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return new JSONResponse([]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Disable a gateway |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $gateway The gateway name |
|
126
|
|
|
* @return JSONResponse<Http::STATUS_OK, array{}, array{}>|JSONResponse<Http::STATUS_BAD_REQUEST, array{message: string}, array{}>> |
|
127
|
|
|
* |
|
128
|
|
|
* 200: OK |
|
129
|
|
|
* 400: User not found |
|
130
|
|
|
*/ |
|
131
|
|
|
#[NoAdminRequired] |
|
132
|
|
|
#[ApiRoute(verb: 'DELETE', url: '/settings/{gateway}/verification')] |
|
133
|
|
|
public function revokeVerification(string $gateway): JSONResponse { |
|
134
|
|
|
$user = $this->userSession->getUser(); |
|
135
|
|
|
|
|
136
|
|
|
if (is_null($user)) { |
|
137
|
|
|
return new JSONResponse(['message' => 'User not found'], Http::STATUS_BAD_REQUEST); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return new JSONResponse($this->setup->disable($user, $gateway)); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|