Skip to content

Commit

Permalink
Merge pull request #11 from gregurco/persistent_storage
Browse files Browse the repository at this point in the history
Persistent storage functionality
  • Loading branch information
gregurco committed Apr 17, 2018
2 parents d9a6025 + e4b34dd commit 0eb6a7a
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 9 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ eight_points_guzzle:
api_payment:
base_url: "http://api.domain.tld"

auth: oauth2
options:
auth: oauth2

# plugin settings
plugin:
Expand All @@ -73,7 +74,8 @@ eight_points_guzzle:
api_payment:
base_url: "http://api.domain.tld"

auth: oauth2
options:
auth: oauth2

# plugin settings
plugin:
Expand All @@ -96,7 +98,8 @@ eight_points_guzzle:
api_payment:
base_url: "http://api.domain.tld"

auth: oauth2
options:
auth: oauth2

# plugin settings
plugin:
Expand All @@ -123,6 +126,7 @@ eight_points_guzzle:
| private_key | Path to private key | for JwtBearer grant type | `"%kernel.root_dir%/path/to/private.key"` |
| scope | One or more scope values indicating which parts of the user's account you wish to access | no | administration |
| grant_type | Grant type class path. Class should implement GrantTypeInterface. <br/> Default: `Sainsburys\\Guzzle\\Oauth2\\GrantType\\ClientCredentials` | no | `Sainsburys\\Guzzle\\Oauth2\\GrantType\\PasswordCredentials`<br/>`Sainsburys\\Guzzle\\Oauth2\\GrantType\\AuthorizationCode`<br/>`Sainsburys\\Guzzle\\Oauth2\\GrantType\\JwtBearer` |
| persistent | Token will be stored in session. <br/> Default: false | no | |

See more information about middleware [here][3].

Expand Down
Empty file modified src/DependencyInjection/GuzzleBundleOAuth2Extension.php
100644 → 100755
Empty file.
25 changes: 19 additions & 6 deletions src/GuzzleBundleOAuth2Plugin.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,24 @@ public function loadForClient(array $config, ContainerBuilder $container, string

//Define middleware
$oAuth2MiddlewareDefinitionName = sprintf('guzzle_bundle_oauth2_plugin.middleware.%s', $clientName);
$oAuth2MiddlewareDefinition = new Definition('%guzzle_bundle_oauth2_plugin.middleware.class%');
$oAuth2MiddlewareDefinition->setArguments([
new Reference($oauthClientDefinitionName),
new Reference($passwordCredentialsDefinitionName),
new Reference($refreshTokenDefinitionName)
]);
if ($config['persistent']) {
$oAuth2MiddlewareDefinition = new Definition('%guzzle_bundle_oauth2_plugin.persistent_middleware.class%');
$oAuth2MiddlewareDefinition->setArguments([
new Reference($oauthClientDefinitionName),
new Reference($passwordCredentialsDefinitionName),
new Reference($refreshTokenDefinitionName),
new Reference('session'),
$clientName
]);
} else {
$oAuth2MiddlewareDefinition = new Definition('%guzzle_bundle_oauth2_plugin.middleware.class%');
$oAuth2MiddlewareDefinition->setArguments([
new Reference($oauthClientDefinitionName),
new Reference($passwordCredentialsDefinitionName),
new Reference($refreshTokenDefinitionName)
]);
}

$oAuth2MiddlewareDefinition->setPublic(true);
$container->setDefinition($oAuth2MiddlewareDefinitionName, $oAuth2MiddlewareDefinition);

Expand Down Expand Up @@ -166,6 +178,7 @@ public function addConfiguration(ArrayNodeDefinition $pluginNode)
->thenInvalid(sprintf('Use instance of %s in grant_type', GrantTypeInterface::class))
->end()
->end()
->booleanNode('persistent')->defaultFalse()->end()
->end();
}

Expand Down
94 changes: 94 additions & 0 deletions src/Middleware/PersistentOAuthMiddleware.php
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']
));
}
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@

<parameters>
<parameter key="guzzle_bundle_oauth2_plugin.middleware.class">Sainsburys\Guzzle\Oauth2\Middleware\OAuthMiddleware</parameter>
<parameter key="guzzle_bundle_oauth2_plugin.persistent_middleware.class">Gregurco\Bundle\GuzzleBundleOAuth2Plugin\Middleware\PersistentOAuthMiddleware</parameter>
</parameters>
</container>
3 changes: 3 additions & 0 deletions tests/GuzzleBundleOAuth2PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function testAddConfiguration()
'private_key' => null,
'auth_location' => 'headers',
'grant_type' => ClientCredentials::class,
'persistent' => false,
],
$node->getDefaultValue()
);
Expand Down Expand Up @@ -103,6 +104,7 @@ public function testLoadForClient()
'private_key' => null,
'auth_location' => 'headers',
'grant_type' => ClientCredentials::class,
'persistent' => false,
],
$container, 'api_payment', $handler
);
Expand Down Expand Up @@ -133,6 +135,7 @@ public function testLoadForClientWithPrivateKey()
'private_key' => '/path/to/private.key',
'auth_location' => 'headers',
'grant_type' => JwtBearer::class,
'persistent' => false,
],
$container, 'api_payment', $handler
);
Expand Down

0 comments on commit 0eb6a7a

Please sign in to comment.