This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reorganize concentric middlewares. Use authfront plugins as pseudo mi…
…ddlewares as well. Factorize more and more index.php and rest.php - cmd.php could do the same. Encapsulate loggingResult in a serializable message.
- Loading branch information
Showing
29 changed files
with
692 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
core/src/core/src/pydio/Core/Http/Message/LoggingResult.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?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 <http://pyd.io/>. | ||
*/ | ||
namespace Pydio\Core\Http\Message; | ||
|
||
defined('AJXP_EXEC') or die('Access not allowed'); | ||
|
||
|
||
use Pydio\Core\Http\XMLSerializableResponseChunk; | ||
|
||
class LoggingResult implements XMLSerializableResponseChunk | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $result; | ||
/** | ||
* @var string | ||
*/ | ||
private $rememberLogin; | ||
/** | ||
* @var string | ||
*/ | ||
private $rememberPass; | ||
/** | ||
* @var string | ||
*/ | ||
private $secureToken; | ||
|
||
|
||
public function __construct($result, $rememberLogin="", $rememberPass = "", $secureToken="") | ||
{ | ||
$this->result = $result; | ||
$this->rememberLogin = $rememberLogin; | ||
$this->rememberPass = $rememberPass; | ||
$this->secureToken = $secureToken; | ||
} | ||
|
||
/** | ||
* @return int | ||
*/ | ||
public function getResult(){ | ||
return $this->result; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function toXML() | ||
{ | ||
$remString = ""; | ||
if ($this->rememberPass != "" && $this->rememberLogin!= "") { | ||
$remString = " remember_login=\"$this->rememberLogin\" remember_pass=\"$this->rememberPass\""; | ||
} | ||
if ($this->secureToken != "") { | ||
$remString .= " secure_token=\"$this->secureToken\""; | ||
} | ||
return "<logging_result value=\"$this->result\"$remString/>"; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
core/src/core/src/pydio/Core/Http/Middleware/AuthMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?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 <http://pyd.io/>. | ||
*/ | ||
namespace Pydio\Core\Http\Middleware; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Pydio\Authfront\Core\AbstractAuthFrontend; | ||
use Pydio\Core\Exception\PydioException; | ||
use Pydio\Core\Exception\WorkspaceNotFoundException; | ||
use Pydio\Core\Http\Server; | ||
use Pydio\Core\PluginFramework\PluginsService; | ||
use Pydio\Core\Services\AuthService; | ||
use Pydio\Core\Services\ConfService; | ||
use Pydio\Log\Core\AJXP_Logger; | ||
|
||
defined('AJXP_EXEC') or die('Access not allowed'); | ||
|
||
|
||
class AuthMiddleware | ||
{ | ||
|
||
/** | ||
* @param ServerRequestInterface $requestInterface | ||
* @param \Psr\Http\Message\ResponseInterface $responseInterface | ||
* @return \Psr\Http\Message\ResponseInterface | ||
* @param callable|null $next | ||
* @throws WorkspaceNotFoundException | ||
*/ | ||
public static function handleRequest(\Psr\Http\Message\ServerRequestInterface &$requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface, callable $next = null){ | ||
|
||
if(AuthService::usersEnabled()){ | ||
|
||
PluginsService::getInstance()->initActivePlugins(); | ||
$frontends = PluginsService::getInstance()->getActivePluginsForType("authfront"); | ||
$index = 0; | ||
/** | ||
* @var AbstractAuthFrontend $frontendPlugin | ||
*/ | ||
foreach($frontends as $frontendPlugin){ | ||
if(!$frontendPlugin->isEnabled()) continue; | ||
if(!method_exists($frontendPlugin, "tryToLogUser")){ | ||
AJXP_Logger::error(__CLASS__, __FUNCTION__, "Trying to use an authfront plugin without tryToLogUser method. Wrongly initialized?"); | ||
continue; | ||
} | ||
//$res = $frontendPlugin->tryToLogUser($httpVars, ($index == count($frontends)-1)); | ||
$isLast = ($index == count($frontends)-1); | ||
$res = $frontendPlugin->tryToLogUser($requestInterface, $responseInterface, $isLast); | ||
$index ++; | ||
if($res) { | ||
if($responseInterface->getBody()->getSize() > 0 || $responseInterface->getStatusCode() != 200){ | ||
// Do not go to the other middleware, return directly. | ||
return $responseInterface; | ||
} | ||
break; | ||
} | ||
} | ||
|
||
} | ||
|
||
if(Server::$mode == Server::MODE_SESSION){ | ||
self::bootSessionServer($requestInterface); | ||
}else{ | ||
self::bootRestServer($requestInterface); | ||
} | ||
|
||
ConfService::reloadServicesAndActivePlugins(); | ||
|
||
return Server::callNextMiddleWare($requestInterface, $responseInterface, $next); | ||
|
||
} | ||
|
||
protected static function bootSessionServer(ServerRequestInterface $request){ | ||
|
||
$parameters = $request->getParsedBody(); | ||
if (AuthService::usersEnabled()) { | ||
|
||
$loggedUser = AuthService::getLoggedUser(); | ||
if ($loggedUser != null) { | ||
$res = ConfService::switchUserToActiveRepository($loggedUser, (isSet($parameters["tmp_repository_id"])?$parameters["tmp_repository_id"]:"-1")); | ||
if (!$res) { | ||
AuthService::disconnect(); | ||
} | ||
} | ||
|
||
}else{ | ||
|
||
if (isSet($parameters["tmp_repository_id"])) { | ||
try{ | ||
ConfService::switchRootDir($parameters["tmp_repository_id"], true); | ||
}catch(PydioException $e){} | ||
} else if (isSet($_SESSION["SWITCH_BACK_REPO_ID"])) { | ||
ConfService::switchRootDir($_SESSION["SWITCH_BACK_REPO_ID"]); | ||
unset($_SESSION["SWITCH_BACK_REPO_ID"]); | ||
} | ||
|
||
} | ||
|
||
//Set language | ||
$loggedUser = AuthService::getLoggedUser(); | ||
if($loggedUser != null && $loggedUser->getPref("lang") != "") ConfService::setLanguage($loggedUser->getPref("lang")); | ||
else if(isSet($request->getCookieParams()["AJXP_lang"])) ConfService::setLanguage($request->getCookieParams()["AJXP_lang"]); | ||
|
||
} | ||
|
||
protected static function bootRestServer(ServerRequestInterface $request){ | ||
|
||
if(AuthService::getLoggedUser() == null){ | ||
header('HTTP/1.0 401 Unauthorized'); | ||
echo 'You are not authorized to access this API.'; | ||
exit; | ||
} | ||
|
||
$repoID = $request->getAttribute("repository_id"); | ||
if($repoID == 'pydio'){ | ||
ConfService::switchRootDir(); | ||
$repo = ConfService::getRepository(); | ||
}else{ | ||
$repo = ConfService::findRepositoryByIdOrAlias($repoID); | ||
if ($repo == null) { | ||
throw new WorkspaceNotFoundException($repoID); | ||
} | ||
if(!ConfService::repositoryIsAccessible($repo->getId(), $repo, AuthService::getLoggedUser(), false, true)){ | ||
header('HTTP/1.0 401 Unauthorized'); | ||
echo 'You are not authorized to access this workspace.'; | ||
exit; | ||
} | ||
ConfService::switchRootDir($repo->getId()); | ||
} | ||
|
||
} | ||
|
||
|
||
} |
Oops, something went wrong.