Skip to content

Commit

Permalink
Merge pull request #27 from thiakil/new-token-supplier
Browse files Browse the repository at this point in the history
New token supplier
  • Loading branch information
kamermans authored Dec 10, 2020
2 parents 8741be9 + 50de8b9 commit 18fb67c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
71 changes: 52 additions & 19 deletions src/OAuth2Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace kamermans\OAuth2;

use GuzzleHttp\Exception\BadResponseException;
use kamermans\OAuth2\GrantType\GrantTypeInterface;
use kamermans\OAuth2\Utils\Helper;

/**
* OAuth2 plugin.
Expand All @@ -16,49 +14,66 @@ class OAuth2Handler
/**
* The grant type implementation used to acquire access tokens.
*
* @var GrantTypeInterface
* @var GrantType\GrantTypeInterface
*/
protected $grantType;

/**
* The grant type implementation used to refresh access tokens.
*
* @var GrantTypeInterface
* @var GrantType\GrantTypeInterface
*/
protected $refreshTokenGrantType;

/**
* The service in charge of including client credentials into requests.
* to get an access token.
*
* @var AccessTokenSigner
* @var Signer\ClientCredentials\SignerInterface
*/
protected $clientCredentialsSigner;

/**
* The service in charge of including the access token into requests.
*
* @var AccessTokenSigner
* @var Signer\AccessToken\SignerInterface
*/
protected $accessTokenSigner;

/**
* The object including access token.
*
* @var TokenInterface
* @var Token\TokenInterface
*/
protected $rawToken;

/**
* The service in charge of persisting access token.
*
* @var TokenPersistenceInterface
* @var Persistence\TokenPersistenceInterface
*/
protected $tokenPersistence;

/**
* @param GrantTypeInterface $grantType
* @param GrantTypeInterface $refreshTokenGrantType
* Callable used to instantiate a blank TokenInterface instance.
* Called with no arguments, must return a newly constructed class implementing TokenInterface
*
* @var callable
*/
protected $newTokenSupplier;

/**
* Factory responsible for parsing server token response
*
* @var callable
*/
protected $tokenFactory;

/**
* @param GrantType\GrantTypeInterface $grantType
* @param GrantType\GrantTypeInterface|null $refreshTokenGrantType
* @param Signer\ClientCredentials\SignerInterface|null $clientCredentialsSigner
* @param Signer\AccessToken\SignerInterface|null $accessTokenSigner
*/
public function __construct(
GrantType\GrantTypeInterface $grantType,
Expand All @@ -81,10 +96,13 @@ public function __construct(

$this->tokenPersistence = new Persistence\NullTokenPersistence();
$this->tokenFactory = new Token\RawTokenFactory();
$this->newTokenSupplier = function(){ return new Token\RawToken(); };
}

/**
* @param Signer\ClientCredentials\SignerInterface $signer
*
* @return self
*/
public function setClientCredentialsSigner(Signer\ClientCredentials\SignerInterface $signer)
{
Expand All @@ -94,7 +112,9 @@ public function setClientCredentialsSigner(Signer\ClientCredentials\SignerInterf
}

/**
* @param AccessToken\SignerInterface $signer
* @param Signer\AccessToken\SignerInterface $signer
*
* @return self
*/
public function setAccessTokenSigner(Signer\AccessToken\SignerInterface $signer)
{
Expand All @@ -105,6 +125,8 @@ public function setAccessTokenSigner(Signer\AccessToken\SignerInterface $signer)

/**
* @param Persistence\TokenPersistenceInterface $tokenPersistence
*
* @return self
*/
public function setTokenPersistence(Persistence\TokenPersistenceInterface $tokenPersistence)
{
Expand All @@ -115,6 +137,8 @@ public function setTokenPersistence(Persistence\TokenPersistenceInterface $token

/**
* @param callable $tokenFactory
*
* @return self
*/
public function setTokenFactory(callable $tokenFactory)
{
Expand All @@ -123,12 +147,23 @@ public function setTokenFactory(callable $tokenFactory)
return $this;
}

/**
* @param callable $tokenSupplier the new token supplier
*
* @return self
*/
public function setNewTokenSupplier(callable $tokenSupplier) {
$this->newTokenSupplier = $tokenSupplier;

return $this;
}

/**
* Manually set the access token.
*
* @param string|array|TokenInterface $token An array of token data, an access token string, or a TokenInterface object
* @param string|array|Token\TokenInterface $token An array of token data, an access token string, or a TokenInterface object
*
* @return self
*/
public function setAccessToken($token)
{
Expand Down Expand Up @@ -161,13 +196,13 @@ public function deleteAccessToken()
*
* @return string|null A valid access token or null if unable to get one
*
* @throws AccessTokenRequestException while trying to run `requestNewAccessToken` method
* @throws Exception\AccessTokenRequestException while trying to run `requestNewAccessToken` method
*/
public function getAccessToken()
{
// If token is not set try to get it from the persistent storage.
if ($this->rawToken === null) {
$this->rawToken = $this->tokenPersistence->restoreToken(new Token\RawToken());
$this->rawToken = $this->tokenPersistence->restoreToken(call_user_func($this->newTokenSupplier));
}

// If token is not set or expired then try to acquire a new one...
Expand All @@ -189,7 +224,7 @@ public function getAccessToken()
/**
* Gets the current Token object
*
* @return Token\RawToken|null
* @return Token\TokenInterface|null
*/
public function getRawToken()
{
Expand All @@ -210,7 +245,7 @@ protected function signRequest($request)
/**
* Helper method for (callable)tokenFactory
*
* @return TokenInterface
* @return Token\TokenInterface
*/
protected function tokenFactory()
{
Expand All @@ -220,9 +255,7 @@ protected function tokenFactory()
/**
* Acquire a new access token from the server.
*
* @return TokenInterface|null
*
* @throws AccessTokenRequestException
* @throws Exception\AccessTokenRequestException
*/
protected function requestNewAccessToken()
{
Expand Down
18 changes: 11 additions & 7 deletions src/OAuth2Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,17 @@ public function __invoke(callable $handler)
}

/**
* Request error event handler.
*
* Handles unauthorized errors by acquiring a new access token and
* retrying the request.
*
* @param ErrorEvent $event Event received
*/
* Request error event handler.
*
* Handles unauthorized errors by acquiring a new access token and
* retrying the request.
*
* @param \Psr\Http\Message\RequestInterface $request
* @param array $options
* @param callable $handler
*
* @return callable
*/
private function onFulfilled(RequestInterface $request, array $options, $handler)
{
return function ($response) use ($request, $options, $handler) {
Expand Down

0 comments on commit 18fb67c

Please sign in to comment.