diff --git a/config/module.config.php b/config/module.config.php index 096adcc..7b743d8 100644 --- a/config/module.config.php +++ b/config/module.config.php @@ -27,6 +27,7 @@ 'ZfrOAuth2\Server\Service\ClientService' => 'ZfrOAuth2Module\Server\Factory\ClientServiceFactory', 'ZfrOAuth2\Server\Service\ScopeService' => 'ZfrOAuth2Module\Server\Factory\ScopeServiceFactory', 'ZfrOAuth2Module\Server\Authentication\Adapter\AccessTokenAdapter' => 'ZfrOAuth2Module\Server\Factory\AccessTokenAdapterFactory', + 'ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage' => 'ZfrOAuth2Module\Server\Factory\AccessTokenStorageFactory', 'ZfrOAuth2Module\Server\Options\ModuleOptions' => 'ZfrOAuth2Module\Server\Factory\ModuleOptionsFactory', 'ZfrOAuth2Module\Server\Grant\GrantPluginManager' => 'ZfrOAuth2Module\Server\Factory\GrantPluginManagerFactory', diff --git a/src/ZfrOAuth2Module/Server/Authentication/Storage/AccessTokenStorage.php b/src/ZfrOAuth2Module/Server/Authentication/Storage/AccessTokenStorage.php new file mode 100644 index 0000000..9c39cfc --- /dev/null +++ b/src/ZfrOAuth2Module/Server/Authentication/Storage/AccessTokenStorage.php @@ -0,0 +1,75 @@ + + * @licence MIT + */ +class AccessTokenStorage extends NonPersistent +{ + /** + * @var ResourceServer + */ + protected $resourceServer; + + /** + * @var HttpRequest + */ + protected $request; + + /** + * @param ResourceServer $resourceServer + */ + public function __construct(ResourceServer $resourceServer) + { + $this->resourceServer = $resourceServer; + } + + /** + * Set the HTTP request + * + * @param HttpRequest $request + * @return void + */ + public function setRequest(HttpRequest $request) + { + $this->request = $request; + } + + /** + * {@inheritDoc} + */ + public function isEmpty() + { + return $this->resourceServer->getAccessToken($this->request) === null; + } + + /** + * {@inheritDoc} + */ + public function read() + { + return $this->resourceServer->getAccessToken($this->request)->getOwner(); + } +} diff --git a/src/ZfrOAuth2Module/Server/Factory/AccessTokenStorageFactory.php b/src/ZfrOAuth2Module/Server/Factory/AccessTokenStorageFactory.php new file mode 100644 index 0000000..781fd01 --- /dev/null +++ b/src/ZfrOAuth2Module/Server/Factory/AccessTokenStorageFactory.php @@ -0,0 +1,48 @@ + + * @licence MIT + */ +class AccessTokenStorageFactory implements FactoryInterface +{ + /** + * {@inheritDoc} + */ + public function createService(ServiceLocatorInterface $serviceLocator) + { + $accessTokenStorage = new AccessTokenStorage($serviceLocator->get('ZfrOAuth2\Server\ResourceServer')); + + // It only makes sense to set the request if it is HTTP request + $request = $serviceLocator->get('Application')->getRequest(); + + if ($request instanceof HttpRequest) { + $accessTokenStorage->setRequest($request); + } + + return $accessTokenStorage; + } +} diff --git a/src/ZfrOAuth2Module/Server/Factory/AuthenticationServiceFactory.php b/src/ZfrOAuth2Module/Server/Factory/AuthenticationServiceFactory.php index 966d560..ece4fdf 100644 --- a/src/ZfrOAuth2Module/Server/Factory/AuthenticationServiceFactory.php +++ b/src/ZfrOAuth2Module/Server/Factory/AuthenticationServiceFactory.php @@ -19,7 +19,6 @@ namespace ZfrOAuth2Module\Server\Factory; use Zend\Authentication\AuthenticationService; -use Zend\Authentication\Storage\NonPersistent; use Zend\ServiceManager\FactoryInterface; use Zend\ServiceManager\ServiceLocatorInterface; @@ -36,7 +35,7 @@ public function createService(ServiceLocatorInterface $serviceLocator) { // When using an API based on a REST API, the authentication is stateless return new AuthenticationService( - new NonPersistent(), + $serviceLocator->get('ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage'), $serviceLocator->get('ZfrOAuth2Module\Server\Authentication\Adapter\AccessTokenAdapter') ); } diff --git a/tests/ZfrOAuth2ModuleTest/Server/Authentication/Storage/AccessTokenStorageTest.php b/tests/ZfrOAuth2ModuleTest/Server/Authentication/Storage/AccessTokenStorageTest.php new file mode 100644 index 0000000..6737385 --- /dev/null +++ b/tests/ZfrOAuth2ModuleTest/Server/Authentication/Storage/AccessTokenStorageTest.php @@ -0,0 +1,69 @@ + + * @licence MIT + * + * @covers \ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage + */ +class AccessTokenStorageTest extends \PHPUnit_Framework_TestCase +{ + public function testIsConsideredAsEmptyIfNoAccessToken() + { + $resourceServer = $this->getMock('ZfrOAuth2\Server\ResourceServer', [], [], '', false); + $request = new HttpRequest(); + + $storage = new AccessTokenStorage($resourceServer); + $storage->setRequest($request); + + $resourceServer->expects($this->once()) + ->method('getAccessToken') + ->with($request) + ->will($this->returnValue(null)); + + $this->isTrue($storage->isEmpty()); + } + + public function testReadOwnerFromAccessToken() + { + $resourceServer = $this->getMock('ZfrOAuth2\Server\ResourceServer', [], [], '', false); + $request = new HttpRequest(); + + $storage = new AccessTokenStorage($resourceServer); + $storage->setRequest($request); + + $token = new AccessToken(); + $owner = $this->getMock('ZfrOAuth2\Server\Entity\TokenOwnerInterface'); + $token->setOwner($owner); + + $resourceServer->expects($this->once()) + ->method('getAccessToken') + ->with($request) + ->will($this->returnValue($token)); + + $this->assertSame($owner, $storage->read()); + } +} diff --git a/tests/ZfrOAuth2ModuleTest/Server/Factory/AuthenticationServiceFactoryTest.php b/tests/ZfrOAuth2ModuleTest/Server/Factory/AuthenticationServiceFactoryTest.php index 20ef2fd..955b377 100644 --- a/tests/ZfrOAuth2ModuleTest/Server/Factory/AuthenticationServiceFactoryTest.php +++ b/tests/ZfrOAuth2ModuleTest/Server/Factory/AuthenticationServiceFactoryTest.php @@ -33,6 +33,11 @@ public function testCanCreateFromFactory() { $serviceManager = new ServiceManager(); + $serviceManager->setService( + 'ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage', + $this->getMock('Zend\Authentication\Storage\StorageInterface') + ); + $serviceManager->setService( 'ZfrOAuth2Module\Server\Authentication\Adapter\AccessTokenAdapter', $this->getMock('Zend\Authentication\Adapter\AdapterInterface') @@ -42,6 +47,5 @@ public function testCanCreateFromFactory() $service = $factory->createService($serviceManager); $this->assertInstanceOf('Zend\Authentication\AuthenticationService', $service); - $this->assertInstanceOf('Zend\Authentication\Storage\NonPersistent', $service->getStorage()); } }