template renderer holds template related information

This commit is contained in:
Julián Gutiérrez 2016-03-16 12:32:21 +01:00
parent 25f93071ea
commit ee91072455
13 changed files with 133 additions and 171 deletions

View File

@ -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;
}
}

View File

@ -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),

View File

@ -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),

View File

@ -0,0 +1,70 @@
<?php
/**
* Base template renderer.
*
* @author Julián Gutiérrez <juliangut@gmail.com>
* @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 = []);
}

View File

@ -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;
}

View File

@ -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);

View File

@ -1,22 +0,0 @@
<?php
/**
* Template renderer Interface.
*
* @author Julián Gutiérrez <juliangut@gmail.com>
* @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 = []);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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');

View File

@ -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');