From 2e27e14075397b7a9739958e4ee4d907fc824754 Mon Sep 17 00:00:00 2001 From: Jimy GAUBE Date: Sun, 21 Feb 2016 20:47:05 +0100 Subject: [PATCH 1/3] Basic couchdb support --- CouchDocument/AccessToken.php | 18 ++++ CouchDocument/AccessTokenManager.php | 18 ++++ CouchDocument/AuthCode.php | 21 +++++ CouchDocument/AuthCodeManager.php | 92 +++++++++++++++++++ CouchDocument/Client.php | 18 ++++ CouchDocument/ClientManager.php | 82 +++++++++++++++++ CouchDocument/RefreshToken.php | 18 ++++ CouchDocument/RefreshTokenManager.php | 18 ++++ CouchDocument/TokenManager.php | 92 +++++++++++++++++++ DependencyInjection/Configuration.php | 2 +- .../FOSOAuthServerExtension.php | 14 +++ Resources/config/couchdb.xml | 3 - .../config/doctrine/AccessToken.couchdb.xml | 6 +- .../config/doctrine/AuthCode.couchdb.xml | 4 +- Resources/config/doctrine/Client.couchdb.xml | 4 +- .../config/doctrine/RefreshToken.couchdb.xml | 4 +- 16 files changed, 401 insertions(+), 13 deletions(-) create mode 100644 CouchDocument/AccessToken.php create mode 100644 CouchDocument/AccessTokenManager.php create mode 100644 CouchDocument/AuthCode.php create mode 100644 CouchDocument/AuthCodeManager.php create mode 100644 CouchDocument/Client.php create mode 100644 CouchDocument/ClientManager.php create mode 100644 CouchDocument/RefreshToken.php create mode 100644 CouchDocument/RefreshTokenManager.php create mode 100644 CouchDocument/TokenManager.php diff --git a/CouchDocument/AccessToken.php b/CouchDocument/AccessToken.php new file mode 100644 index 00000000..8dd9f3ee --- /dev/null +++ b/CouchDocument/AccessToken.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use FOS\OAuthServerBundle\Model\AccessToken as BaseAccessToken; + +class AccessToken extends BaseAccessToken +{ +} diff --git a/CouchDocument/AccessTokenManager.php b/CouchDocument/AccessTokenManager.php new file mode 100644 index 00000000..033e9c0a --- /dev/null +++ b/CouchDocument/AccessTokenManager.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use FOS\OAuthServerBundle\Model\AccessTokenManagerInterface; + +class AccessTokenManager extends TokenManager implements AccessTokenManagerInterface +{ +} diff --git a/CouchDocument/AuthCode.php b/CouchDocument/AuthCode.php new file mode 100644 index 00000000..9111ef1d --- /dev/null +++ b/CouchDocument/AuthCode.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use FOS\OAuthServerBundle\Model\AuthCode as BaseAuthCode; + +/** + * @author Richard Fullmer + */ +class AuthCode extends BaseAuthCode +{ +} diff --git a/CouchDocument/AuthCodeManager.php b/CouchDocument/AuthCodeManager.php new file mode 100644 index 00000000..9609c076 --- /dev/null +++ b/CouchDocument/AuthCodeManager.php @@ -0,0 +1,92 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use Doctrine\ODM\CouchDB\DocumentManager; +use Doctrine\ODM\CouchDB\DocumentRepository; +use FOS\OAuthServerBundle\Model\AuthCodeInterface; +use FOS\OAuthServerBundle\Model\AuthCodeManager as BaseAuthCodeManager; + +class AuthCodeManager extends BaseAuthCodeManager +{ + /** + * @var DocumentManager + */ + protected $dm; + + /** + * @var DocumentRepository + */ + protected $repository; + + /** + * @var string + */ + protected $class; + + public function __construct(DocumentManager $dm, $class) + { + $this->dm = $dm; + $this->repository = $dm->getRepository($class); + $this->class = $class; + } + + /** + * {@inheritdoc} + */ + public function getClass() + { + return $this->class; + } + + /** + * {@inheritdoc} + */ + public function findAuthCodeBy(array $criteria) + { + return $this->repository->findOneBy($criteria); + } + + /** + * {@inheritdoc} + */ + public function updateAuthCode(AuthCodeInterface $authCode) + { + $this->dm->persist($authCode); + $this->dm->flush(); + } + + /** + * {@inheritdoc} + */ + public function deleteAuthCode(AuthCodeInterface $authCode) + { + $this->dm->remove($authCode); + $this->dm->flush(); + } + + /** + * {@inheritdoc} + */ + public function deleteExpired() + { + $result = $this + ->repository + ->createQueryBuilder() + ->remove() + ->field('expiresAt')->lt(time()) + ->getQuery(array('safe' => true)) + ->execute(); + + return $result['n']; + } +} diff --git a/CouchDocument/Client.php b/CouchDocument/Client.php new file mode 100644 index 00000000..33f0a523 --- /dev/null +++ b/CouchDocument/Client.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use FOS\OAuthServerBundle\Model\Client as BaseClient; + +class Client extends BaseClient +{ +} diff --git a/CouchDocument/ClientManager.php b/CouchDocument/ClientManager.php new file mode 100644 index 00000000..e6d67617 --- /dev/null +++ b/CouchDocument/ClientManager.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use Doctrine\ODM\CouchDB\DocumentManager; +use Doctrine\ODM\CouchDB\DocumentRepository; +use FOS\OAuthServerBundle\Model\ClientManager as BaseClientManager; +use FOS\OAuthServerBundle\Model\ClientInterface; + +class ClientManager extends BaseClientManager +{ + /** + * @var DocumentManager + */ + protected $dm; + + /** + * @var DocumentRepository + */ + protected $repository; + + /** + * @var string + */ + protected $class; + + public function __construct(DocumentManager $dm, $class) + { + $this->dm = $dm; + $this->repository = $dm->getRepository($class); + $this->class = $class; + } + + /** + * {@inheritdoc} + */ + public function getClass() + { + return $this->class; + } + + /** + * {@inheritdoc} + */ + public function findClientBy(array $criteria) + { + $client = $this->repository->findOneBy(array("randomId"=>$criteria["randomId"])); + + if ($client != null) + if ($client->getId() == $criteria["id"]) + return $client; + + return null; + } + + /** + * {@inheritdoc} + */ + public function updateClient(ClientInterface $client) + { + $this->dm->persist($client); + $this->dm->flush(); + } + + /** + * {@inheritdoc} + */ + public function deleteClient(ClientInterface $client) + { + $this->dm->remove($client); + $this->dm->flush(); + } +} diff --git a/CouchDocument/RefreshToken.php b/CouchDocument/RefreshToken.php new file mode 100644 index 00000000..521dada6 --- /dev/null +++ b/CouchDocument/RefreshToken.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use FOS\OAuthServerBundle\Model\RefreshToken as BaseRefreshToken; + +class RefreshToken extends BaseRefreshToken +{ +} diff --git a/CouchDocument/RefreshTokenManager.php b/CouchDocument/RefreshTokenManager.php new file mode 100644 index 00000000..46813a50 --- /dev/null +++ b/CouchDocument/RefreshTokenManager.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface; + +class RefreshTokenManager extends TokenManager implements RefreshTokenManagerInterface +{ +} diff --git a/CouchDocument/TokenManager.php b/CouchDocument/TokenManager.php new file mode 100644 index 00000000..2689712d --- /dev/null +++ b/CouchDocument/TokenManager.php @@ -0,0 +1,92 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\OAuthServerBundle\CouchDocument; + +use Doctrine\ODM\CouchDB\DocumentManager; +use Doctrine\ODM\CouchDB\DocumentRepository; +use FOS\OAuthServerBundle\Model\TokenInterface; +use FOS\OAuthServerBundle\Model\TokenManager as BaseTokenManager; + +class TokenManager extends BaseTokenManager +{ + /** + * @var DocumentManager + */ + protected $dm; + + /** + * @var DocumentRepository + */ + protected $repository; + + /** + * @var string + */ + protected $class; + + public function __construct(DocumentManager $dm, $class) + { + $this->dm = $dm; + $this->repository = $dm->getRepository($class); + $this->class = $class; + } + + /** + * {@inheritdoc} + */ + public function getClass() + { + return $this->class; + } + + /** + * {@inheritdoc} + */ + public function findTokenBy(array $criteria) + { + return $this->repository->findOneBy($criteria); + } + + /** + * {@inheritdoc} + */ + public function updateToken(TokenInterface $token) + { + $this->dm->persist($token); + $this->dm->flush(); + } + + /** + * {@inheritdoc} + */ + public function deleteToken(TokenInterface $token) + { + $this->dm->remove($token); + $this->dm->flush(); + } + + /** + * {@inheritdoc} + */ + public function deleteExpired() + { + $result = $this + ->repository + ->createQueryBuilder() + ->remove() + ->field('expiresAt')->lt(time()) + ->getQuery(array('safe' => true)) + ->execute(); + + return $result['n']; + } +} diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 15a30fa9..541c9d7d 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -30,7 +30,7 @@ public function getConfigTreeBuilder() $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('fos_oauth_server'); - $supportedDrivers = array('orm', 'mongodb', 'propel'); + $supportedDrivers = array('orm', 'mongodb', 'propel', 'couchdb'); $rootNode ->children() diff --git a/DependencyInjection/FOSOAuthServerExtension.php b/DependencyInjection/FOSOAuthServerExtension.php index 602de0aa..55a79f03 100644 --- a/DependencyInjection/FOSOAuthServerExtension.php +++ b/DependencyInjection/FOSOAuthServerExtension.php @@ -76,6 +76,20 @@ public function load(array $configs, ContainerBuilder $container) } } + // Handle the MongoDB document manager name in a specific way as it does not have a registry to make it easy + // TODO: change it when bumping the requirement to Symfony 2.1 + if ('couchdb' === $config['db_driver']) { + if (null === $config['model_manager_name']) { + $container->setAlias('fos_oauth_server.document_manager', new Alias('doctrine_couchdb.odm.default_document_manager', false)); + } else { + $container->setAlias('fos_oauth_server.document_manager', new Alias( + sprintf('doctrine.odm.%s_couchdb.document_manager', + $config['model_manager_name']), + false + )); + } + } + // Entity manager factory definition // TODO: Go back to xml configuration when bumping the requirement to Symfony >=2.6 if ('orm' === $config['db_driver']) { diff --git a/Resources/config/couchdb.xml b/Resources/config/couchdb.xml index 15df13c5..9c8698ff 100644 --- a/Resources/config/couchdb.xml +++ b/Resources/config/couchdb.xml @@ -25,9 +25,6 @@ %fos_oauth_server.model.refresh_token.class% - - %fos_oauth_server.model_manager_name% - diff --git a/Resources/config/doctrine/AccessToken.couchdb.xml b/Resources/config/doctrine/AccessToken.couchdb.xml index ae715578..f85b4b6d 100644 --- a/Resources/config/doctrine/AccessToken.couchdb.xml +++ b/Resources/config/doctrine/AccessToken.couchdb.xml @@ -1,9 +1,9 @@ - - - + + + diff --git a/Resources/config/doctrine/AuthCode.couchdb.xml b/Resources/config/doctrine/AuthCode.couchdb.xml index f9316f08..75e4939e 100644 --- a/Resources/config/doctrine/AuthCode.couchdb.xml +++ b/Resources/config/doctrine/AuthCode.couchdb.xml @@ -1,10 +1,10 @@ - + - + diff --git a/Resources/config/doctrine/Client.couchdb.xml b/Resources/config/doctrine/Client.couchdb.xml index b3968446..665ea722 100644 --- a/Resources/config/doctrine/Client.couchdb.xml +++ b/Resources/config/doctrine/Client.couchdb.xml @@ -1,8 +1,8 @@ - - + + diff --git a/Resources/config/doctrine/RefreshToken.couchdb.xml b/Resources/config/doctrine/RefreshToken.couchdb.xml index e59ba38b..afda61bb 100644 --- a/Resources/config/doctrine/RefreshToken.couchdb.xml +++ b/Resources/config/doctrine/RefreshToken.couchdb.xml @@ -1,9 +1,9 @@ - + - + From 18a04608c1ddba9ff19f3c4ae6f44ae79cac535b Mon Sep 17 00:00:00 2001 From: Jimy GAUBE Date: Wed, 9 Mar 2016 11:22:33 +0100 Subject: [PATCH 2/3] Fix refresh token retrieval, indexing token --- CouchDocument/TokenManager.php | 4 +--- Model/TokenManager.php | 3 +++ Resources/config/doctrine/RefreshToken.couchdb.xml | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CouchDocument/TokenManager.php b/CouchDocument/TokenManager.php index 2689712d..d3c7b2d8 100644 --- a/CouchDocument/TokenManager.php +++ b/CouchDocument/TokenManager.php @@ -79,9 +79,7 @@ public function deleteToken(TokenInterface $token) */ public function deleteExpired() { - $result = $this - ->repository - ->createQueryBuilder() + $result = $this->dm->createQuery("symfony", "accesstoken") ->remove() ->field('expiresAt')->lt(time()) ->getQuery(array('safe' => true)) diff --git a/Model/TokenManager.php b/Model/TokenManager.php index 059f4e0e..129a4652 100644 --- a/Model/TokenManager.php +++ b/Model/TokenManager.php @@ -28,6 +28,9 @@ public function createToken() */ public function findTokenByToken($token) { + $result = $this->findTokenBy(array('token' => $token)); + var_dump($result); + die(); return $this->findTokenBy(array('token' => $token)); } } diff --git a/Resources/config/doctrine/RefreshToken.couchdb.xml b/Resources/config/doctrine/RefreshToken.couchdb.xml index afda61bb..78c38690 100644 --- a/Resources/config/doctrine/RefreshToken.couchdb.xml +++ b/Resources/config/doctrine/RefreshToken.couchdb.xml @@ -2,7 +2,7 @@ - + From 9686bd8532b8daf07a84683f48f20c8a7fce53b2 Mon Sep 17 00:00:00 2001 From: trunk Date: Wed, 10 May 2017 21:07:59 +0200 Subject: [PATCH 3/3] Cleaning TokenManager Cleaning --- Model/TokenManager.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Model/TokenManager.php b/Model/TokenManager.php index 129a4652..e86677f9 100644 --- a/Model/TokenManager.php +++ b/Model/TokenManager.php @@ -29,8 +29,6 @@ public function createToken() public function findTokenByToken($token) { $result = $this->findTokenBy(array('token' => $token)); - var_dump($result); - die(); return $this->findTokenBy(array('token' => $token)); } }