From c60b29d201b5ec51146a1d28bff3f9bbbdcf4f4e Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Sun, 9 Mar 2014 20:03:05 +0000 Subject: [PATCH] First commit of AuthCode grant and entity --- .../OAuth2/Server/Entity/AbstractToken.php | 2 +- src/League/OAuth2/Server/Entity/AuthCode.php | 78 +++++++++++++++++++ tests/Entities/AuthCodeTest.php | 51 ++++++++++++ 3 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 src/League/OAuth2/Server/Entity/AuthCode.php create mode 100644 tests/Entities/AuthCodeTest.php diff --git a/src/League/OAuth2/Server/Entity/AbstractToken.php b/src/League/OAuth2/Server/Entity/AbstractToken.php index 145e3dda..07cfe988 100644 --- a/src/League/OAuth2/Server/Entity/AbstractToken.php +++ b/src/League/OAuth2/Server/Entity/AbstractToken.php @@ -176,7 +176,7 @@ abstract class AbstractToken * @param array $unformated Array of \League\OAuth2\Server\Entity\Scope * @return array */ - private function formatScopes($unformated = []) + protected function formatScopes($unformated = []) { $scopes = []; foreach ($unformated as $scope) { diff --git a/src/League/OAuth2/Server/Entity/AuthCode.php b/src/League/OAuth2/Server/Entity/AuthCode.php new file mode 100644 index 00000000..70f2052d --- /dev/null +++ b/src/League/OAuth2/Server/Entity/AuthCode.php @@ -0,0 +1,78 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * @link http://github.com/php-loep/oauth2-server + */ + +namespace League\OAuth2\Server\Entity; + +use League\OAuth2\Server\Storage\SessionStorageInterface; +use League\OAuth2\Server\Storage\AccessTokenInterface; +use League\OAuth2\Server\Util\SecureKey; +use League\OAuth2\Server\Exception\InvalidAccessTokenException; +use Symfony\Component\HttpFoundation\ParameterBag; + +/** + * Access token entity class + */ +class AuthCode extends AbstractToken +{ + /** + * {@inheritdoc} + */ + public function getSession() + { + if ($this->session instanceof Session) { + return $this->session; + } + + $this->session = $this->server->getStorage('session')->getByAuthCode($this->token); + return $this->session; + } + + /** + * {@inheritdoc} + */ + public function getScopes() + { + if ($this->scopes === null) { + $this->scopes = $this->formatScopes( + $this->server->getStorage('auth_code')->getScopes($this->getToken()) + ); + } + + return $this->scopes; + } + + /** + * {@inheritdoc} + */ + public function save() + { + $this->server->getStorage('auth_code')->create( + $this->getToken(), + $this->getExpireTime(), + $this->getSession()->getId() + ); + + // Associate the scope with the token + foreach ($this->getScopes() as $scope) { + $this->server->getStorage('auth_code')->associateScope($this->getToken(), $scope->getId()); + } + + return $this; + } + + /** + * {@inheritdoc} + */ + public function expire() + { + $this->server->getStorage('auth_code')->delete($this->getToken()); + } +} diff --git a/tests/Entities/AuthCodeTest.php b/tests/Entities/AuthCodeTest.php new file mode 100644 index 00000000..0d325ea6 --- /dev/null +++ b/tests/Entities/AuthCodeTest.php @@ -0,0 +1,51 @@ +shouldReceive('create'); + $authCodeStorage->shouldReceive('associateScope'); + $authCodeStorage->shouldReceive('setServer'); + $authCodeStorage->shouldReceive('getScopes')->andReturn([ + (new Scope($server))->setId('foo') + ]); + + $sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface'); + $sessionStorage->shouldReceive('getByAuthCode')->andReturn( + (new Session($server)) + ); + $sessionStorage->shouldReceive('setServer'); + + $server->setAuthCodeStorage($authCodeStorage); + $server->setSessionStorage($sessionStorage); + + $entity = new AuthCode($server); + $this->assertTrue($entity->save() instanceof AuthCode); + } + + function testExpire() + { + $server = new Authorization(); + + $authCodeStorage = M::mock('League\OAuth2\Server\Storage\AuthCodeInterface'); + $authCodeStorage->shouldReceive('delete'); + $authCodeStorage->shouldReceive('setServer'); + + $server->setAuthCodeStorage($authCodeStorage); + + $entity = new AuthCode($server); + $this->assertSame($entity->expire(), null); + } +}