From dbddbb634bcb6df346988b85cb2847e76e22e632 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Fri, 4 Dec 2015 12:11:07 +0100 Subject: Use EventDispatcher to allow additional setup of auth backends - move federation auth to federation app --- apps/dav/lib/connector/fedauth.php | 55 --------------------------------- apps/dav/lib/server.php | 7 ++++- apps/federation/appinfo/application.php | 14 +++++++++ apps/federation/dav/fedauth.php | 55 +++++++++++++++++++++++++++++++++ lib/public/sabrepluginevent.php | 15 ++++++++- 5 files changed, 89 insertions(+), 57 deletions(-) delete mode 100644 apps/dav/lib/connector/fedauth.php create mode 100644 apps/federation/dav/fedauth.php diff --git a/apps/dav/lib/connector/fedauth.php b/apps/dav/lib/connector/fedauth.php deleted file mode 100644 index 42a29cef3fc..00000000000 --- a/apps/dav/lib/connector/fedauth.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * @copyright Copyright (c) 2015, ownCloud, Inc. - * @license AGPL-3.0 - * - * 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. - * - * This program 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, version 3, - * along with this program. If not, see - * - */ -namespace OCA\DAV\Connector; - -use OCA\Federation\DbHandler; -use OCP\IDBConnection; -use Sabre\DAV\Auth\Backend\AbstractBasic; - -class FedAuth extends AbstractBasic { - - /** - * FedAuth constructor. - * - * @param IDBConnection $db - */ - public function __construct(IDBConnection $db) { - $this->db = $db; - $this->principalPrefix = 'principals/system/'; - } - - /** - * Validates a username and password - * - * This method should return true or false depending on if login - * succeeded. - * - * @param string $username - * @param string $password - * @return bool - */ - protected function validateUserPass($username, $password) { - $h = new DbHandler($this->db, - \OC::$server->getL10N('federation') - ); - return $h->auth($username, $password); - } -} diff --git a/apps/dav/lib/server.php b/apps/dav/lib/server.php index 8b7171c145c..93e903e6bf1 100644 --- a/apps/dav/lib/server.php +++ b/apps/dav/lib/server.php @@ -8,6 +8,7 @@ use OCA\DAV\Connector\Sabre\Auth; use OCA\DAV\Connector\Sabre\BlockLegacyClientPlugin; use OCA\DAV\Files\CustomPropertiesBackend; use OCP\IRequest; +use OCP\SabrePluginEvent; use Sabre\DAV\Auth\Plugin; class Server { @@ -37,8 +38,12 @@ class Server { $this->server->addPlugin(new BlockLegacyClientPlugin(\OC::$server->getConfig())); $authPlugin = new Plugin($authBackend, 'ownCloud'); - $authPlugin->addBackend(new FedAuth(\OC::$server->getDatabaseConnection())); $this->server->addPlugin($authPlugin); + + // allow setup of additional auth backends + $event = new SabrePluginEvent($this->server); + $dispatcher->dispatch('OCA\DAV\Connector\Sabre::authInit', $event); + $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\DummyGetResponsePlugin()); $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\ExceptionLoggerPlugin('webdav', $logger)); $this->server->addPlugin(new \OCA\DAV\Connector\Sabre\LockPlugin()); diff --git a/apps/federation/appinfo/application.php b/apps/federation/appinfo/application.php index 45d88548b70..f0fefb948af 100644 --- a/apps/federation/appinfo/application.php +++ b/apps/federation/appinfo/application.php @@ -23,6 +23,7 @@ namespace OCA\Federation\AppInfo; use OCA\Federation\API\OCSAuthAPI; use OCA\Federation\Controller\SettingsController; +use OCA\Federation\DAV\FedAuth; use OCA\Federation\DbHandler; use OCA\Federation\Hooks; use OCA\Federation\Middleware\AddServerMiddleware; @@ -30,7 +31,9 @@ use OCA\Federation\TrustedServers; use OCP\API; use OCP\App; use OCP\AppFramework\IAppContainer; +use OCP\SabrePluginEvent; use OCP\Util; +use Sabre\DAV\Auth\Plugin; class Application extends \OCP\AppFramework\App { @@ -144,6 +147,17 @@ class Application extends \OCP\AppFramework\App { $hooksManager, 'addServerHook' ); + + $dispatcher = $this->getContainer()->getServer()->getEventDispatcher(); + $dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function($event) use($container) { + if ($event instanceof SabrePluginEvent) { + $authPlugin = $event->getServer()->getPlugin('auth'); + if ($authPlugin instanceof Plugin) { + $db = $container->getServer()->getDatabaseConnection(); + $authPlugin->addBackend(new FedAuth($db)); + } + } + }); } } diff --git a/apps/federation/dav/fedauth.php b/apps/federation/dav/fedauth.php new file mode 100644 index 00000000000..ade5448d1bc --- /dev/null +++ b/apps/federation/dav/fedauth.php @@ -0,0 +1,55 @@ + + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * 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. + * + * This program 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, version 3, + * along with this program. If not, see + * + */ +namespace OCA\Federation\DAV; + +use OCA\Federation\DbHandler; +use OCP\IDBConnection; +use Sabre\DAV\Auth\Backend\AbstractBasic; + +class FedAuth extends AbstractBasic { + + /** + * FedAuth constructor. + * + * @param IDBConnection $db + */ + public function __construct(IDBConnection $db) { + $this->db = $db; + $this->principalPrefix = 'principals/system/'; + } + + /** + * Validates a username and password + * + * This method should return true or false depending on if login + * succeeded. + * + * @param string $username + * @param string $password + * @return bool + */ + protected function validateUserPass($username, $password) { + $h = new DbHandler($this->db, + \OC::$server->getL10N('federation') + ); + return $h->auth($username, $password); + } +} diff --git a/lib/public/sabrepluginevent.php b/lib/public/sabrepluginevent.php index fed3237166d..1a64c8ac3ed 100644 --- a/lib/public/sabrepluginevent.php +++ b/lib/public/sabrepluginevent.php @@ -23,6 +23,7 @@ namespace OCP; use OCP\AppFramework\Http; +use Sabre\DAV\Server; use Symfony\Component\EventDispatcher\Event; /** @@ -36,12 +37,16 @@ class SabrePluginEvent extends Event { /** @var string */ protected $message; + /** @var Server */ + protected $server; + /** * @since 8.2.0 */ - public function __construct() { + public function __construct($server = null) { $this->message = ''; $this->statusCode = Http::STATUS_OK; + $this->server = $server; } /** @@ -79,4 +84,12 @@ class SabrePluginEvent extends Event { public function getMessage() { return $this->message; } + + /** + * @return null|Server + * @since 9.0.0 + */ + public function getServer() { + return $this->server; + } } -- cgit v1.2.3