Skip to content

Commit

Permalink
Merge pull request #14 from gregurco/add_options
Browse files Browse the repository at this point in the history
Add options (audience and retry_limit)
  • Loading branch information
gregurco authored Jul 2, 2018
2 parents 691a611 + 157ba2c commit 615f6e9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,10 @@ eight_points_guzzle:
| resource | The App ID URI of the web API (secured resource) | no | https://service.contoso.com/ |
| 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 |
| audience | | no | |
| 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 | |
| retry_limit | How many times request will be repeated on failure. <br/> Default: 5 | no | |

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

Expand Down
9 changes: 8 additions & 1 deletion src/GuzzleBundleOAuth2Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function loadForClient(array $config, ContainerBuilder $container, string
GrantTypeBase::CONFIG_RESOURCE => $config['resource'],
JwtBearer::CONFIG_PRIVATE_KEY => null,
'scope' => $config['scope'],
'audience' => $config['audience'],
];

if ($config['private_key']) {
Expand Down Expand Up @@ -110,7 +111,11 @@ public function loadForClient(array $config, ContainerBuilder $container, string
$container->setDefinition($oAuth2MiddlewareDefinitionName, $oAuth2MiddlewareDefinition);

$onBeforeExpression = new Expression(sprintf('service("%s").onBefore()', $oAuth2MiddlewareDefinitionName));
$onFailureExpression = new Expression(sprintf('service("%s").onFailure(5)', $oAuth2MiddlewareDefinitionName));
$onFailureExpression = new Expression(sprintf(
'service("%s").onFailure(%d)',
$oAuth2MiddlewareDefinitionName,
$config['retry_limit']
));

$handler->addMethodCall('push', [$onBeforeExpression]);
$handler->addMethodCall('push', [$onFailureExpression]);
Expand Down Expand Up @@ -160,6 +165,7 @@ public function addConfiguration(ArrayNodeDefinition $pluginNode)
->scalarNode('client_secret')->defaultNull()->end()
->scalarNode('token_url')->defaultNull()->end()
->scalarNode('scope')->defaultNull()->end()
->scalarNode('audience')->defaultNull()->end()
->scalarNode('resource')->defaultNull()->end()
->scalarNode('private_key')->defaultNull()->end()
->scalarNode('auth_location')
Expand All @@ -179,6 +185,7 @@ public function addConfiguration(ArrayNodeDefinition $pluginNode)
->end()
->end()
->booleanNode('persistent')->defaultFalse()->end()
->booleanNode('retry_limit')->defaultValue(5)->end()
->end();
}

Expand Down
6 changes: 6 additions & 0 deletions tests/GuzzleBundleOAuth2PluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ public function testAddConfiguration()
'client_secret' => null,
'token_url' => null,
'scope' => null,
'audience' => null,
'resource' => null,
'private_key' => null,
'auth_location' => 'headers',
'grant_type' => ClientCredentials::class,
'persistent' => false,
'retry_limit' => 5,
],
$node->getDefaultValue()
);
Expand Down Expand Up @@ -100,11 +102,13 @@ public function testLoadForClient()
'client_id' => 'test-client-id',
'client_secret' => '',
'scope' => 'administration',
'audience' => null,
'resource' => null,
'private_key' => null,
'auth_location' => 'headers',
'grant_type' => ClientCredentials::class,
'persistent' => false,
'retry_limit' => 5,
],
$container, 'api_payment', $handler
);
Expand All @@ -131,11 +135,13 @@ public function testLoadForClientWithPrivateKey()
'client_id' => 'test-client-id',
'client_secret' => '',
'scope' => 'administration',
'audience' => null,
'resource' => null,
'private_key' => '/path/to/private.key',
'auth_location' => 'headers',
'grant_type' => JwtBearer::class,
'persistent' => false,
'retry_limit' => 5,
],
$container, 'api_payment', $handler
);
Expand Down

0 comments on commit 615f6e9

Please sign in to comment.