Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow array in supported_scopes option #552

Merged
merged 4 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ protected function getRedirectionUrl(UserInterface $user)
}

/**
* @return ClientInterface.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marked as error by php-cs-fixed ¯_(ツ)_/¯

* @return ClientInterface
*/
protected function getClient()
{
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function addServiceSection(ArrayNodeDefinition $node)
->arrayNode('options')
->useAttributeAsKey('key')
->treatNullLike([])
->prototype('scalar')->end()
->prototype('variable')->end()
->end()
->end()
->end()
Expand Down
18 changes: 17 additions & 1 deletion DependencyInjection/FOSOAuthServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace FOS\OAuthServerBundle\DependencyInjection;

use FOS\OAuthServerBundle\Util\LegacyFormHelper;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
Expand Down Expand Up @@ -51,7 +52,11 @@ public function load(array $configs, ContainerBuilder $container)
$container->setAlias('fos_oauth_server.user_provider', new Alias($config['service']['user_provider'], false));
}

$container->setParameter('fos_oauth_server.server.options', $config['service']['options']);
$options = $config['service']['options'];
if (is_array($options['supported_scopes'] ?? null)) {
$options['supported_scopes'] = $this->computeArraySupportedScopes($options['supported_scopes']);
}
$container->setParameter('fos_oauth_server.server.options', $options);

$this->remapParametersNamespaces($config, $container, [
'' => [
Expand Down Expand Up @@ -150,4 +155,15 @@ protected function loadAuthorize(array $config, ContainerBuilder $container, Xml
'form' => 'fos_oauth_server.authorize.form.%s',
]);
}

private function computeArraySupportedScopes(array $supportedScopes)
{
foreach ($supportedScopes as $scope) {
if (false !== mb_strpos($scope, ' ')) {
throw new InvalidConfigurationException('The array notation for supported_scopes should not contain spaces in array items. Either use full array notation or use the string notation for supported_scopes. See https://git.io/vx1X0 for more informations.');
}
}

return implode(' ', $supportedScopes);
}
}
14 changes: 14 additions & 0 deletions Resources/doc/dealing_with_scopes.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ That's why the `scope` column in the model layer is a string, not an array for i

To configure allowed scopes in your application, you have to edit your `app/config/config.yml` file:

``` yaml
# app/config/config.yml
fos_oauth_server:
service:
options:
supported_scopes:
- scope1
- scope2
- scope3
- scope4
```

If you have a short list of scopes, you can define them as a simple string:
``` yaml
# app/config/config.yml
fos_oauth_server:
Expand All @@ -21,6 +34,7 @@ fos_oauth_server:
supported_scopes: scope1 scope2
```


Now, clients will be able to pass a `scope` parameter when they request an access token.


Expand Down
113 changes: 113 additions & 0 deletions Tests/DependencyInjection/FOSOAuthServerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@

namespace FOS\OAuthServerBundle\Tests\DependencyInjection;

use FOS\OAuthServerBundle\DependencyInjection\FOSOAuthServerExtension;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\Routing\Loader\XmlFileLoader;

class FOSOAuthServerExtensionTest extends \PHPUnit\Framework\TestCase
{
private $container;

public function setUp()
{
$parameterBag = new ParameterBag();
$this->container = new ContainerBuilder($parameterBag);

parent::setUp();
}

public function testLoadAuthorizeRouting()
{
$locator = new FileLocator();
Expand All @@ -39,4 +53,103 @@ public function testLoadTokenRouting()
$this->assertSame('/oauth/v2/token', $tokenRoute->getPath());
$this->assertSame(['GET', 'POST'], $tokenRoute->getMethods());
}

public function testWithoutService()
{
$config = [
'db_driver' => 'orm',
'client_class' => 'dumb_class',
'access_token_class' => 'dumb_access_token_class',
'refresh_token_class' => 'dumb_refresh_token_class',
'auth_code_class' => 'dumb_auth_code_class',
];
$instance = new FOSOAuthServerExtension();
$instance->load([$config], $this->container);

$this->assertSame(
$this->container->getParameter('fos_oauth_server.server.options'),
[]
);
}

public function testStringSupportedScopes()
{
$scopes = 'scope1 scope2 scope3 scope4';

$config = [
'db_driver' => 'orm',
'client_class' => 'dumb_class',
'access_token_class' => 'dumb_access_token_class',
'refresh_token_class' => 'dumb_refresh_token_class',
'auth_code_class' => 'dumb_auth_code_class',
'service' => [
'options' => [
'supported_scopes' => $scopes,
],
],
];

$instance = new FOSOAuthServerExtension();
$instance->load([$config], $this->container);

$this->assertSame(
$this->container->getParameter('fos_oauth_server.server.options'),
[
'supported_scopes' => 'scope1 scope2 scope3 scope4',
]
);
}

public function testArraySupportedScopes()
{
$scopes = ['scope1', 'scope2', 'scope3', 'scope4'];

$config = [
'db_driver' => 'orm',
'client_class' => 'dumb_class',
'access_token_class' => 'dumb_access_token_class',
'refresh_token_class' => 'dumb_refresh_token_class',
'auth_code_class' => 'dumb_auth_code_class',
'service' => [
'options' => [
'supported_scopes' => $scopes,
'enforce_redirect' => true,
],
],
];
$instance = new FOSOAuthServerExtension();
$instance->load([$config], $this->container);

$this->assertSame(
$this->container->getParameter('fos_oauth_server.server.options'),
[
'supported_scopes' => 'scope1 scope2 scope3 scope4',
'enforce_redirect' => true,
]
);
}

public function testArraySupportedScopesWithSpace()
{
$scopes = ['scope1 scope2', 'scope3', 'scope4'];

$config = [
'db_driver' => 'orm',
'client_class' => 'dumb_class',
'access_token_class' => 'dumb_access_token_class',
'refresh_token_class' => 'dumb_refresh_token_class',
'auth_code_class' => 'dumb_auth_code_class',
'service' => [
'options' => [
'supported_scopes' => $scopes,
'enforce_redirect' => true,
],
],
];
$instance = new FOSOAuthServerExtension();

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessage('The array notation for supported_scopes should not contain spaces in array items. Either use full array notation or use the string notation for supported_scopes. See https://git.io/vx1X0 for more informations.');
$instance->load([$config], $this->container);
}
}