Skip to content

Commit

Permalink
Merge pull request #576 from dkarlovi/merge-1.6
Browse files Browse the repository at this point in the history
Merge 1.6
  • Loading branch information
dkarlovi authored May 1, 2018
2 parents fe3e2b5 + feb81da commit e12b077
Show file tree
Hide file tree
Showing 27 changed files with 514 additions and 152 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ matrix:
fast_finish: true
include:
- php: 7.1
env: SYMFONY_VERSION=3.0.*
env: SYMFONY_VERSION=3.4.*
- php: 7.2
env: SYMFONY_VERSION=4.0.*
- php: 7.2
Expand Down
38 changes: 23 additions & 15 deletions Command/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,35 @@

use FOS\OAuthServerBundle\Model\AuthCodeManagerInterface;
use FOS\OAuthServerBundle\Model\TokenManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CleanCommand extends ContainerAwareCommand
class CleanCommand extends Command
{
private $accessTokenManager;
private $refreshTokenManager;
private $authCodeManager;

public function __construct(
TokenManagerInterface $accessTokenManager,
TokenManagerInterface $refreshTokenManager,
AuthCodeManagerInterface $authCodeManager)
{
parent::__construct();

$this->accessTokenManager = $accessTokenManager;
$this->refreshTokenManager = $refreshTokenManager;
$this->authCodeManager = $authCodeManager;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();

$this
->setName('fos:oauth-server:clean')
->setDescription('Clean expired tokens')
Expand All @@ -43,19 +61,9 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$services = [
'fos_oauth_server.access_token_manager' => 'Access token',
'fos_oauth_server.refresh_token_manager' => 'Refresh token',
'fos_oauth_server.auth_code_manager' => 'Auth code',
];

foreach ($services as $service => $name) {
/** @var TokenManagerInterface $instance */
$instance = $this->getContainer()->get($service);
if ($instance instanceof TokenManagerInterface || $instance instanceof AuthCodeManagerInterface) {
$result = $instance->deleteExpired();
$output->writeln(sprintf('Removed <info>%d</info> items from <comment>%s</comment> storage.', $result, $name));
}
foreach ([$this->accessTokenManager, $this->refreshTokenManager, $this->authCodeManager] as $service) {
$result = $service->deleteExpired();
$output->writeln(sprintf('Removed <info>%d</info> items from <comment>%s</comment> storage.', $result, get_class($service)));
}
}
}
23 changes: 15 additions & 8 deletions Command/CreateClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,30 @@
namespace FOS\OAuthServerBundle\Command;

use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

class CreateClientCommand extends ContainerAwareCommand
class CreateClientCommand extends Command
{
private $clientManager;

public function __construct(ClientManagerInterface $clientManager)
{
parent::__construct();

$this->clientManager = $clientManager;
}

/**
* {@inheritdoc}
*/
protected function configure()
{
parent::configure();

$this
->setName('fos:oauth-server:create-client')
->setDescription('Creates a new client')
Expand Down Expand Up @@ -63,18 +74,14 @@ protected function execute(InputInterface $input, OutputInterface $output)

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

// Get the client manager
/** @var ClientManagerInterface $clientManager */
$clientManager = $this->getContainer()->get('fos_oauth_server.client_manager.default');

// Create a new client
$client = $clientManager->createClient();
$client = $this->clientManager->createClient();

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

// Save the client
$clientManager->updateClient($client);
$this->clientManager->updateClient($client);

// Give the credentials back to the user
$headers = ['Client ID', 'Client Secret'];
Expand Down
30 changes: 7 additions & 23 deletions Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
use OAuth2\OAuth2;
use OAuth2\OAuth2ServerException;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -39,12 +37,8 @@
*
* @author Chris Jones <[email protected]>
*/
class AuthorizeController implements ContainerAwareInterface
class AuthorizeController
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var ClientInterface
*/
Expand Down Expand Up @@ -107,12 +101,11 @@ class AuthorizeController implements ContainerAwareInterface

/**
* This controller had been made as a service due to support symfony 4 where all* services are private by default.
* Thus, there is considered a bad practice to fetch services directly from container.
* Thus, this is considered a bad practice to fetch services directly from container.
*
* @todo This controller could be refactored to do not rely on so many dependencies
* @todo This controller could be refactored to not rely on so many dependencies
*
* @param RequestStack $requestStack
* @param SessionInterface $session
* @param Form $authorizeForm
* @param AuthorizeFormHandler $authorizeFormHandler
* @param OAuth2 $oAuth2Server
Expand All @@ -121,11 +114,11 @@ class AuthorizeController implements ContainerAwareInterface
* @param UrlGeneratorInterface $router
* @param ClientManagerInterface $clientManager
* @param EventDispatcherInterface $eventDispatcher
* @param SessionInterface $session
* @param string $templateEngineType
*/
public function __construct(
RequestStack $requestStack,
SessionInterface $session,
Form $authorizeForm,
AuthorizeFormHandler $authorizeFormHandler,
OAuth2 $oAuth2Server,
Expand All @@ -134,6 +127,7 @@ public function __construct(
UrlGeneratorInterface $router,
ClientManagerInterface $clientManager,
EventDispatcherInterface $eventDispatcher,
SessionInterface $session = null,
$templateEngineType = 'twig'
) {
$this->requestStack = $requestStack;
Expand All @@ -149,16 +143,6 @@ public function __construct(
$this->eventDispatcher = $eventDispatcher;
}

/**
* Sets the container.
*
* @param ContainerInterface|null $container A ContainerInterface instance or null
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}

/**
* Authorize.
*/
Expand All @@ -170,7 +154,7 @@ public function authorizeAction(Request $request)
throw new AccessDeniedException('This user does not have access to this section.');
}

if (true === $this->session->get('_fos_oauth_server.ensure_logout')) {
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
$this->session->invalidate(600);
$this->session->set('_fos_oauth_server.ensure_logout', true);
}
Expand Down Expand Up @@ -211,7 +195,7 @@ public function authorizeAction(Request $request)
*/
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request)
{
if (true === $this->session->get('_fos_oauth_server.ensure_logout')) {
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
$this->tokenStorage->setToken(null);
$this->session->invalidate();
}
Expand Down
27 changes: 26 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,34 @@ public function getConfigTreeBuilder()
/** @var ArrayNodeDefinition $rootNode */
$rootNode = $treeBuilder->root('fos_oauth_server');

$supportedDrivers = ['orm', 'mongodb', 'propel'];
$supportedDrivers = ['orm', 'mongodb', 'propel', 'custom'];

$rootNode
->validate()
->always(function ($v) {
if ('custom' !== $v['db_driver']) {
return $v;
}

if (empty($v['service']['client_manager']) || $v['service']['client_manager'] === 'fos_oauth_server.client_manager.default') {
throw new \InvalidArgumentException('The service client_manager must be set explicitly for custom db_driver.');
}

if (empty($v['service']['access_token_manager']) || $v['service']['access_token_manager'] === 'fos_oauth_server.access_token_manager.default') {
throw new \InvalidArgumentException('The service access_token_manager must be set explicitly for custom db_driver.');
}

if (empty($v['service']['refresh_token_manager']) || $v['service']['refresh_token_manager'] === 'fos_oauth_server.refresh_token_manager.default') {
throw new \InvalidArgumentException('The service refresh_token_manager must be set explicitly for custom db_driver.');
}

if (empty($v['service']['auth_code_manager']) || $v['service']['auth_code_manager'] === 'fos_oauth_server.auth_code_manager.default') {
throw new \InvalidArgumentException('The service auth_code_manager must be set explicitly for custom db_driver.');
}

return $v;
})
->end()
->children()
->scalarNode('db_driver')
->validate()
Expand Down
15 changes: 9 additions & 6 deletions DependencyInjection/FOSOAuthServerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public function load(array $configs, ContainerBuilder $container)
$config = $processor->processConfiguration($configuration, $configs);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load(sprintf('%s.xml', $config['db_driver']));

if ('custom' !== $config['db_driver']) {
$loader->load(sprintf('%s.xml', $config['db_driver']));
}

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

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

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

/**
Expand Down
2 changes: 1 addition & 1 deletion Resources/config/authorize.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

<service id="fos_oauth_server.controller.authorize" class="FOS\OAuthServerBundle\Controller\AuthorizeController" public="true">
<argument type="service" id="request_stack" />
<argument type="service" id="session" />
<argument type="service" id="fos_oauth_server.authorize.form" />
<argument type="service" id="fos_oauth_server.authorize.form.handler" />
<argument type="service" id="fos_oauth_server.server" />
Expand All @@ -34,6 +33,7 @@
<argument type="service" id="router" />
<argument type="service" id="fos_oauth_server.client_manager" />
<argument type="service" id="event_dispatcher" />
<argument type="service" id="session" on-invalid="null" />
<argument>%fos_oauth_server.template.engine%</argument>
</service>
</services>
Expand Down
5 changes: 5 additions & 0 deletions Resources/config/couchdb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<service id="fos_user.document_manager" factory-service="doctrine_couchdb" factory-method="getObjectManager" class="Doctrine\ODM\CouchDB\DocumentManager" public="false">
<argument>%fos_oauth_server.model_manager_name%</argument>
</service>

<service id="FOS\OAuthServerBundle\Model\ClientManagerInterface" alias="fos_oauth_server.client_manager.default" />
<service id="FOS\OAuthServerBundle\Model\AccessTokenManagerInterface" alias="fos_oauth_server.access_token_manager.default" />
<service id="FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface" alias="fos_oauth_server.refresh_token_manager.default" />
<service id="FOS\OAuthServerBundle\Model\AuthCodeManagerInterface" alias="fos_oauth_server.auth_code_manager.default" />
</services>

</container>
5 changes: 5 additions & 0 deletions Resources/config/mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
<argument type="service" id="fos_oauth_server.document_manager" />
<argument>%fos_oauth_server.model.refresh_token.class%</argument>
</service>

<service id="FOS\OAuthServerBundle\Model\ClientManagerInterface" alias="fos_oauth_server.client_manager.default" />
<service id="FOS\OAuthServerBundle\Model\AccessTokenManagerInterface" alias="fos_oauth_server.access_token_manager.default" />
<service id="FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface" alias="fos_oauth_server.refresh_token_manager.default" />
<service id="FOS\OAuthServerBundle\Model\AuthCodeManagerInterface" alias="fos_oauth_server.auth_code_manager.default" />
</services>

</container>
15 changes: 14 additions & 1 deletion Resources/config/oauth.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@
<argument>%fos_oauth_server.server.options%</argument>
</service>

<service id="FOS\OAuthServerBundle\Controller\TokenController">
<service id="FOS\OAuthServerBundle\Controller\TokenController" class="FOS\OAuthServerBundle\Controller\TokenController">
<argument type="service" id="fos_oauth_server.server" />
</service>

<service id="fos_oauth_server.controller.token" alias="FOS\OAuthServerBundle\Controller\TokenController" public="true" />

<service id="fos_oauth_server.clean_command" class="FOS\OAuthServerBundle\Command\CleanCommand">
<argument type="service" id="fos_oauth_server.access_token_manager" />
<argument type="service" id="fos_oauth_server.refresh_token_manager" />
<argument type="service" id="fos_oauth_server.auth_code_manager" />
<tag name="console.command" />
</service>

<service id="fos_oauth_server.create_client_command" class="FOS\OAuthServerBundle\Command\CreateClientCommand">
<argument type="service" id="fos_oauth_server.client_manager" />
<tag name="console.command" />
</service>
</services>
</container>
5 changes: 5 additions & 0 deletions Resources/config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<service id="fos_oauth_server.entity_manager" class="Doctrine\ORM\EntityManager" public="false">
<argument>%fos_oauth_server.model_manager_name%</argument>
</service>

<service id="FOS\OAuthServerBundle\Model\ClientManagerInterface" alias="fos_oauth_server.client_manager.default" />
<service id="FOS\OAuthServerBundle\Model\AccessTokenManagerInterface" alias="fos_oauth_server.access_token_manager.default" />
<service id="FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface" alias="fos_oauth_server.refresh_token_manager.default" />
<service id="FOS\OAuthServerBundle\Model\AuthCodeManagerInterface" alias="fos_oauth_server.auth_code_manager.default" />
</services>

</container>
5 changes: 5 additions & 0 deletions Resources/config/propel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
<service id="fos_oauth_server.auth_code_manager.default" class="FOS\OAuthServerBundle\Propel\AuthCodeManager">
<argument>%fos_oauth_server.model.auth_code.class%</argument>
</service>

<service id="FOS\OAuthServerBundle\Model\ClientManagerInterface" alias="fos_oauth_server.client_manager.default" />
<service id="FOS\OAuthServerBundle\Model\AccessTokenManagerInterface" alias="fos_oauth_server.access_token_manager.default" />
<service id="FOS\OAuthServerBundle\Model\RefreshTokenManagerInterface" alias="fos_oauth_server.refresh_token_manager.default" />
<service id="FOS\OAuthServerBundle\Model\AuthCodeManagerInterface" alias="fos_oauth_server.auth_code_manager.default" />
</services>

</container>
26 changes: 26 additions & 0 deletions Resources/doc/custom_db_driver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Custom db driver.
=================

The bundle provides drivers for Doctrine ORM, Doctrine MongoDB, and Propel libraries.
Though sometimes you might want to use the bundle with a custom or in-house written storage.
For that, the bundle has support for custom storage.
Once set, setting manager options in fos_oauth_server.service section becomes mandatory.

Here's an example of custom configuration:

```yaml
# config/packages/fos_oauth_server.yaml

fos_oauth_server:
db_driver: custom
service:
user_provider: 'user_provider_manager_service_id'
client_manager: 'client_provider_manager_service_id'
access_token_manager: 'access_token_manager_service_id'
refresh_token_manager: 'refresh_token_manager_service_id'
auth_code_manager: 'auth_code_manager_service_id'

```

[Back to index](index.md)

Loading

0 comments on commit e12b077

Please sign in to comment.