This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
AuthCliMiddleware.php
287 lines (233 loc) · 10.9 KB
/
AuthCliMiddleware.php
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?php
/*
* Copyright 2007-2015 Abstrium <contact (at) pydio.com>
* This file is part of Pydio.
*
* Pydio 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.
*
* Pydio 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 Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
namespace Pydio\Core\Http\Cli;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Pydio\Auth\Core\MemorySafe;
use Pydio\Core\Exception\AuthRequiredException;
use Pydio\Core\Exception\LoginException;
use Pydio\Core\Http\Response\SerializableResponseStream;
use Pydio\Core\Http\Server;
use Pydio\Core\Model\Context;
use Pydio\Core\PluginFramework\PluginsService;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Services\ConfService;
use Pydio\Core\Services\RolesService;
use Pydio\Core\Services\UsersService;
use Pydio\Core\Utils\ApplicationState;
use Pydio\Core\Utils\Crypto;
use Pydio\Core\Utils\TextEncoder;
use Pydio\Log\Core\Logger;
use Pydio\Tasks\Task;
use Pydio\Tasks\TaskService;
use Symfony\Component\Console\Output\OutputInterface;
use Zend\Diactoros\Response;
use Zend\Diactoros\Stream;
defined('AJXP_EXEC') or die('Access not allowed');
/**
* Authenticatino middleware used in Command Line context
* @package Pydio\Core\Http\Cli
*/
class AuthCliMiddleware
{
/**
* @param $options
* @return \Pydio\Core\Model\UserInterface
* @throws AuthRequiredException
*/
protected static function authenticateFromCliParameters($options){
$optUser = $options["u"];
$optPass = $options["p"];
if (!empty($options["p"])) {
$optPass = $options["p"];
} else {
// Consider "u" is a crypted version of u:p
$optToken = $options["t"];
$optUser = Crypto::decrypt($optUser, md5($optToken.Crypto::getCliSecret()));
$envPass = MemorySafe::loadPasswordStringFromEnvironment($optUser);
if($envPass !== false){
unset($optToken);
$optPass = $envPass;
}
}
if (UsersService::usersEnabled() && !empty($optUser)) {
try{
$loggedUser = AuthService::logUser($optUser, $optPass, isSet($optToken), false);
}catch (LoginException $l){
throw new AuthRequiredException();
}
} else {
throw new AuthRequiredException();
}
return $loggedUser;
}
/**
* @param ServerRequestInterface $requestInterface
* @param \Psr\Http\Message\ResponseInterface $responseInterface
* @param callable|null $next
* @return ResponseInterface
* @throws AuthRequiredException
*/
public static function handleRequest(ServerRequestInterface $requestInterface, ResponseInterface $responseInterface, callable $next = null){
$driverImpl = ConfService::getAuthDriverImpl();
PluginsService::getInstance(Context::emptyContext())->setPluginUniqueActiveForType("auth", $driverImpl->getName(), $driverImpl);
/** @var OutputInterface $output */
$output = $requestInterface->getAttribute("cli-output");
$options = $requestInterface->getAttribute("cli-options");
$optRepoId = $options["r"];
$impersonateUsers = false;
// TODO 1/ REIMPLEMENT parameter queue: to pass a file with many user names?
/**
* if (strpos($optUser, "queue:") === 0) {
$optUserQueue = substr($optUser, strlen("queue:"));
$optUser = false;
//echo("QUEUE : ".$optUserQueue);
if (is_file($optUserQueue)) {
$lines = file($optUserQueue);
if (count($lines) && !empty($lines[0])) {
$allUsers = explode(",", $lines[0]);
$optUser = array_shift($allUsers);
file_put_contents($optUserQueue, implode(",", $allUsers));
}
}
if ($optUser === false) {
if (is_file($optUserQueue)) {
unlink($optUserQueue);
}
die("No more users inside queue");
}
*/
// TODO 2/ REIMPLEMENT DETECT USER PARAMETER BASED ON REPOSITORY PATH OPTION ?
/*
if ($optDetectUser != false) {
$path = $repository->getOption("PATH", true);
if (strpos($path, "AJXP_USER") !== false) {
$path = str_replace(
array("AJXP_INSTALL_PATH", "AJXP_DATA_PATH", "/"),
array(AJXP_INSTALL_PATH, AJXP_DATA_PATH, DIRECTORY_SEPARATOR),
$path
);
$parts = explode("AJXP_USER", $path);
if(count($parts) == 1) $parts[1] = "";
$first = str_replace("\\", "\\\\", $parts[0]);
$last = str_replace("\\", "\\\\", $parts[1]);
*/
//if (preg_match("/$first(.*)$last.*/", $optDetectUser, $matches)) {
// $detectedUser = $matches[1];
// }
//}
//}
$loggedUser = self::authenticateFromCliParameters($options);
$requestInterface = $requestInterface->withAttribute("action", $options["a"]);
if(UsersService::usersEnabled() && ApplicationState::detectApplicationFirstRun()){
RolesService::bootSequence();
}
// Output directly to STDOUT
$responseInterface = $responseInterface->withBody(new Stream(STDOUT, "w"));
$applyCallback = function ($userId, $baseGroup, $index, $total) use ($optRepoId, $requestInterface, $output, $next){
$actionName = $requestInterface->getAttribute("action");
$taskId = $requestInterface->getAttribute("pydio-task-id");
$output->writeln("<info>*****************************</info>");
$output->writeln("<info>Current User is '".$userId."'</info>");
$output->writeln("<info>*****************************</info>");
try{
$user = UsersService::getUserById($userId);
if(is_array($optRepoId)){
$repos = [];
foreach($optRepoId as $repoId){
$repoObj = UsersService::getRepositoryWithPermission($user, $repoId);
$repos[$repoObj->getId()] = $repoObj;
}
}else if($optRepoId === "*"){
$repos = UsersService::getRepositoriesForUser($user, false); // Do not include shared
// Exclude "ajxp_*" repositories and "inbox"
foreach($repos as $id=>$object){
if($id === "inbox" || strpos($id, "ajxp_") === 0) unset($repos[$id]);
}
}else{
$repoObject = UsersService::getRepositoryWithPermission($user, $optRepoId);
$repos = [$repoObject->getId() => $repoObject];
}
foreach( $repos as $repoId => $repositoryInterface ){
try{
$output->writeln("<comment>Applying action '$actionName' on workspace ".$repositoryInterface->getDisplay()." (".$repoId.")</comment>");
$subResponse = new Response();
$ctx = Context::contextWithObjects($user, $repositoryInterface);
$requestInterface = $requestInterface->withAttribute("ctx", $ctx);
Logger::updateContext($ctx);
TextEncoder::updateContext($ctx);
$subResponse = Server::callNextMiddleWareAndRewind(function($middleware){
return (is_array($middleware) && $middleware["0"] == "Pydio\\Core\\Http\\Cli\\AuthCliMiddleware" && $middleware[1] == "handleRequest");
},
$requestInterface,
$subResponse,
$next
);
$body = $subResponse->getBody();
if($body instanceof SerializableResponseStream){
if($requestInterface->getParsedBody()["format"] == "json"){
$body->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_JSON, ["pretty" => true]);
}else if($requestInterface->getParsedBody()["format"] == "xml"){
$body->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_XML, ["pretty" => true]);
}else if($body->supportsSerializer(SerializableResponseStream::SERIALIZER_TYPE_CLI)){
$body->setSerializer(SerializableResponseStream::SERIALIZER_TYPE_CLI, ["output" => $output]);
}
}
$output->writeln( $body->getContents() );
}catch (\Exception $repoEx){
$output->writeln("<error>$taskId: ".$repoEx->getMessage()."</error>");
if(!empty($taskId)){
TaskService::getInstance()->updateTaskStatus($taskId, Task::STATUS_FAILED, $repoEx->getMessage());
}
}
}
}catch (\Exception $userEx){
$output->writeln("<error>USER: ".$userEx->getMessage()."</error>");
if(!empty($taskId)){
TaskService::getInstance()->updateTaskStatus($taskId, Task::STATUS_FAILED, $userEx->getMessage());
}
}
};
if(!empty($options["i"])){
if(!is_array($options["i"]) && strpos($options["i"], "*") === false){
$options["i"] = [$options["i"]];
}
$impersonateUsers = $options["i"];
}
if ($impersonateUsers !== false && $loggedUser->isAdmin()) {
if(is_array($impersonateUsers)){
// Apply to a predefined list of users
foreach ($impersonateUsers as $index => $impersonateUser) {
$applyCallback($impersonateUser, "/", $index, count($impersonateUsers));
}
}else if($impersonateUsers === "*"){
// Apply to all users of current group
UsersService::browseUsersGroupsWithCallback($loggedUser->getGroupPath(), $applyCallback, false);
}else if($impersonateUsers === "**/*"){
// Apply to all users recursively
UsersService::browseUsersGroupsWithCallback($loggedUser->getGroupPath(), $applyCallback, true);
}
}else{
$applyCallback($loggedUser->getId(), "/", 1, 1);
}
return $responseInterface;
}
}