From ee910724550e5f644b0a9a7fc7f1a11b1ef52cdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Guti=C3=A9rrez?= Date: Wed, 16 Mar 2016 12:32:21 +0100 Subject: [PATCH] template renderer holds template related information --- src/Grant/AbstractAuthorizeGrant.php | 62 ++-------------- src/Grant/AuthCodeGrant.php | 16 ++--- src/Grant/ImplicitGrant.php | 22 ++---- src/TemplateRenderer/AbstractRenderer.php | 70 +++++++++++++++++++ .../DefaultTemplates/authorize_client.php | 0 .../DefaultTemplates/login_user.php | 0 src/TemplateRenderer/MustacheRenderer.php | 12 ++-- src/TemplateRenderer/PlatesRenderer.php | 10 ++- src/TemplateRenderer/RendererInterface.php | 22 ------ src/TemplateRenderer/SmartyRenderer.php | 14 ++-- src/TemplateRenderer/TwigRenderer.php | 14 ++-- tests/Grant/AuthCodeGrantTest.php | 24 ++----- tests/Grant/ImplicitGrantTest.php | 38 ++-------- 13 files changed, 133 insertions(+), 171 deletions(-) create mode 100644 src/TemplateRenderer/AbstractRenderer.php rename src/{ResponseTypes => TemplateRenderer}/DefaultTemplates/authorize_client.php (100%) rename src/{ResponseTypes => TemplateRenderer}/DefaultTemplates/login_user.php (100%) delete mode 100644 src/TemplateRenderer/RendererInterface.php diff --git a/src/Grant/AbstractAuthorizeGrant.php b/src/Grant/AbstractAuthorizeGrant.php index 9914e989..bab3ee01 100644 --- a/src/Grant/AbstractAuthorizeGrant.php +++ b/src/Grant/AbstractAuthorizeGrant.php @@ -18,73 +18,25 @@ use League\Plates\Engine; abstract class AbstractAuthorizeGrant extends AbstractGrant { /** - * @var null|\League\OAuth2\Server\TemplateRenderer\RendererInterface + * @var \League\OAuth2\Server\TemplateRenderer\RendererInterface */ protected $templateRenderer; /** - * @var null|string - */ - protected $loginTemplate; - - /** - * @var null|string - */ - protected $authorizeTemplate; - - /** - * @param array $data + * Retrieve template renderer. * - * @return string - */ - protected function renderLoginTemplate(array $data = []) - { - return $this->getTemplateRenderer()->render($this->getLoginTemplate(), $data); - } - - /** - * @param array $data - * - * @return string - */ - protected function renderAuthorizeTemplate(array $data = []) - { - return $this->getTemplateRenderer()->render($this->getAuthorizeTemplate(), $data); - } - - /** * @return \League\OAuth2\Server\TemplateRenderer\RendererInterface */ protected function getTemplateRenderer() { if (!$this->templateRenderer instanceof RendererInterface) { - $this->templateRenderer = new PlatesRenderer(new Engine(__DIR__ . '/../ResponseTypes/DefaultTemplates')); + $this->templateRenderer = new PlatesRenderer( + new Engine(__DIR__ . '/../TemplateRenderer/DefaultTemplates'), + 'login_user', + 'authorize_client' + ); } return $this->templateRenderer; } - - /** - * @return string - */ - protected function getLoginTemplate() - { - if (empty($this->loginTemplate)) { - $this->loginTemplate = 'login_user'; - } - - return $this->loginTemplate; - } - - /** - * @return string - */ - protected function getAuthorizeTemplate() - { - if (empty($this->authorizeTemplate)) { - $this->authorizeTemplate = 'authorize_client'; - } - - return $this->authorizeTemplate; - } } diff --git a/src/Grant/AuthCodeGrant.php b/src/Grant/AuthCodeGrant.php index f1db4921..a3fb2e34 100644 --- a/src/Grant/AuthCodeGrant.php +++ b/src/Grant/AuthCodeGrant.php @@ -11,7 +11,7 @@ use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; use League\OAuth2\Server\Repositories\UserRepositoryInterface; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; -use League\OAuth2\Server\TemplateRenderer\RendererInterface; +use League\OAuth2\Server\TemplateRenderer\AbstractRenderer; use League\OAuth2\Server\Utils\KeyCrypt; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response; @@ -29,26 +29,20 @@ class AuthCodeGrant extends AbstractAuthorizeGrant * @param \League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface $refreshTokenRepository * @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository * @param \DateInterval $authCodeTTL - * @param string|null $loginTemplate - * @param string|null $authorizeTemplate - * @param \League\OAuth2\Server\TemplateRenderer\RendererInterface|null $templateRenderer + * @param \League\OAuth2\Server\TemplateRenderer\AbstractRenderer|null $templateRenderer */ public function __construct( AuthCodeRepositoryInterface $authCodeRepository, RefreshTokenRepositoryInterface $refreshTokenRepository, UserRepositoryInterface $userRepository, \DateInterval $authCodeTTL, - $loginTemplate = null, - $authorizeTemplate = null, - RendererInterface $templateRenderer = null + AbstractRenderer $templateRenderer = null ) { $this->setAuthCodeRepository($authCodeRepository); $this->setRefreshTokenRepository($refreshTokenRepository); $this->setUserRepository($userRepository); $this->authCodeTTL = $authCodeTTL; $this->refreshTokenTTL = new \DateInterval('P1M'); - $this->loginTemplate = $loginTemplate; - $this->authorizeTemplate = $authorizeTemplate; $this->templateRenderer = $templateRenderer; } @@ -144,7 +138,7 @@ class AuthCodeGrant extends AbstractAuthorizeGrant // The user hasn't logged in yet so show a login form if ($userId === null) { - $html = $this->renderLoginTemplate([ + $html = $this->getTemplateRenderer()->renderLogin([ 'error' => $loginError, 'postback_uri' => (string) $postbackUri->withQuery($queryString), ]); @@ -154,7 +148,7 @@ class AuthCodeGrant extends AbstractAuthorizeGrant // The user hasn't approved the client yet so show an authorize form if ($userId !== null && $userHasApprovedClient === null) { - $html = $this->renderAuthorizeTemplate([ + $html = $this->getTemplateRenderer()->renderAuthorize([ 'client' => $client, 'scopes' => $scopes, 'postback_uri' => (string) $postbackUri->withQuery($queryString), diff --git a/src/Grant/ImplicitGrant.php b/src/Grant/ImplicitGrant.php index 4e306d76..89a3c2e6 100644 --- a/src/Grant/ImplicitGrant.php +++ b/src/Grant/ImplicitGrant.php @@ -8,7 +8,7 @@ use League\OAuth2\Server\Entities\Interfaces\UserEntityInterface; use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Repositories\UserRepositoryInterface; use League\OAuth2\Server\ResponseTypes\ResponseTypeInterface; -use League\OAuth2\Server\TemplateRenderer\RendererInterface; +use League\OAuth2\Server\TemplateRenderer\AbstractRenderer; use League\OAuth2\Server\Utils\KeyCrypt; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response; @@ -17,21 +17,13 @@ use Zend\Diactoros\Uri; class ImplicitGrant extends AbstractAuthorizeGrant { /** - * @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository - * @param string|null $loginTemplate - * @param string|null $authorizeTemplate - * @param \League\OAuth2\Server\TemplateRenderer\RendererInterface|null $templateRenderer + * @param \League\OAuth2\Server\Repositories\UserRepositoryInterface $userRepository + * @param \League\OAuth2\Server\TemplateRenderer\AbstractRenderer|null $templateRenderer */ - public function __construct( - UserRepositoryInterface $userRepository, - $loginTemplate = null, - $authorizeTemplate = null, - RendererInterface $templateRenderer = null - ) { + public function __construct(UserRepositoryInterface $userRepository, AbstractRenderer $templateRenderer = null) + { $this->setUserRepository($userRepository); $this->refreshTokenTTL = new \DateInterval('P1M'); - $this->loginTemplate = $loginTemplate; - $this->authorizeTemplate = $authorizeTemplate; $this->templateRenderer = $templateRenderer; } @@ -144,7 +136,7 @@ class ImplicitGrant extends AbstractAuthorizeGrant // The user hasn't logged in yet so show a login form if ($userId === null) { - $html = $this->renderLoginTemplate([ + $html = $this->getTemplateRenderer()->renderLogin([ 'error' => $loginError, 'postback_uri' => (string) $postbackUri->withQuery($queryString), ]); @@ -154,7 +146,7 @@ class ImplicitGrant extends AbstractAuthorizeGrant // The user hasn't approved the client yet so show an authorize form if ($userId !== null && $userHasApprovedClient === null) { - $html = $this->renderAuthorizeTemplate([ + $html = $this->getTemplateRenderer()->renderAuthorize([ 'client' => $client, 'scopes' => $scopes, 'postback_uri' => (string) $postbackUri->withQuery($queryString), diff --git a/src/TemplateRenderer/AbstractRenderer.php b/src/TemplateRenderer/AbstractRenderer.php new file mode 100644 index 00000000..b8c3fa63 --- /dev/null +++ b/src/TemplateRenderer/AbstractRenderer.php @@ -0,0 +1,70 @@ + + * @copyright Copyright (c) Alex Bilbie + * @license http://mit-license.org/ + * + * @link https://github.com/thephpleague/oauth2-server + */ +namespace League\OAuth2\Server\TemplateRenderer; + +abstract class AbstractRenderer +{ + /** + * @var string + */ + protected $loginTemplate; + + /** + * @var string + */ + protected $authorizeTemplate; + + /** + * PlatesRenderer constructor. + * + * @param string $loginTemplate + * @param string $authorizeTemplate + */ + public function __construct($loginTemplate, $authorizeTemplate) + { + $this->loginTemplate = $loginTemplate; + $this->authorizeTemplate = $authorizeTemplate; + } + + /** + * Render login template. + * + * @param array $data + * + * @return string + */ + public function renderLogin(array $data = []) + { + return $this->render($this->loginTemplate, $data); + } + + /** + * Render authorize template. + * + * @param array $data + * + * @return string + */ + public function renderAuthorize(array $data = []) + { + return $this->render($this->authorizeTemplate, $data); + } + + /** + * Render template. + * + * @param string $template + * @param array $data + * + * @return string + */ + abstract protected function render($template, array $data = []); +} diff --git a/src/ResponseTypes/DefaultTemplates/authorize_client.php b/src/TemplateRenderer/DefaultTemplates/authorize_client.php similarity index 100% rename from src/ResponseTypes/DefaultTemplates/authorize_client.php rename to src/TemplateRenderer/DefaultTemplates/authorize_client.php diff --git a/src/ResponseTypes/DefaultTemplates/login_user.php b/src/TemplateRenderer/DefaultTemplates/login_user.php similarity index 100% rename from src/ResponseTypes/DefaultTemplates/login_user.php rename to src/TemplateRenderer/DefaultTemplates/login_user.php diff --git a/src/TemplateRenderer/MustacheRenderer.php b/src/TemplateRenderer/MustacheRenderer.php index e5287eac..fbc0abe9 100644 --- a/src/TemplateRenderer/MustacheRenderer.php +++ b/src/TemplateRenderer/MustacheRenderer.php @@ -10,9 +10,7 @@ */ namespace League\OAuth2\Server\TemplateRenderer; -use Mustache_Engine; - -class MustacheRenderer implements RendererInterface +class MustacheRenderer extends AbstractRenderer { /** * @var \Mustache_Engine @@ -20,12 +18,16 @@ class MustacheRenderer implements RendererInterface private $engine; /** - * TwigRenderer constructor. + * PlatesRenderer constructor. * * @param \Mustache_Engine $engine + * @param string $loginTemplate + * @param string $authorizeTemplate */ - public function __construct(Mustache_Engine $engine) + public function __construct(\Mustache_Engine $engine, $loginTemplate, $authorizeTemplate) { + parent::__construct($loginTemplate, $authorizeTemplate); + $this->engine = $engine; } diff --git a/src/TemplateRenderer/PlatesRenderer.php b/src/TemplateRenderer/PlatesRenderer.php index 114815af..6cd4a77b 100644 --- a/src/TemplateRenderer/PlatesRenderer.php +++ b/src/TemplateRenderer/PlatesRenderer.php @@ -12,7 +12,7 @@ namespace League\OAuth2\Server\TemplateRenderer; use League\Plates\Engine; -class PlatesRenderer implements RendererInterface +class PlatesRenderer extends AbstractRenderer { /** * @var \League\Plates\Engine @@ -23,16 +23,20 @@ class PlatesRenderer implements RendererInterface * PlatesRenderer constructor. * * @param \League\Plates\Engine $engine + * @param string $loginTemplate + * @param string $authorizeTemplate */ - public function __construct(Engine $engine) + public function __construct(Engine $engine, $loginTemplate, $authorizeTemplate) { + parent::__construct($loginTemplate, $authorizeTemplate); + $this->engine = $engine; } /** * {@inheritdoc} */ - public function render($template, array $data = []) + protected function render($template, array $data = []) { if ($this->engine->getFileExtension()) { $template = preg_replace(sprintf('/\.%s$/', $this->engine->getFileExtension()), '', $template); diff --git a/src/TemplateRenderer/RendererInterface.php b/src/TemplateRenderer/RendererInterface.php deleted file mode 100644 index 676334ab..00000000 --- a/src/TemplateRenderer/RendererInterface.php +++ /dev/null @@ -1,22 +0,0 @@ - - * @copyright Copyright (c) Alex Bilbie - * @license http://mit-license.org/ - * - * @link https://github.com/thephpleague/oauth2-server - */ -namespace League\OAuth2\Server\TemplateRenderer; - -interface RendererInterface -{ - /** - * @param string $template - * @param array $data - * - * @return string - */ - public function render($template, array $data = []); -} diff --git a/src/TemplateRenderer/SmartyRenderer.php b/src/TemplateRenderer/SmartyRenderer.php index a54a4719..a69185e1 100644 --- a/src/TemplateRenderer/SmartyRenderer.php +++ b/src/TemplateRenderer/SmartyRenderer.php @@ -10,9 +10,7 @@ */ namespace League\OAuth2\Server\TemplateRenderer; -use Smarty; - -class SmartyRenderer implements RendererInterface +class SmartyRenderer extends AbstractRenderer { /** * Smarty class. @@ -22,17 +20,23 @@ class SmartyRenderer implements RendererInterface private $smarty; /** + * PlatesRenderer constructor. + * * @param \Smarty $smarty + * @param string $loginTemplate + * @param string $authorizeTemplate */ - public function __construct(Smarty $smarty) + public function __construct(\Smarty $smarty, $loginTemplate, $authorizeTemplate) { + parent::__construct($loginTemplate, $authorizeTemplate); + $this->smarty = $smarty; } /** * {@inheritdoc} */ - public function render($template, array $data = []) + protected function render($template, array $data = []) { $this->smarty->assign($data); diff --git a/src/TemplateRenderer/TwigRenderer.php b/src/TemplateRenderer/TwigRenderer.php index 37e3f565..e4e0c13d 100644 --- a/src/TemplateRenderer/TwigRenderer.php +++ b/src/TemplateRenderer/TwigRenderer.php @@ -10,9 +10,7 @@ */ namespace League\OAuth2\Server\TemplateRenderer; -use Twig_Environment; - -class TwigRenderer implements RendererInterface +class TwigRenderer extends AbstractRenderer { /** * @var \Twig_Environment @@ -20,19 +18,23 @@ class TwigRenderer implements RendererInterface private $environment; /** - * TwigRenderer constructor. + * PlatesRenderer constructor. * * @param \Twig_Environment $environment + * @param string $loginTemplate + * @param string $authorizeTemplate */ - public function __construct(Twig_Environment $environment) + public function __construct(\Twig_Environment $environment, $loginTemplate, $authorizeTemplate) { + parent::__construct($loginTemplate, $authorizeTemplate); + $this->environment = $environment; } /** * {@inheritdoc} */ - public function render($template, array $data = []) + protected function render($template, array $data = []) { return $this->environment->loadTemplate($template)->render($data); } diff --git a/tests/Grant/AuthCodeGrantTest.php b/tests/Grant/AuthCodeGrantTest.php index 230c9442..54973d8e 100644 --- a/tests/Grant/AuthCodeGrantTest.php +++ b/tests/Grant/AuthCodeGrantTest.php @@ -28,9 +28,7 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase $this->getMock(AuthCodeRepositoryInterface::class), $this->getMock(RefreshTokenRepositoryInterface::class), $this->getMock(UserRepositoryInterface::class), - new \DateInterval('PT10M'), - 'foo/bar.php', - 'foo/bar.php' + new \DateInterval('PT10M') ); $this->assertEquals('authorization_code', $grant->getIdentifier()); @@ -77,9 +75,7 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase $this->getMock(AuthCodeRepositoryInterface::class), $this->getMock(RefreshTokenRepositoryInterface::class), $userRepositoryMock, - new \DateInterval('PT10M'), - '', - '' + new \DateInterval('PT10M') ); $grant->setClientRepository($clientRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); @@ -134,9 +130,7 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase $this->getMock(AuthCodeRepositoryInterface::class), $this->getMock(RefreshTokenRepositoryInterface::class), $userRepositoryMock, - new \DateInterval('PT10M'), - '', - '' + new \DateInterval('PT10M') ); $grant->setClientRepository($clientRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); @@ -196,9 +190,7 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase $this->getMock(AuthCodeRepositoryInterface::class), $this->getMock(RefreshTokenRepositoryInterface::class), $userRepositoryMock, - new \DateInterval('PT10M'), - '', - '' + new \DateInterval('PT10M') ); $grant->setClientRepository($clientRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); @@ -365,9 +357,7 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase $this->getMock(AuthCodeRepositoryInterface::class), $this->getMock(RefreshTokenRepositoryInterface::class), $userRepositoryMock, - new \DateInterval('PT10M'), - '', - '' + new \DateInterval('PT10M') ); $grant->setClientRepository($clientRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); @@ -417,9 +407,7 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase $this->getMock(AuthCodeRepositoryInterface::class), $this->getMock(RefreshTokenRepositoryInterface::class), $userRepositoryMock, - new \DateInterval('PT10M'), - '', - '' + new \DateInterval('PT10M') ); $grant->setClientRepository($clientRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); diff --git a/tests/Grant/ImplicitGrantTest.php b/tests/Grant/ImplicitGrantTest.php index 9d075a69..5bc1156e 100644 --- a/tests/Grant/ImplicitGrantTest.php +++ b/tests/Grant/ImplicitGrantTest.php @@ -18,22 +18,14 @@ class ImplicitGrantTest extends \PHPUnit_Framework_TestCase { public function testGetIdentifier() { - $grant = new ImplicitGrant( - $this->getMock(UserRepositoryInterface::class), - 'foo/bar.php', - 'foo/bar.php' - ); + $grant = new ImplicitGrant($this->getMock(UserRepositoryInterface::class)); $this->assertEquals('implicit', $grant->getIdentifier()); } public function testCanRespondToRequest() { - $grant = new ImplicitGrant( - $this->getMock(UserRepositoryInterface::class), - 'foo/bar.php', - 'foo/bar.php' - ); + $grant = new ImplicitGrant($this->getMock(UserRepositoryInterface::class)); $request = new ServerRequest( [], @@ -65,7 +57,7 @@ class ImplicitGrantTest extends \PHPUnit_Framework_TestCase $accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); $accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf(); - $grant = new ImplicitGrant($userRepositoryMock, 'foo', 'foo'); + $grant = new ImplicitGrant($userRepositoryMock); $grant->setClientRepository($clientRepositoryMock); $grant->setAccessTokenRepository($accessTokenRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); @@ -106,11 +98,7 @@ class ImplicitGrantTest extends \PHPUnit_Framework_TestCase */ public function testRespondToAuthorizationRequestMissingClientId() { - $grant = new ImplicitGrant( - $this->getMock(UserRepositoryInterface::class), - 'foo/bar.php', - 'foo/bar.php' - ); + $grant = new ImplicitGrant($this->getMock(UserRepositoryInterface::class)); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); $grant->setPathToPrivateKey('file://' . __DIR__ . '/../Utils/private.key'); @@ -151,11 +139,7 @@ class ImplicitGrantTest extends \PHPUnit_Framework_TestCase $clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); $clientRepositoryMock->method('getClientEntity')->willReturn($client); - $grant = new ImplicitGrant( - $this->getMock(UserRepositoryInterface::class), - 'foo/bar.php', - 'foo/bar.php' - ); + $grant = new ImplicitGrant($this->getMock(UserRepositoryInterface::class)); $grant->setClientRepository($clientRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); $grant->setPathToPrivateKey('file://' . __DIR__ . '/../Utils/private.key'); @@ -205,11 +189,7 @@ class ImplicitGrantTest extends \PHPUnit_Framework_TestCase $userEntity = new UserEntity(); $userRepositoryMock->method('getUserEntityByUserCredentials')->willReturn($userEntity); - $grant = new ImplicitGrant( - $this->getMock(UserRepositoryInterface::class), - 'foo/bar.php', - 'foo/bar.php' - ); + $grant = new ImplicitGrant($this->getMock(UserRepositoryInterface::class)); $grant->setClientRepository($clientRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); $grant->setPathToPrivateKey('file://' . __DIR__ . '/../Utils/private.key'); @@ -265,11 +245,7 @@ class ImplicitGrantTest extends \PHPUnit_Framework_TestCase $userEntity = new UserEntity(); $userRepositoryMock->method('getUserEntityByUserCredentials')->willReturn($userEntity); - $grant = new ImplicitGrant( - $this->getMock(UserRepositoryInterface::class), - 'foo/bar.php', - 'foo/bar.php' - ); + $grant = new ImplicitGrant($this->getMock(UserRepositoryInterface::class)); $grant->setClientRepository($clientRepositoryMock); $grant->setPathToPublicKey('file://' . __DIR__ . '/../Utils/public.key'); $grant->setPathToPrivateKey('file://' . __DIR__ . '/../Utils/private.key');