Skip to content

Commit

Permalink
Allow to delete old tokens through console
Browse files Browse the repository at this point in the history
  • Loading branch information
bakura10 committed Jan 18, 2014
1 parent c69263a commit 848c9aa
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"zendframework/zend-servicemanager": "~2.2",
"zendframework/zend-modulemanager": "~2.2",
"zendframework/zend-mvc": "~2.2",
"zendframework/zend-console": "~2.2",
"zendframework/zend-stdlib": "~2.2",
"doctrine/doctrine-module": "0.8.*@dev",
"zfr/zfr-oauth2-server": "dev-master"
Expand Down
17 changes: 17 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@
]
],

'console' => [
'router' => [
'routes' => [
'delete-expired-tokens' => [
'type' => 'Simple',
'options' => [
'route' => 'oauth2 delete expired tokens',
'defaults' => [
'controller' => 'ZfrOAuth2Module\Server\Controller\TokenController',
'action' => 'delete-expired-tokens'
]
]
]
]
]
],

'controllers' => [
'factories' => [
'ZfrOAuth2Module\Server\Controller\AuthorizationController' => 'ZfrOAuth2Module\Server\Factory\AuthorizeControllerFactory',
Expand Down
36 changes: 24 additions & 12 deletions src/ZfrOAuth2Module/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,54 @@

namespace ZfrOAuth2Module;

use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
use Zend\Console\Adapter\AdapterInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ConsoleBannerProviderInterface;
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface;
use Zend\ModuleManager\Feature\DependencyIndicatorInterface;

/**
* Module
*
* @license MIT
*/
class Module implements BootstrapListenerInterface, ConfigProviderInterface, DependencyIndicatorInterface
class Module implements
ConfigProviderInterface,
ConsoleBannerProviderInterface,
ConsoleUsageProviderInterface,
DependencyIndicatorInterface
{
/**
* {@inheritDoc}
*/
public function onBootstrap(EventInterface $event)
public function getConfig()
{
/* @var $application \Zend\Mvc\Application */
$application = $event->getTarget();
$serviceManager = $application->getServiceManager();
$eventManager = $application->getEventManager();
return include __DIR__ . '/../../config/module.config.php';
}

/**
* {@inheritDoc}
*/
public function getConfig()
public function getModuleDependencies()
{
return include __DIR__ . '/../../config/module.config.php';
return ['DoctrineModule'];
}

/**
* {@inheritDoc}
*/
public function getModuleDependencies()
public function getConsoleBanner(AdapterInterface $console)
{
return ['DoctrineModule'];
return 'ZfrOAuth2Server';
}

/**
* {@inheritDoc}
*/
public function getConsoleUsage(AdapterInterface $console)
{
return [
'oauth2 delete expired tokens' => 'Delete expired access tokens'
];
}
}
21 changes: 21 additions & 0 deletions src/ZfrOAuth2Module/Server/Controller/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

namespace ZfrOAuth2Module\Server\Controller;

use Zend\Console\Request as ConsoleRequest;
use Zend\Http\Request as HttpRequest;
use Zend\Mvc\Controller\AbstractActionController;
use ZfrOAuth2\Server\AuthorizationServer;
use ZfrOAuth2Module\Server\Exception\RuntimeException;

/**
* @author Michaël Gallego <mic.gallego@gmail.com>
Expand Down Expand Up @@ -55,4 +57,23 @@ public function tokenAction()

return $this->authorizationServer->handleTokenRequest($this->request);
}

/**
* Delete expired tokens
*
* @return string
* @throws RuntimeException
*/
public function deleteExpiredTokensAction()
{
if (!$this->request instanceof ConsoleRequest) {
throw new RuntimeException('You can only use this action from console');
}

/* @var \ZfrOAuth2\Server\Service\TokenService $accessTokenService */
$accessTokenService = $this->serviceLocator->get('ZfrOAuth2\Server\Service\AccessTokenService');
$accessTokenService->deleteExpiredTokens();

return 'Expired access tokens were properly deleted';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrOAuth2ModuleTest\Server\Factory;

use Zend\ServiceManager\ServiceManager;
use ZfrOAuth2Module\Server\Factory\AuthorizationControllerFactory;
use ZfrOAuth2Module\Server\Factory\AuthorizationGrantFactory;

/**
* @author Michaël Gallego <mic.gallego@gmail.com>
* @licence MIT
*
* @covers ZfrOAuth2Module\Server\Factory\AuthorizationControllerFactory
*/
class AuthorizationControllerFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCanCreateFromFactory()
{
$serviceManager = new ServiceManager();

$pluginManager = $this->getMock('Zend\ServiceManager\AbstractPluginManager');
$pluginManager->expects($this->once())->method('getServiceLocator')->will($this->returnValue($serviceManager));

$serviceManager->setService(
'ZfrOAuth2\Server\AuthorizationServer',
$this->getMock('ZfrOAuth2\Server\AuthorizationServer', [], [], '', false)
);

$factory = new AuthorizationControllerFactory();
$service = $factory->createService($pluginManager);

$this->assertInstanceOf('ZfrOAuth2Module\Server\Controller\AuthorizationController', $service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace ZfrOAuth2ModuleTest\Server\Factory;

use Zend\ServiceManager\ServiceManager;
use ZfrOAuth2Module\Server\Factory\ModuleOptionsFactory;

/**
* @author Michaël Gallego <mic.gallego@gmail.com>
* @licence MIT
*
* @covers ZfrOAuth2Module\Server\Factory\ModuleOptionsFactory
*/
class ModuleOptionsFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCanCreateFromFactory()
{
$serviceManager = new ServiceManager();

$serviceManager->setService('Config', ['zfr_oauth2_server' => []]);

$factory = new ModuleOptionsFactory();
$service = $factory->createService($serviceManager);

$this->assertInstanceOf('ZfrOAuth2Module\Server\Options\ModuleOptions', $service);
}
}

0 comments on commit 848c9aa

Please sign in to comment.