|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Christoph Wurst <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
6
|
|
|
* @license AGPL-3.0 |
|
7
|
|
|
* |
|
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
10
|
|
|
* as published by the Free Software Foundation. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\Settings\Controller; |
|
23
|
|
|
|
|
24
|
|
|
use OC\AppFramework\Http; |
|
25
|
|
|
use OC\Authentication\Exceptions\InvalidTokenException; |
|
26
|
|
|
use OC\Authentication\Exceptions\PasswordlessTokenException; |
|
27
|
|
|
use OC\Authentication\Token\IProvider; |
|
28
|
|
|
use OC\Authentication\Token\IToken; |
|
29
|
|
|
use OCP\AppFramework\Controller; |
|
30
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
31
|
|
|
use OCP\IRequest; |
|
32
|
|
|
use OCP\ISession; |
|
33
|
|
|
use OCP\IUserManager; |
|
34
|
|
|
use OCP\Security\ISecureRandom; |
|
35
|
|
|
use OCP\Session\Exceptions\SessionNotAvailableException; |
|
36
|
|
|
|
|
37
|
|
|
class AuthSettingsController extends Controller { |
|
38
|
|
|
|
|
39
|
|
|
/** @var IProvider */ |
|
40
|
|
|
private $tokenProvider; |
|
41
|
|
|
|
|
42
|
|
|
/** @var IUserManager */ |
|
43
|
|
|
private $userManager; |
|
44
|
|
|
|
|
45
|
|
|
/** @var ISession */ |
|
46
|
|
|
private $session; |
|
47
|
|
|
|
|
48
|
|
|
/** @var string */ |
|
49
|
|
|
private $uid; |
|
50
|
|
|
|
|
51
|
|
|
/** @var ISecureRandom */ |
|
52
|
|
|
private $random; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $appName |
|
56
|
|
|
* @param IRequest $request |
|
57
|
|
|
* @param IProvider $tokenProvider |
|
58
|
|
|
* @param IUserManager $userManager |
|
59
|
|
|
* @param ISession $session |
|
60
|
|
|
* @param ISecureRandom $random |
|
61
|
|
|
* @param string $uid |
|
62
|
|
|
*/ |
|
63
|
|
View Code Duplication |
public function __construct($appName, IRequest $request, IProvider $tokenProvider, IUserManager $userManager, |
|
|
|
|
|
|
64
|
|
|
ISession $session, ISecureRandom $random, $uid) { |
|
65
|
|
|
parent::__construct($appName, $request); |
|
66
|
|
|
$this->tokenProvider = $tokenProvider; |
|
67
|
|
|
$this->userManager = $userManager; |
|
68
|
|
|
$this->uid = $uid; |
|
69
|
|
|
$this->session = $session; |
|
70
|
|
|
$this->random = $random; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @NoAdminRequired |
|
75
|
|
|
* @NoSubadminRequired |
|
76
|
|
|
* |
|
77
|
|
|
* @return JSONResponse |
|
78
|
|
|
*/ |
|
79
|
|
|
public function index() { |
|
80
|
|
|
$user = $this->userManager->get($this->uid); |
|
81
|
|
|
if (is_null($user)) { |
|
82
|
|
|
return []; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
$tokens = $this->tokenProvider->getTokenByUser($user); |
|
85
|
|
|
|
|
86
|
|
|
try { |
|
87
|
|
|
$sessionId = $this->session->getId(); |
|
88
|
|
|
} catch (SessionNotAvailableException $ex) { |
|
89
|
|
|
return $this->getServiceNotAvailableResponse(); |
|
90
|
|
|
} |
|
91
|
|
|
try { |
|
92
|
|
|
$sessionToken = $this->tokenProvider->getToken($sessionId); |
|
93
|
|
|
} catch (InvalidTokenException $ex) { |
|
94
|
|
|
return $this->getServiceNotAvailableResponse(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return array_map(function(IToken $token) use ($sessionToken) { |
|
|
|
|
|
|
98
|
|
|
$data = $token->jsonSerialize(); |
|
99
|
|
|
if ($sessionToken->getId() === $token->getId()) { |
|
100
|
|
|
$data['canDelete'] = false; |
|
101
|
|
|
} else { |
|
102
|
|
|
$data['canDelete'] = true; |
|
103
|
|
|
} |
|
104
|
|
|
return $data; |
|
105
|
|
|
}, $tokens); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @NoAdminRequired |
|
110
|
|
|
* @NoSubadminRequired |
|
111
|
|
|
* |
|
112
|
|
|
* @return JSONResponse |
|
113
|
|
|
*/ |
|
114
|
|
|
public function create($name) { |
|
115
|
|
|
try { |
|
116
|
|
|
$sessionId = $this->session->getId(); |
|
117
|
|
|
} catch (SessionNotAvailableException $ex) { |
|
118
|
|
|
return $this->getServiceNotAvailableResponse(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
try { |
|
122
|
|
|
$sessionToken = $this->tokenProvider->getToken($sessionId); |
|
123
|
|
|
$loginName = $sessionToken->getLoginName(); |
|
124
|
|
|
try { |
|
125
|
|
|
$password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
126
|
|
|
} catch (PasswordlessTokenException $ex) { |
|
127
|
|
|
$password = null; |
|
128
|
|
|
} |
|
129
|
|
|
} catch (InvalidTokenException $ex) { |
|
130
|
|
|
return $this->getServiceNotAvailableResponse(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
$token = $this->generateRandomDeviceToken(); |
|
134
|
|
|
$deviceToken = $this->tokenProvider->generateToken($token, $this->uid, $loginName, $password, $name, IToken::PERMANENT_TOKEN); |
|
135
|
|
|
|
|
136
|
|
|
return [ |
|
|
|
|
|
|
137
|
|
|
'token' => $token, |
|
138
|
|
|
'loginName' => $loginName, |
|
139
|
|
|
'deviceToken' => $deviceToken |
|
140
|
|
|
]; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
private function getServiceNotAvailableResponse() { |
|
144
|
|
|
$resp = new JSONResponse(); |
|
145
|
|
|
$resp->setStatus(Http::STATUS_SERVICE_UNAVAILABLE); |
|
146
|
|
|
return $resp; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Return a 20 digit device password |
|
151
|
|
|
* |
|
152
|
|
|
* Example: ABCDE-FGHIJ-KLMNO-PQRST |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
private function generateRandomDeviceToken() { |
|
157
|
|
|
$groups = []; |
|
158
|
|
|
for ($i = 0; $i < 4; $i++) { |
|
159
|
|
|
$groups[] = $this->random->generate(5, implode('', range('A', 'Z'))); |
|
160
|
|
|
} |
|
161
|
|
|
return implode('-', $groups); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @NoAdminRequired |
|
166
|
|
|
* @NoSubadminRequired |
|
167
|
|
|
* |
|
168
|
|
|
* @return JSONResponse |
|
169
|
|
|
*/ |
|
170
|
|
|
public function destroy($id) { |
|
171
|
|
|
$user = $this->userManager->get($this->uid); |
|
172
|
|
|
if (is_null($user)) { |
|
173
|
|
|
return []; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$this->tokenProvider->invalidateTokenById($user, $id); |
|
177
|
|
|
return []; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.