Remove OAuthListener Response Listener after processing

This commit is contained in:
Benoît Burnichon
2015-10-29 12:51:34 +01:00
parent f9c02131f1
commit 574377dea6
3 changed files with 97 additions and 46 deletions

View File

@@ -24,7 +24,6 @@ use Alchemy\Phrasea\Model\Manipulator\ApiOauthTokenManipulator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -135,39 +134,9 @@ class OAuthListener
private function registerClosingAccountCallback(EventDispatcherInterface $dispatcher, Application $app)
{
$dispatcher->addListener(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) {
$request = $event->getRequest();
$response = $event->getResponse();
$session = $this->getSession($app);
/** @var ApiOauthToken $token */
$token = $session->get('token');
$this->getApiLogManipulator($app)->create($token->getAccount(), $request, $response);
$this->getApiOAuthTokenManipulator($app)->setLastUsed($token, new \DateTime());
$session->set('token', null);
if (null !== $this->getAuthenticator($app)->getUser()) {
$this->getAuthenticator($app)->closeAccount();
}
}, -20);
$dispatcher->addListener(KernelEvents::RESPONSE, new OAuthResponseListener($app), -20);
}
/**
* @param Application $app
* @return ApiLogManipulator
*/
private function getApiLogManipulator(Application $app)
{
return $app['manipulator.api-log'];
}
/**
* @param Application $app
* @return ApiOauthTokenManipulator
*/
private function getApiOAuthTokenManipulator(Application $app)
{
return $app['manipulator.api-oauth-token'];
}
/**
* @param Application $app

View File

@@ -0,0 +1,80 @@
<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2015 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Event\Listener;
use Alchemy\Phrasea\Application;
use Alchemy\Phrasea\Authentication\Authenticator;
use Alchemy\Phrasea\Model\Entities\ApiOauthToken;
use Alchemy\Phrasea\Model\Manipulator\ApiLogManipulator;
use Alchemy\Phrasea\Model\Manipulator\ApiOauthTokenManipulator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
class OAuthResponseListener
{
/** @var Application */
private $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function __invoke(FilterResponseEvent $event, $eventName, EventDispatcherInterface $dispatcher)
{
$request = $event->getRequest();
$response = $event->getResponse();
$session = $this->getSession();
/** @var ApiOauthToken $token */
$token = $session->get('token');
$this->getApiLogManipulator()->create($token->getAccount(), $request, $response);
$this->getApiOAuthTokenManipulator()->setLastUsed($token, new \DateTime());
$session->set('token', null);
if (null !== $this->getAuthenticator()->getUser()) {
$this->getAuthenticator()->closeAccount();
}
$dispatcher->removeListener($eventName, $this);
}
/**
* @return ApiLogManipulator
*/
private function getApiLogManipulator()
{
return $this->app['manipulator.api-log'];
}
/**
* @return ApiOauthTokenManipulator
*/
private function getApiOAuthTokenManipulator()
{
return $this->app['manipulator.api-oauth-token'];
}
/**
* @return Session
*/
private function getSession()
{
return $this->app['session'];
}
/**
* @return Authenticator
*/
private function getAuthenticator()
{
return $this->app['authentication'];
}
}

View File

@@ -315,32 +315,34 @@ abstract class ApiTestCase extends \PhraseanetWebTestCase
{
$this->setToken($this->userAccessToken);
self::$DI['client']->request('GET', '/api/v1/monitor/tasks/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client = $this->getClient();
$client->request('GET', '/api/v1/monitor/tasks/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']);
self::$DI['client']->request('GET', '/api/v1/monitor/scheduler/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client->request('GET', '/api/v1/monitor/scheduler/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']);
self::$DI['client']->request('GET', '/api/v1/monitor/task/1/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client->request('GET', '/api/v1/monitor/task/1/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']);
self::$DI['client']->request('POST', '/api/v1/monitor/task/1/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client->request('POST', '/api/v1/monitor/task/1/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']);
self::$DI['client']->request('POST', '/api/v1/monitor/task/1/start/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client->request('POST', '/api/v1/monitor/task/1/start/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']);
self::$DI['client']->request('POST', '/api/v1/monitor/task/1/stop/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client->request('POST', '/api/v1/monitor/task/1/stop/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']);
self::$DI['client']->request('GET', '/api/v1/monitor/phraseanet/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize(self::$DI['client']->getResponse()->getContent());
$client->request('GET', '/api/v1/monitor/phraseanet/', $this->getParameters(), [], ['HTTP_Accept' => $this->getAcceptMimeType()]);
$content = $this->unserialize($client->getResponse()->getContent());
$this->assertEquals(401, $content['meta']['http_code']);
}