2013-08-17 13:16:48 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Thomas Tanghus <thomas@tanghus.net>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2013-08-17 13:16:48 +04:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
namespace OC\AppFramework\Middleware\Security;
|
|
|
|
|
2013-10-23 07:57:34 +04:00
|
|
|
use OC\AppFramework\Http;
|
2014-05-06 18:29:19 +04:00
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2014-04-20 18:12:46 +04:00
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
2013-10-05 18:59:06 +04:00
|
|
|
use OCP\AppFramework\Middleware;
|
2013-08-21 03:00:26 +04:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2014-05-28 04:12:01 +04:00
|
|
|
use OCP\INavigationManager;
|
|
|
|
use OCP\IURLGenerator;
|
2013-10-07 02:33:54 +04:00
|
|
|
use OCP\IRequest;
|
2014-05-28 04:12:01 +04:00
|
|
|
use OCP\ILogger;
|
2014-11-14 19:20:51 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
2014-11-17 17:10:47 +03:00
|
|
|
use OCP\Util;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to do all the authentication and checking stuff for a controller method
|
|
|
|
* It reads out the annotations of a controller method and checks which if
|
|
|
|
* security things should be checked and also handles errors in case a security
|
|
|
|
* check fails
|
|
|
|
*/
|
|
|
|
class SecurityMiddleware extends Middleware {
|
|
|
|
|
2014-05-28 04:12:01 +04:00
|
|
|
private $navigationManager;
|
2013-08-17 13:16:48 +04:00
|
|
|
private $request;
|
2014-05-06 21:13:59 +04:00
|
|
|
private $reflector;
|
2014-05-28 04:12:01 +04:00
|
|
|
private $appName;
|
|
|
|
private $urlGenerator;
|
|
|
|
private $logger;
|
|
|
|
private $isLoggedIn;
|
|
|
|
private $isAdminUser;
|
2014-05-06 21:13:59 +04:00
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
/**
|
2013-10-07 02:33:54 +04:00
|
|
|
* @param IRequest $request
|
2014-05-06 18:29:19 +04:00
|
|
|
* @param ControllerMethodReflector $reflector
|
2014-05-28 04:12:01 +04:00
|
|
|
* @param INavigationManager $navigationManager
|
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param string $appName
|
|
|
|
* @param bool $isLoggedIn
|
|
|
|
* @param bool $isAdminUser
|
2013-08-17 13:16:48 +04:00
|
|
|
*/
|
2014-05-28 04:12:01 +04:00
|
|
|
public function __construct(IRequest $request,
|
|
|
|
ControllerMethodReflector $reflector,
|
|
|
|
INavigationManager $navigationManager,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
ILogger $logger,
|
|
|
|
$appName,
|
|
|
|
$isLoggedIn,
|
|
|
|
$isAdminUser){
|
|
|
|
$this->navigationManager = $navigationManager;
|
2013-08-17 13:16:48 +04:00
|
|
|
$this->request = $request;
|
2014-05-06 18:29:19 +04:00
|
|
|
$this->reflector = $reflector;
|
2014-05-28 04:12:01 +04:00
|
|
|
$this->appName = $appName;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->isLoggedIn = $isLoggedIn;
|
|
|
|
$this->isAdminUser = $isAdminUser;
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This runs all the security checks before a method call. The
|
|
|
|
* security checks are determined by inspecting the controller method
|
|
|
|
* annotations
|
2014-02-19 12:31:54 +04:00
|
|
|
* @param string $controller the controllername or string
|
2013-08-17 13:16:48 +04:00
|
|
|
* @param string $methodName the name of the method
|
|
|
|
* @throws SecurityException when a security check fails
|
|
|
|
*/
|
|
|
|
public function beforeController($controller, $methodName){
|
|
|
|
|
|
|
|
// this will set the current navigation entry of the app, use this only
|
|
|
|
// for normal HTML requests and not for AJAX requests
|
2014-05-28 04:12:01 +04:00
|
|
|
$this->navigationManager->setActiveEntry($this->appName);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
// security checks
|
2014-05-06 18:29:19 +04:00
|
|
|
$isPublicPage = $this->reflector->hasAnnotation('PublicPage');
|
2013-08-20 23:21:21 +04:00
|
|
|
if(!$isPublicPage) {
|
2014-05-28 04:12:01 +04:00
|
|
|
if(!$this->isLoggedIn) {
|
2013-08-17 13:16:48 +04:00
|
|
|
throw new SecurityException('Current user is not logged in', Http::STATUS_UNAUTHORIZED);
|
|
|
|
}
|
|
|
|
|
2014-05-06 18:29:19 +04:00
|
|
|
if(!$this->reflector->hasAnnotation('NoAdminRequired')) {
|
2014-05-28 04:12:01 +04:00
|
|
|
if(!$this->isAdminUser) {
|
2013-08-20 23:21:21 +04:00
|
|
|
throw new SecurityException('Logged in user must be an admin', Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 17:10:47 +03:00
|
|
|
// CSRF check - also registers the CSRF token since the session may be closed later
|
|
|
|
Util::callRegister();
|
2014-05-06 18:29:19 +04:00
|
|
|
if(!$this->reflector->hasAnnotation('NoCSRFRequired')) {
|
2013-10-07 02:33:54 +04:00
|
|
|
if(!$this->request->passesCSRFCheck()) {
|
2013-08-17 13:16:48 +04:00
|
|
|
throw new SecurityException('CSRF check failed', Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 19:20:51 +03:00
|
|
|
/**
|
|
|
|
* FIXME: Use DI once available
|
|
|
|
* Checks if app is enabled (also inclues a check whether user is allowed to access the resource)
|
|
|
|
* The getAppPath() check is here since components such as settings also use the AppFramework and
|
|
|
|
* therefore won't pass this check.
|
|
|
|
*/
|
|
|
|
if(\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) {
|
|
|
|
throw new SecurityException('App is not enabled', Http::STATUS_PRECONDITION_FAILED);
|
|
|
|
}
|
|
|
|
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If an SecurityException is being caught, ajax requests return a JSON error
|
|
|
|
* response and non ajax requests redirect to the index
|
|
|
|
* @param Controller $controller the controller that is being called
|
|
|
|
* @param string $methodName the name of the method that will be called on
|
|
|
|
* the controller
|
|
|
|
* @param \Exception $exception the thrown exception
|
|
|
|
* @throws \Exception the passed in exception if it cant handle it
|
|
|
|
* @return Response a Response object or null in case that the exception could not be handled
|
|
|
|
*/
|
|
|
|
public function afterException($controller, $methodName, \Exception $exception){
|
|
|
|
if($exception instanceof SecurityException){
|
|
|
|
|
|
|
|
if (stripos($this->request->getHeader('Accept'),'html')===false) {
|
|
|
|
|
|
|
|
$response = new JSONResponse(
|
|
|
|
array('message' => $exception->getMessage()),
|
|
|
|
$exception->getCode()
|
|
|
|
);
|
2014-05-28 04:12:01 +04:00
|
|
|
$this->logger->debug($exception->getMessage());
|
2013-08-17 13:16:48 +04:00
|
|
|
} else {
|
|
|
|
|
2013-10-07 02:33:54 +04:00
|
|
|
// TODO: replace with link to route
|
2014-05-28 04:12:01 +04:00
|
|
|
$url = $this->urlGenerator->getAbsoluteURL('index.php');
|
2014-07-01 18:54:52 +04:00
|
|
|
// add redirect URL to redirect to the previous page after login
|
|
|
|
$url .= '?redirect_url=' . urlencode($this->request->server['REQUEST_URI']);
|
2013-08-17 13:16:48 +04:00
|
|
|
$response = new RedirectResponse($url);
|
2014-05-28 04:12:01 +04:00
|
|
|
$this->logger->debug($exception->getMessage());
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|