Skip to content

Commit e12b077

Browse files
authored
Merge pull request #576 from dkarlovi/merge-1.6
Merge 1.6
2 parents fe3e2b5 + feb81da commit e12b077

27 files changed

+514
-152
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ matrix:
1313
fast_finish: true
1414
include:
1515
- php: 7.1
16-
env: SYMFONY_VERSION=3.0.*
16+
env: SYMFONY_VERSION=3.4.*
1717
- php: 7.2
1818
env: SYMFONY_VERSION=4.0.*
1919
- php: 7.2

Command/CleanCommand.php

+23-15
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,35 @@
1515

1616
use FOS\OAuthServerBundle\Model\AuthCodeManagerInterface;
1717
use FOS\OAuthServerBundle\Model\TokenManagerInterface;
18-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
18+
use Symfony\Component\Console\Command\Command;
1919
use Symfony\Component\Console\Input\InputInterface;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121

22-
class CleanCommand extends ContainerAwareCommand
22+
class CleanCommand extends Command
2323
{
24+
private $accessTokenManager;
25+
private $refreshTokenManager;
26+
private $authCodeManager;
27+
28+
public function __construct(
29+
TokenManagerInterface $accessTokenManager,
30+
TokenManagerInterface $refreshTokenManager,
31+
AuthCodeManagerInterface $authCodeManager)
32+
{
33+
parent::__construct();
34+
35+
$this->accessTokenManager = $accessTokenManager;
36+
$this->refreshTokenManager = $refreshTokenManager;
37+
$this->authCodeManager = $authCodeManager;
38+
}
39+
2440
/**
2541
* {@inheritdoc}
2642
*/
2743
protected function configure()
2844
{
45+
parent::configure();
46+
2947
$this
3048
->setName('fos:oauth-server:clean')
3149
->setDescription('Clean expired tokens')
@@ -43,19 +61,9 @@ protected function configure()
4361
*/
4462
protected function execute(InputInterface $input, OutputInterface $output)
4563
{
46-
$services = [
47-
'fos_oauth_server.access_token_manager' => 'Access token',
48-
'fos_oauth_server.refresh_token_manager' => 'Refresh token',
49-
'fos_oauth_server.auth_code_manager' => 'Auth code',
50-
];
51-
52-
foreach ($services as $service => $name) {
53-
/** @var TokenManagerInterface $instance */
54-
$instance = $this->getContainer()->get($service);
55-
if ($instance instanceof TokenManagerInterface || $instance instanceof AuthCodeManagerInterface) {
56-
$result = $instance->deleteExpired();
57-
$output->writeln(sprintf('Removed <info>%d</info> items from <comment>%s</comment> storage.', $result, $name));
58-
}
64+
foreach ([$this->accessTokenManager, $this->refreshTokenManager, $this->authCodeManager] as $service) {
65+
$result = $service->deleteExpired();
66+
$output->writeln(sprintf('Removed <info>%d</info> items from <comment>%s</comment> storage.', $result, get_class($service)));
5967
}
6068
}
6169
}

Command/CreateClientCommand.php

+15-8
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,30 @@
1414
namespace FOS\OAuthServerBundle\Command;
1515

1616
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
17-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
17+
use Symfony\Component\Console\Command\Command;
1818
use Symfony\Component\Console\Input\InputInterface;
1919
use Symfony\Component\Console\Input\InputOption;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Style\SymfonyStyle;
2222

23-
class CreateClientCommand extends ContainerAwareCommand
23+
class CreateClientCommand extends Command
2424
{
25+
private $clientManager;
26+
27+
public function __construct(ClientManagerInterface $clientManager)
28+
{
29+
parent::__construct();
30+
31+
$this->clientManager = $clientManager;
32+
}
33+
2534
/**
2635
* {@inheritdoc}
2736
*/
2837
protected function configure()
2938
{
39+
parent::configure();
40+
3041
$this
3142
->setName('fos:oauth-server:create-client')
3243
->setDescription('Creates a new client')
@@ -63,18 +74,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
6374

6475
$io->title('Client Credentials');
6576

66-
// Get the client manager
67-
/** @var ClientManagerInterface $clientManager */
68-
$clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');
69-
7077
// Create a new client
71-
$client = $clientManager->createClient();
78+
$client = $this->clientManager->createClient();
7279

7380
$client->setRedirectUris($input->getOption('redirect-uri'));
7481
$client->setAllowedGrantTypes($input->getOption('grant-type'));
7582

7683
// Save the client
77-
$clientManager->updateClient($client);
84+
$this->clientManager->updateClient($client);
7885

7986
// Give the credentials back to the user
8087
$headers = ['Client ID', 'Client Secret'];

Controller/AuthorizeController.php

+7-23
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
use OAuth2\OAuth2;
2121
use OAuth2\OAuth2ServerException;
2222
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
23-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
24-
use Symfony\Component\DependencyInjection\ContainerInterface;
2523
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
2624
use Symfony\Component\Form\Form;
2725
use Symfony\Component\HttpFoundation\Request;
@@ -39,12 +37,8 @@
3937
*
4038
* @author Chris Jones <leeked@gmail.com>
4139
*/
42-
class AuthorizeController implements ContainerAwareInterface
40+
class AuthorizeController
4341
{
44-
/**
45-
* @var ContainerInterface
46-
*/
47-
protected $container;
4842
/**
4943
* @var ClientInterface
5044
*/
@@ -107,12 +101,11 @@ class AuthorizeController implements ContainerAwareInterface
107101

108102
/**
109103
* This controller had been made as a service due to support symfony 4 where all* services are private by default.
110-
* Thus, there is considered a bad practice to fetch services directly from container.
104+
* Thus, this is considered a bad practice to fetch services directly from container.
111105
*
112-
* @todo This controller could be refactored to do not rely on so many dependencies
106+
* @todo This controller could be refactored to not rely on so many dependencies
113107
*
114108
* @param RequestStack $requestStack
115-
* @param SessionInterface $session
116109
* @param Form $authorizeForm
117110
* @param AuthorizeFormHandler $authorizeFormHandler
118111
* @param OAuth2 $oAuth2Server
@@ -121,11 +114,11 @@ class AuthorizeController implements ContainerAwareInterface
121114
* @param UrlGeneratorInterface $router
122115
* @param ClientManagerInterface $clientManager
123116
* @param EventDispatcherInterface $eventDispatcher
117+
* @param SessionInterface $session
124118
* @param string $templateEngineType
125119
*/
126120
public function __construct(
127121
RequestStack $requestStack,
128-
SessionInterface $session,
129122
Form $authorizeForm,
130123
AuthorizeFormHandler $authorizeFormHandler,
131124
OAuth2 $oAuth2Server,
@@ -134,6 +127,7 @@ public function __construct(
134127
UrlGeneratorInterface $router,
135128
ClientManagerInterface $clientManager,
136129
EventDispatcherInterface $eventDispatcher,
130+
SessionInterface $session = null,
137131
$templateEngineType = 'twig'
138132
) {
139133
$this->requestStack = $requestStack;
@@ -149,16 +143,6 @@ public function __construct(
149143
$this->eventDispatcher = $eventDispatcher;
150144
}
151145

152-
/**
153-
* Sets the container.
154-
*
155-
* @param ContainerInterface|null $container A ContainerInterface instance or null
156-
*/
157-
public function setContainer(ContainerInterface $container = null)
158-
{
159-
$this->container = $container;
160-
}
161-
162146
/**
163147
* Authorize.
164148
*/
@@ -170,7 +154,7 @@ public function authorizeAction(Request $request)
170154
throw new AccessDeniedException('This user does not have access to this section.');
171155
}
172156

173-
if (true === $this->session->get('_fos_oauth_server.ensure_logout')) {
157+
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
174158
$this->session->invalidate(600);
175159
$this->session->set('_fos_oauth_server.ensure_logout', true);
176160
}
@@ -211,7 +195,7 @@ public function authorizeAction(Request $request)
211195
*/
212196
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request)
213197
{
214-
if (true === $this->session->get('_fos_oauth_server.ensure_logout')) {
198+
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
215199
$this->tokenStorage->setToken(null);
216200
$this->session->invalidate();
217201
}

DependencyInjection/Configuration.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,34 @@ public function getConfigTreeBuilder()
3434
/** @var ArrayNodeDefinition $rootNode */
3535
$rootNode = $treeBuilder->root('fos_oauth_server');
3636

37-
$supportedDrivers = ['orm', 'mongodb', 'propel'];
37+
$supportedDrivers = ['orm', 'mongodb', 'propel', 'custom'];
3838

3939
$rootNode
40+
->validate()
41+
->always(function ($v) {
42+
if ('custom' !== $v['db_driver']) {
43+
return $v;
44+
}
45+
46+
if (empty($v['service']['client_manager']) || $v['service']['client_manager'] === 'fos_oauth_server.client_manager.default') {
47+
throw new \InvalidArgumentException('The service client_manager must be set explicitly for custom db_driver.');
48+
}
49+
50+
if (empty($v['service']['access_token_manager']) || $v['service']['access_token_manager'] === 'fos_oauth_server.access_token_manager.default') {
51+
throw new \InvalidArgumentException('The service access_token_manager must be set explicitly for custom db_driver.');
52+
}
53+
54+
if (empty($v['service']['refresh_token_manager']) || $v['service']['refresh_token_manager'] === 'fos_oauth_server.refresh_token_manager.default') {
55+
throw new \InvalidArgumentException('The service refresh_token_manager must be set explicitly for custom db_driver.');
56+
}
57+
58+
if (empty($v['service']['auth_code_manager']) || $v['service']['auth_code_manager'] === 'fos_oauth_server.auth_code_manager.default') {
59+
throw new \InvalidArgumentException('The service auth_code_manager must be set explicitly for custom db_driver.');
60+
}
61+
62+
return $v;
63+
})
64+
->end()
4065
->children()
4166
->scalarNode('db_driver')
4267
->validate()

DependencyInjection/FOSOAuthServerExtension.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ public function load(array $configs, ContainerBuilder $container)
3636
$config = $processor->processConfiguration($configuration, $configs);
3737

3838
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
39-
$loader->load(sprintf('%s.xml', $config['db_driver']));
39+
40+
if ('custom' !== $config['db_driver']) {
41+
$loader->load(sprintf('%s.xml', $config['db_driver']));
42+
}
4043

4144
foreach (['oauth', 'security'] as $basename) {
4245
$loader->load(sprintf('%s.xml', $basename));
@@ -92,12 +95,12 @@ public function load(array $configs, ContainerBuilder $container)
9295

9396
if (!empty($config['authorize'])) {
9497
$this->loadAuthorize($config['authorize'], $container, $loader);
95-
}
9698

97-
// Authorize form factory definition
98-
// TODO: Go back to xml configuration when bumping the requirement to Symfony >=2.6
99-
$authorizeFormDefinition = $container->getDefinition('fos_oauth_server.authorize.form');
100-
$authorizeFormDefinition->setFactory([new Reference('form.factory'), 'createNamed']);
99+
// Authorize form factory definition
100+
// TODO: Go back to xml configuration when bumping the requirement to Symfony >=2.6
101+
$authorizeFormDefinition = $container->getDefinition('fos_oauth_server.authorize.form');
102+
$authorizeFormDefinition->setFactory([new Reference('form.factory'), 'createNamed']);
103+
}
101104
}
102105

103106
/**

Resources/config/authorize.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
<service id="fos_oauth_server.controller.authorize" class="FOS\OAuthServerBundle\Controller\AuthorizeController" public="true">
2727
<argument type="service" id="request_stack" />
28-
<argument type="service" id="session" />
2928
<argument type="service" id="fos_oauth_server.authorize.form" />
3029
<argument type="service" id="fos_oauth_server.authorize.form.handler" />
3130
<argument type="service" id="fos_oauth_server.server" />
@@ -34,6 +33,7 @@
3433
<argument type="service" id="router" />
3534
<argument type="service" id="fos_oauth_server.client_manager" />
3635
<argument type="service" id="event_dispatcher" />
36+
<argument type="service" id="session" on-invalid="null" />
3737
<argument>%fos_oauth_server.template.engine%</argument>
3838
</service>
3939
</services>

Resources/config/couchdb.xml

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
<service id="fos_user.document_manager" factory-service="doctrine_couchdb" factory-method="getObjectManager" class="Doctrine\ODM\CouchDB\DocumentManager" public="false">
2929
<argument>%fos_oauth_server.model_manager_name%</argument>
3030
</service>
31+
32+
<service id="FOS\OAuthServerBundle\Model\ClientManagerInterface" alias="fos_oauth_server.client_manager.default" />
33+
<service id="FOS\OAuthServerBundle\Model\AccessTokenManagerInterface" alias="fos_oauth_server.access_token_manager.default" />
34+
<service id="FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface" alias="fos_oauth_server.refresh_token_manager.default" />
35+
<service id="FOS\OAuthServerBundle\Model\AuthCodeManagerInterface" alias="fos_oauth_server.auth_code_manager.default" />
3136
</services>
3237

3338
</container>

Resources/config/mongodb.xml

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
<argument type="service" id="fos_oauth_server.document_manager" />
2525
<argument>%fos_oauth_server.model.refresh_token.class%</argument>
2626
</service>
27+
28+
<service id="FOS\OAuthServerBundle\Model\ClientManagerInterface" alias="fos_oauth_server.client_manager.default" />
29+
<service id="FOS\OAuthServerBundle\Model\AccessTokenManagerInterface" alias="fos_oauth_server.access_token_manager.default" />
30+
<service id="FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface" alias="fos_oauth_server.refresh_token_manager.default" />
31+
<service id="FOS\OAuthServerBundle\Model\AuthCodeManagerInterface" alias="fos_oauth_server.auth_code_manager.default" />
2732
</services>
2833

2934
</container>

Resources/config/oauth.xml

+14-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,22 @@
2323
<argument>%fos_oauth_server.server.options%</argument>
2424
</service>
2525

26-
<service id="FOS\OAuthServerBundle\Controller\TokenController">
26+
<service id="FOS\OAuthServerBundle\Controller\TokenController" class="FOS\OAuthServerBundle\Controller\TokenController">
2727
<argument type="service" id="fos_oauth_server.server" />
2828
</service>
29+
2930
<service id="fos_oauth_server.controller.token" alias="FOS\OAuthServerBundle\Controller\TokenController" public="true" />
31+
32+
<service id="fos_oauth_server.clean_command" class="FOS\OAuthServerBundle\Command\CleanCommand">
33+
<argument type="service" id="fos_oauth_server.access_token_manager" />
34+
<argument type="service" id="fos_oauth_server.refresh_token_manager" />
35+
<argument type="service" id="fos_oauth_server.auth_code_manager" />
36+
<tag name="console.command" />
37+
</service>
38+
39+
<service id="fos_oauth_server.create_client_command" class="FOS\OAuthServerBundle\Command\CreateClientCommand">
40+
<argument type="service" id="fos_oauth_server.client_manager" />
41+
<tag name="console.command" />
42+
</service>
3043
</services>
3144
</container>

Resources/config/orm.xml

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
<service id="fos_oauth_server.entity_manager" class="Doctrine\ORM\EntityManager" public="false">
2929
<argument>%fos_oauth_server.model_manager_name%</argument>
3030
</service>
31+
32+
<service id="FOS\OAuthServerBundle\Model\ClientManagerInterface" alias="fos_oauth_server.client_manager.default" />
33+
<service id="FOS\OAuthServerBundle\Model\AccessTokenManagerInterface" alias="fos_oauth_server.access_token_manager.default" />
34+
<service id="FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface" alias="fos_oauth_server.refresh_token_manager.default" />
35+
<service id="FOS\OAuthServerBundle\Model\AuthCodeManagerInterface" alias="fos_oauth_server.auth_code_manager.default" />
3136
</services>
3237

3338
</container>

Resources/config/propel.xml

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
<service id="fos_oauth_server.auth_code_manager.default" class="FOS\OAuthServerBundle\Propel\AuthCodeManager">
2020
<argument>%fos_oauth_server.model.auth_code.class%</argument>
2121
</service>
22+
23+
<service id="FOS\OAuthServerBundle\Model\ClientManagerInterface" alias="fos_oauth_server.client_manager.default" />
24+
<service id="FOS\OAuthServerBundle\Model\AccessTokenManagerInterface" alias="fos_oauth_server.access_token_manager.default" />
25+
<service id="FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface" alias="fos_oauth_server.refresh_token_manager.default" />
26+
<service id="FOS\OAuthServerBundle\Model\AuthCodeManagerInterface" alias="fos_oauth_server.auth_code_manager.default" />
2227
</services>
2328

2429
</container>

Resources/doc/custom_db_driver.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Custom db driver.
2+
=================
3+
4+
The bundle provides drivers for Doctrine ORM, Doctrine MongoDB, and Propel libraries.
5+
Though sometimes you might want to use the bundle with a custom or in-house written storage.
6+
For that, the bundle has support for custom storage.
7+
Once set, setting manager options in fos_oauth_server.service section becomes mandatory.
8+
9+
Here's an example of custom configuration:
10+
11+
```yaml
12+
# config/packages/fos_oauth_server.yaml
13+
14+
fos_oauth_server:
15+
db_driver: custom
16+
service:
17+
user_provider: 'user_provider_manager_service_id'
18+
client_manager: 'client_provider_manager_service_id'
19+
access_token_manager: 'access_token_manager_service_id'
20+
refresh_token_manager: 'refresh_token_manager_service_id'
21+
auth_code_manager: 'auth_code_manager_service_id'
22+
23+
```
24+
25+
[Back to index](index.md)
26+

0 commit comments

Comments
 (0)