From 3de1b5917a6ebd8991fe905ce905b0f99f33f99b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Guti=C3=A9rrez?= Date: Fri, 15 Jan 2016 12:41:48 +0100 Subject: [PATCH 1/2] deferred default objects creation --- src/Server.php | 71 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 57 insertions(+), 14 deletions(-) diff --git a/src/Server.php b/src/Server.php index b18bddf4..0a158a6d 100644 --- a/src/Server.php +++ b/src/Server.php @@ -2,7 +2,6 @@ namespace League\OAuth2\Server; -use DateInterval; use League\Event\EmitterAwareInterface; use League\Event\EmitterAwareTrait; use League\OAuth2\Server\Exception\OAuthServerException; @@ -31,6 +30,11 @@ class Server implements EmitterAwareInterface */ protected $grantTypeAccessTokenTTL = []; + /** + * @var string + */ + protected $defaultPrivateKeyPath; + /** * @var ResponseTypeInterface */ @@ -49,12 +53,13 @@ class Server implements EmitterAwareInterface /** * New server instance * - * @param string $pathToPrivateKey + * @param string $defaultPrivateKeyPath + * @param DateInterval $defaultAccessTokenTTL */ - public function __construct($pathToPrivateKey) + public function __construct($defaultPrivateKeyPath, \DateInterval $defaultAccessTokenTTL = null) { - $this->setDefaultResponseType(new BearerTokenResponse($pathToPrivateKey)); - $this->setDefaultAccessTokenTTL(new DateInterval('PT01H')); // default token TTL of 1 hour + $this->defaultPrivateKeyPath = $defaultPrivateKeyPath; + $this->defaultAccessTokenTTL = $defaultAccessTokenTTL; } /** @@ -67,6 +72,44 @@ class Server implements EmitterAwareInterface $this->defaultResponseType = $defaultTokenType; } + /** + * Get the default token type that grants will return + * + * @return ResponseTypeInterface + */ + protected function getDefaultResponseType() + { + if (!$this->defaultResponseType instanceof ResponseTypeInterface) { + $this->defaultResponseType = new BearerTokenResponse($this->defaultPrivateKeyPath); + } + + return $this->defaultResponseType; + } + + /** + * Set the default TTL of access tokens + * + * @param DateInterval $defaultAccessTokenTTL + */ + public function setDefaultAccessTokenTTL(\DateInterval $defaultAccessTokenTTL) + { + $this->defaultAccessTokenTTL = $defaultAccessTokenTTL; + } + + /** + * Get the default TTL of access tokens + * + * @return DateInterval + */ + protected function getDefaultAccessTokenTTL() + { + if (!$this->defaultAccessTokenTTL instanceof \DateInterval) { + $this->defaultAccessTokenTTL = new \DateInterval('PT01H'); // default token TTL of 1 hour + } + + return $this->defaultAccessTokenTTL; + } + /** * Set the delimiter string used to separate scopes in a request * @@ -78,13 +121,13 @@ class Server implements EmitterAwareInterface } /** - * Set the default TTL of access tokens + * Get the delimiter string used to separate scopes in a request * - * @param DateInterval $defaultAccessTokenTTL + * @return string */ - public function setDefaultAccessTokenTTL(DateInterval $defaultAccessTokenTTL) + protected function getScopeDelimiterString() { - $this->defaultAccessTokenTTL = $defaultAccessTokenTTL; + return $this->scopeDelimiterString; } /** @@ -97,7 +140,7 @@ class Server implements EmitterAwareInterface public function enableGrantType( GrantTypeInterface $grantType, ResponseTypeInterface $responseType = null, - DateInterval $accessTokenTTL = null + \DateInterval $accessTokenTTL = null ) { $grantType->setEmitter($this->getEmitter()); $this->enabledGrantTypes[$grantType->getIdentifier()] = $grantType; @@ -106,14 +149,14 @@ class Server implements EmitterAwareInterface if ($responseType instanceof ResponseTypeInterface) { $this->grantResponseTypes[$grantType->getIdentifier()] = $responseType; } else { - $this->grantResponseTypes[$grantType->getIdentifier()] = $this->defaultResponseType; + $this->grantResponseTypes[$grantType->getIdentifier()] = $this->getDefaultResponseType(); } // Set grant access token TTL - if ($accessTokenTTL instanceof DateInterval) { + if ($accessTokenTTL instanceof \DateInterval) { $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $accessTokenTTL; } else { - $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $this->defaultAccessTokenTTL; + $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()] = $this->getDefaultAccessTokenTTL(); } } @@ -138,7 +181,7 @@ class Server implements EmitterAwareInterface $request, $this->grantResponseTypes[$grantType->getIdentifier()], $this->grantTypeAccessTokenTTL[$grantType->getIdentifier()], - $this->scopeDelimiterString + $this->getScopeDelimiterString() ); } } From c4830608a2d01a303dda46cbe10f553e45877d21 Mon Sep 17 00:00:00 2001 From: Hannes Van De Vreken Date: Fri, 15 Jan 2016 16:08:10 +0100 Subject: [PATCH 2/2] Fix markdown syntax --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 84be9e67..8c3c3ca1 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ The following versions of PHP are supported: ## Documentation -The library documentation can be found at [http://oauth2.thephpleague.com)(http://oauth2.thephpleague.com). +The library documentation can be found at [http://oauth2.thephpleague.com](http://oauth2.thephpleague.com). You can contribute to this documentation in the [gh-pages branch](https://github.com/thephpleague/oauth2-server/tree/gh-pages/). ## Changelog