-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from gregurco/persistent_storage
Persistent storage functionality
- Loading branch information
Showing
6 changed files
with
124 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
namespace Gregurco\Bundle\GuzzleBundleOAuth2Plugin\Middleware; | ||
|
||
use Sainsburys\Guzzle\Oauth2\AccessToken; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\GrantTypeInterface; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\RefreshTokenGrantTypeInterface; | ||
use GuzzleHttp\ClientInterface; | ||
use Sainsburys\Guzzle\Oauth2\Middleware\OAuthMiddleware; | ||
use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||
|
||
class PersistentOAuthMiddleware extends OAuthMiddleware | ||
{ | ||
/** @var SessionInterface */ | ||
protected $session; | ||
|
||
/** @var string */ | ||
protected $clientName; | ||
|
||
/** | ||
* Create a new Oauth2 subscriber. | ||
* | ||
* @param ClientInterface $client | ||
* @param GrantTypeInterface $grantType | ||
* @param RefreshTokenGrantTypeInterface $refreshTokenGrantType | ||
* @param SessionInterface $session | ||
* @param string $clientName | ||
*/ | ||
public function __construct( | ||
ClientInterface $client, | ||
GrantTypeInterface $grantType = null, | ||
RefreshTokenGrantTypeInterface $refreshTokenGrantType = null, | ||
SessionInterface $session, | ||
string $clientName | ||
) { | ||
parent::__construct($client, $grantType, $refreshTokenGrantType); | ||
|
||
$this->session = $session; | ||
$this->clientName = $clientName; | ||
} | ||
|
||
/** | ||
* Get a new access token. | ||
* | ||
* @return AccessToken|null | ||
*/ | ||
protected function acquireAccessToken() | ||
{ | ||
$token = parent::acquireAccessToken(); | ||
|
||
$this->storeTokenInSession($token); | ||
|
||
return $token; | ||
} | ||
|
||
/** | ||
* @param AccessToken $token | ||
*/ | ||
protected function storeTokenInSession(AccessToken $token) | ||
{ | ||
$this->session->start(); | ||
$this->session->set($this->clientName . '_token', [ | ||
'token' => $token->getToken(), | ||
'type' => $token->getType(), | ||
'data' => $token->getData(), | ||
]); | ||
$this->session->save(); | ||
} | ||
|
||
/** | ||
* @return null|AccessToken | ||
*/ | ||
public function getAccessToken() | ||
{ | ||
if ($this->accessToken === null) { | ||
$this->restoreTokenFromSession(); | ||
} | ||
|
||
return parent::getAccessToken(); | ||
} | ||
|
||
protected function restoreTokenFromSession() | ||
{ | ||
if ($this->session->has($this->clientName . '_token')) { | ||
$sessionTokenData = $this->session->get($this->clientName . '_token'); | ||
|
||
$this->setAccessToken(new AccessToken( | ||
$sessionTokenData['token'], | ||
$sessionTokenData['type'], | ||
$sessionTokenData['data'] | ||
)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters