diff --git a/appinfo/app.php b/appinfo/app.php index f29ab5b39..57baa188a 100644 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -81,6 +81,9 @@ return; } +$app = new \OCA\User_SAML\AppInfo\Application(); +$app->registerDavAuth(); + $redirectSituation = false; $user = $userSession->getUser(); diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 0a7a2429f..126099df8 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -21,9 +21,11 @@ namespace OCA\User_SAML\AppInfo; +use OCA\User_SAML\DavPlugin; use OCA\User_SAML\Middleware\OnlyLoggedInMiddleware; use OCP\AppFramework\App; use OCP\AppFramework\IAppContainer; +use OCP\SabrePluginEvent; class Application extends App { public function __construct(array $urlParams = array()) { @@ -33,12 +35,32 @@ public function __construct(array $urlParams = array()) { /** * Middleware */ - $container->registerService('OnlyLoggedInMiddleware', function(IAppContainer $c){ + $container->registerService('OnlyLoggedInMiddleware', function (IAppContainer $c) { return new OnlyLoggedInMiddleware( $c->query('ControllerMethodReflector'), $c->query('ServerContainer')->getUserSession() ); }); + + $container->registerService(DavPlugin::class, function (IAppContainer $c) { + $server = $c->getServer(); + return new DavPlugin( + $server->getSession(), + $server->getConfig(), + $_SERVER + ); + }); + $container->registerMiddleWare('OnlyLoggedInMiddleware'); } + + public function registerDavAuth() { + + $container = $this->getContainer(); + + $dispatcher = $container->getServer()->getEventDispatcher(); + $dispatcher->addListener('OCA\DAV\Connector\Sabre::addPlugin', function (SabrePluginEvent $event) use ($container) { + $event->getServer()->addPlugin($container->query(DavPlugin::class)); + }); + } } diff --git a/lib/DavPlugin.php b/lib/DavPlugin.php new file mode 100644 index 000000000..b794b85b5 --- /dev/null +++ b/lib/DavPlugin.php @@ -0,0 +1,67 @@ + + * + * @license GNU AGPL version 3 or any later version + * + * This program 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. + * + * 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 + * along with this program. If not, see . + * + */ + +namespace OCA\User_SAML; + +use OCA\DAV\Connector\Sabre\Auth; +use OCP\IConfig; +use OCP\ISession; +use Sabre\DAV\CorePlugin; +use Sabre\DAV\FS\Directory; +use Sabre\DAV\Server; +use Sabre\DAV\ServerPlugin; +use Sabre\DAV\Tree; +use Sabre\HTTP\RequestInterface; +use Sabre\HTTP\ResponseInterface; + +class DavPlugin extends ServerPlugin { + private $session; + private $config; + private $auth; + /** @var Server */ + private $server; + + public function __construct(ISession $session, IConfig $config, array $auth) { + $this->session = $session; + $this->config = $config; + $this->auth = $auth; + } + + + public function initialize(Server $server) { + // before auth + $server->on('beforeMethod', [$this, 'beforeMethod'], 9); + $this->server = $server; + } + + public function beforeMethod(RequestInterface $request, ResponseInterface $response) { + if ( + $this->config->getAppValue('user_saml', 'type') === 'environment-variable' && + !$this->session->exists('user_saml.samlUserData') + ) { + $uidMapping = $this->config->getAppValue('user_saml', 'general-uid_mapping'); + if (isset($this->auth[$uidMapping])) { + $this->session->set(Auth::DAV_AUTHENTICATED, $this->auth[$uidMapping]); + $this->session->set('user_saml.samlUserData', $this->auth); + } + } + } +} diff --git a/tests/unit/Controller/SAMLControllerTest.php b/tests/unit/Controller/SAMLControllerTest.php index 2ff2a4618..c46fc38dd 100644 --- a/tests/unit/Controller/SAMLControllerTest.php +++ b/tests/unit/Controller/SAMLControllerTest.php @@ -177,7 +177,7 @@ public function testLoginWithEnvVariableAndExistingUser() { ->with('/') ->willReturn('https://nextcloud.com/absolute/'); $this->userBackend - ->expects($this->once()) + ->expects($this->any()) ->method('getCurrentUserId') ->willReturn('MyUid'); /** @var IUser|\PHPUnit_Framework_MockObject_MockObject $user */