|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (c) 2017 Lukas Reschke <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @license GNU AGPL version 3 or any later version |
|
7
|
|
|
* |
|
8
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
10
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
11
|
|
|
* License, or (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* This program is distributed in the hope that it will be useful, |
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16
|
|
|
* GNU Affero General Public License for more details. |
|
17
|
|
|
* |
|
18
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20
|
|
|
* |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace OCA\OAuth2\Controller; |
|
24
|
|
|
|
|
25
|
|
|
use OC\Authentication\Token\DefaultTokenMapper; |
|
26
|
|
|
use OCA\OAuth2\Db\AccessTokenMapper; |
|
27
|
|
|
use OCA\OAuth2\Db\Client; |
|
28
|
|
|
use OCA\OAuth2\Db\ClientMapper; |
|
29
|
|
|
use OCP\AppFramework\Controller; |
|
30
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
31
|
|
|
use OCP\IRequest; |
|
32
|
|
|
use OCP\Security\ISecureRandom; |
|
33
|
|
|
|
|
34
|
|
|
class SettingsController extends Controller { |
|
35
|
|
|
/** @var ClientMapper */ |
|
36
|
|
|
private $clientMapper; |
|
37
|
|
|
/** @var ISecureRandom */ |
|
38
|
|
|
private $secureRandom; |
|
39
|
|
|
/** @var AccessTokenMapper */ |
|
40
|
|
|
private $accessTokenMapper; |
|
41
|
|
|
/** @var DefaultTokenMapper */ |
|
42
|
|
|
private $defaultTokenMapper; |
|
43
|
|
|
|
|
44
|
|
|
const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $appName |
|
48
|
|
|
* @param IRequest $request |
|
49
|
|
|
* @param ClientMapper $clientMapper |
|
50
|
|
|
* @param ISecureRandom $secureRandom |
|
51
|
|
|
* @param AccessTokenMapper $accessTokenMapper |
|
52
|
|
|
* @param DefaultTokenMapper $defaultTokenMapper |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct(string $appName, |
|
55
|
|
|
IRequest $request, |
|
56
|
|
|
ClientMapper $clientMapper, |
|
57
|
|
|
ISecureRandom $secureRandom, |
|
58
|
|
|
AccessTokenMapper $accessTokenMapper, |
|
59
|
|
|
DefaultTokenMapper $defaultTokenMapper |
|
60
|
|
|
) { |
|
61
|
|
|
parent::__construct($appName, $request); |
|
62
|
|
|
$this->secureRandom = $secureRandom; |
|
63
|
|
|
$this->clientMapper = $clientMapper; |
|
64
|
|
|
$this->accessTokenMapper = $accessTokenMapper; |
|
65
|
|
|
$this->defaultTokenMapper = $defaultTokenMapper; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function addClient(string $name, |
|
69
|
|
|
string $redirectUri): JSONResponse { |
|
70
|
|
|
$client = new Client(); |
|
71
|
|
|
$client->setName($name); |
|
72
|
|
|
$client->setRedirectUri($redirectUri); |
|
73
|
|
|
$client->setSecret($this->secureRandom->generate(64, self::validChars)); |
|
74
|
|
|
$client->setClientIdentifier($this->secureRandom->generate(64, self::validChars)); |
|
75
|
|
|
$client = $this->clientMapper->insert($client); |
|
76
|
|
|
|
|
77
|
|
|
$result = [ |
|
78
|
|
|
'id' => $client->getId(), |
|
79
|
|
|
'name' => $client->getName(), |
|
|
|
|
|
|
80
|
|
|
'redirectUri' => $client->getRedirectUri(), |
|
|
|
|
|
|
81
|
|
|
'clientId' => $client->getClientIdentifier(), |
|
|
|
|
|
|
82
|
|
|
'clientSecret' => $client->getSecret(), |
|
|
|
|
|
|
83
|
|
|
]; |
|
84
|
|
|
|
|
85
|
|
|
return new JSONResponse($result); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function deleteClient(int $id): JSONResponse { |
|
89
|
|
|
$client = $this->clientMapper->getByUid($id); |
|
90
|
|
|
$this->accessTokenMapper->deleteByClientId($id); |
|
91
|
|
|
$this->defaultTokenMapper->deleteByName($client->getName()); |
|
92
|
|
|
$this->clientMapper->delete($client); |
|
93
|
|
|
return new JSONResponse([]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function getClients(): JSONResponse { |
|
97
|
|
|
$clients = $this->clientMapper->getClients(); |
|
98
|
|
|
|
|
99
|
|
|
$result = []; |
|
100
|
|
|
|
|
101
|
|
|
foreach ($clients as $client) { |
|
102
|
|
|
$result[] = [ |
|
103
|
|
|
'id' => $client->getId(), |
|
104
|
|
|
'name' => $client->getName(), |
|
105
|
|
|
'redirectUri' => $client->getRedirectUri(), |
|
106
|
|
|
'clientId' => $client->getClientIdentifier(), |
|
107
|
|
|
'clientSecret' => $client->getSecret(), |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return new JSONResponse($result); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: