-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an authentication storage based on access tokens
- Loading branch information
Showing
6 changed files
with
199 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/ZfrOAuth2Module/Server/Authentication/Storage/AccessTokenStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?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 ZfrOAuth2Module\Server\Authentication\Storage; | ||
|
||
use Zend\Authentication\Storage\NonPersistent; | ||
use Zend\Http\Request as HttpRequest; | ||
use ZfrOAuth2\Server\ResourceServer; | ||
|
||
/** | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
* @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(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/ZfrOAuth2Module/Server/Factory/AccessTokenStorageFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?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 ZfrOAuth2Module\Server\Factory; | ||
|
||
use Zend\Http\Request as HttpRequest; | ||
use Zend\ServiceManager\FactoryInterface; | ||
use Zend\ServiceManager\ServiceLocatorInterface; | ||
use ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage; | ||
|
||
/** | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
tests/ZfrOAuth2ModuleTest/Server/Authentication/Storage/AccessTokenStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?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\Authentication\Storage; | ||
|
||
use Zend\Authentication\Result; | ||
use Zend\Http\Request as HttpRequest; | ||
use ZfrOAuth2\Server\Entity\AccessToken; | ||
use ZfrOAuth2Module\Server\Authentication\Storage\AccessTokenStorage; | ||
|
||
/** | ||
* @author Michaël Gallego <mic.gallego@gmail.com> | ||
* @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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters