Skip to content

Commit

Permalink
Merge pull request #56 from owncloud/feature/introduce-event-handler
Browse files Browse the repository at this point in the history
Introduce class EventHandler
  • Loading branch information
DeepDiver1975 authored Jan 16, 2020
2 parents 38f6936 + 2a0823f commit ea06fb9
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 26 deletions.
31 changes: 5 additions & 26 deletions lib/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,8 @@
use Jumbojett\OpenIDConnectClientException;
use OC;
use OC\HintException;
use OCA\OpenIdConnect\Sabre\OpenIdSabreAuthPlugin;
use OCP\AppFramework\App;
use OCP\ICache;
use OCP\IServerContainer;
use OCP\SabrePluginEvent;
use Sabre\DAV\Auth\Plugin;

class Application extends App {
/** @var Logger */
Expand All @@ -38,6 +34,7 @@ public function __construct(array $urlParams = []) {
/**
* @throws OpenIDConnectClientException
* @throws HintException
* @codeCoverageIgnore
*/
public function boot(): void {
$server = $this->getContainer()->getServer();
Expand All @@ -51,14 +48,16 @@ public function boot(): void {
if ($openIdConfig === null) {
return;
}
$session = $server->getSession();
$userSession = $server->getUserSession();
$urlGenerator = $server->getURLGenerator();
$request = $server->getRequest();
$loginPage = new LoginPageBehaviour($this->logger, $userSession, $urlGenerator, $request);
$loginPage->handleLoginPageBehaviour($openIdConfig);

// Add event listener
$this->registerEventHandler($server);
$eventHandler = new EventHandler($server->getEventDispatcher(), $request, $userSession, $session);
$eventHandler->registerEventHandler();

$this->verifySession();
}
Expand All @@ -69,6 +68,7 @@ private function logout(): void {

$server->getSession()->remove('oca.openid-connect.access-token');
$server->getSession()->remove('oca.openid-connect.refresh-token');
$server->getSession()->remove('oca.openid-connect.id-token');
$server->getUserSession()->logout();
}

Expand Down Expand Up @@ -204,25 +204,4 @@ private function refreshToken($exp): void {
}
}
}

/**
* @param IServerContainer $server
*/
protected function registerEventHandler(IServerContainer $server): void {
$dispatcher = $server->getEventDispatcher();
$dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', static function ($event) use ($server) {
if ($event instanceof SabrePluginEvent) {
$authPlugin = $event->getServer()->getPlugin('auth');
if ($authPlugin instanceof Plugin) {
$authPlugin->addBackend(
new OpenIdSabreAuthPlugin($server->getSession(),
$server->getUserSession(),
$server->getRequest(),
$server->query(OpenIdConnectAuthModule::class),
'principals/')
);
}
}
});
}
}
75 changes: 75 additions & 0 deletions lib/EventHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* ownCloud
*
* @author Thomas Müller <[email protected]>
* @copyright (C) 2019 ownCloud GmbH
* @license ownCloud Commercial License
*
* This code is covered by the ownCloud Commercial License.
*
* You should have received a copy of the ownCloud Commercial License
* along with this program. If not, see
* <https://owncloud.com/licenses/owncloud-commercial/>.
*
*/
namespace OCA\OpenIdConnect;

use OCA\OpenIdConnect\Sabre\OpenIdSabreAuthPlugin;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserSession;
use OCP\SabrePluginEvent;
use Sabre\DAV\Auth\Plugin;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class EventHandler {

/** @var EventDispatcherInterface */
private $dispatcher;
/** @var IRequest */
private $request;
/** @var IUserSession */
private $userSession;
/** @var ISession */
private $session;

public function __construct(EventDispatcherInterface $dispatcher,
IRequest $request,
IUserSession $userSession,
ISession $session) {
$this->dispatcher = $dispatcher;
$this->request = $request;
$this->userSession = $userSession;
$this->session = $session;
}

public function registerEventHandler(): void {
$this->dispatcher->addListener('OCA\DAV\Connector\Sabre::authInit', function ($event) {
if (!$event instanceof SabrePluginEvent) {
return;
}
if ($event->getServer() === null) {
return;
}
$authPlugin = $event->getServer()->getPlugin('auth');
if ($authPlugin instanceof Plugin) {
$authPlugin->addBackend($this->createPlugin());
}
});
}

/**
* @return OpenIdSabreAuthPlugin
* @throws \OCP\AppFramework\QueryException
* @codeCoverageIgnore
*/
protected function createPlugin(): OpenIdSabreAuthPlugin {
$module = \OC::$server->query(OpenIdConnectAuthModule::class);
return new OpenIdSabreAuthPlugin($this->session,
$this->userSession,
$this->request,
$module,
'principals/');
}
}
89 changes: 89 additions & 0 deletions tests/unit/EventHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace OCA\OpenIdConnect\Tests\Unit;

use OCA\OpenIdConnect\EventHandler;
use OCP\IRequest;
use OCP\ISession;
use OCP\IUserSession;
use OCP\SabrePluginEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Sabre\DAV\Auth\Plugin;
use Sabre\DAV\Server;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase;

class EventHandlerTest extends TestCase {

/**
* @var MockObject | ISession
*/
private $session;
/**
* @var MockObject | EventHandler
*/
private $eventHandler;
/**
* @var MockObject | IUserSession
*/
private $userSession;
/**
* @var MockObject | EventDispatcherInterface
*/
private $dispatcher;
/**
* @var MockObject | IRequest
*/
private $request;

protected function setUp(): void {
parent::setUp();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->request = $this->createMock(IRequest::class);
$this->session = $this->createMock(ISession::class);
$this->userSession = $this->createMock(IUserSession::class);

$this->eventHandler = $this->getMockBuilder(EventHandler::class)
->setConstructorArgs([$this->dispatcher, $this->request, $this->userSession, $this->session])
->setMethods(['createPlugin'])
->getMock();
}

public function testAddListener(): void {
$this->dispatcher->expects(self::once())->method('addListener')->with('OCA\DAV\Connector\Sabre::authInit');
$this->eventHandler->registerEventHandler();
}

public function testListenerDifferentEvent(): void {
$event = $this->createMock(Event::class);
$this->dispatcher->method('addListener')->willReturnCallback(static function ($name, $callback) use ($event) {
$callback($event);
});
$this->eventHandler->expects(self::never())->method('createPlugin');
$this->eventHandler->registerEventHandler();
}

public function testListenerNullServer(): void {
$event = $this->createMock(SabrePluginEvent::class);
$event->expects(self::once())->method('getServer');
$this->dispatcher->method('addListener')->willReturnCallback(static function ($name, $callback) use ($event) {
$callback($event);
});
$this->eventHandler->expects(self::never())->method('createPlugin');
$this->eventHandler->registerEventHandler();
}

public function testListener(): void {
$plugin = $this->createMock(Plugin::class);
$plugin->expects(self::once())->method('addBackend');
$server = $this->createMock(Server::class);
$server->expects(self::once())->method('getPlugin')->willReturn($plugin);
$event = new SabrePluginEvent($server);
$this->dispatcher->method('addListener')->willReturnCallback(static function ($name, $callback) use ($event) {
$callback($event);
});
$this->eventHandler->expects(self::once())->method('createPlugin');
$this->eventHandler->registerEventHandler();
}
}

0 comments on commit ea06fb9

Please sign in to comment.