Skip to content

Commit

Permalink
symfony 5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Thierrygen committed Nov 8, 2024
1 parent dc8ff34 commit 043183d
Show file tree
Hide file tree
Showing 28 changed files with 489 additions and 223 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
phpstan.neon
phpunit.xml
Tests/autoload.php
var/
vendor/
Propel/om/
Propel/map/
composer.lock
.php_cs.cache
.phpunit.result.cache

.idea/
26 changes: 12 additions & 14 deletions Command/CleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,18 @@ class CleanCommand extends Command
{
protected static $defaultName = 'fos:oauth-server:clean';

private $accessTokenManager;
private $refreshTokenManager;
private $authCodeManager;

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

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

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

Expand All @@ -64,9 +56,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
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)));
$output->writeln(
sprintf(
'Removed <info>%d</info> items from <comment>%s</comment> storage.',
$result,
get_class($service)
)
);
}

return 0;
return Command::SUCCESS;
}
}
13 changes: 5 additions & 8 deletions Command/CreateClientCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@ class CreateClientCommand extends Command
{
protected static $defaultName = 'fos:oauth-server:create-client';

private $clientManager;

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

$this->clientManager = $clientManager;
}

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

Expand Down Expand Up @@ -92,6 +89,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$io->table($headers, $rows);

return 0;
return Command::SUCCESS;
}
}
98 changes: 19 additions & 79 deletions Controller/AuthorizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use FOS\OAuthServerBundle\Form\Handler\AuthorizeFormHandler;
use FOS\OAuthServerBundle\Model\ClientInterface;
use FOS\OAuthServerBundle\Model\ClientManagerInterface;
use RuntimeException;
use OAuth2\OAuth2;
use OAuth2\OAuth2ServerException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Expand All @@ -31,7 +32,7 @@
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\UserInterface;
use Twig\Environment as TwigEnvironment;
use Twig\Environment;

/**
* Controller handling basic authorization.
Expand All @@ -40,61 +41,6 @@
*/
class AuthorizeController
{
/**
* @var ClientInterface
*/
private $client;

/**
* @var SessionInterface
*/
private $session;

/**
* @var Form
*/
private $authorizeForm;

/**
* @var AuthorizeFormHandler
*/
private $authorizeFormHandler;

/**
* @var OAuth2
*/
private $oAuth2Server;

/**
* @var RequestStack
*/
private $requestStack;

/**
* @var TokenStorageInterface
*/
private $tokenStorage;

/**
* @var TwigEnvironment
*/
private $twig;

/**
* @var UrlGeneratorInterface
*/
private $router;

/**
* @var ClientManagerInterface
*/
private $clientManager;

/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;

/**
* This controller had been made as a service due to support symfony 4 where all* services are private by default.
* Thus, this is considered a bad practice to fetch services directly from container.
Expand All @@ -104,16 +50,16 @@ class AuthorizeController
* @param SessionInterface $session
*/
public function __construct(
RequestStack $requestStack,
Form $authorizeForm,
AuthorizeFormHandler $authorizeFormHandler,
OAuth2 $oAuth2Server,
TokenStorageInterface $tokenStorage,
UrlGeneratorInterface $router,
ClientManagerInterface $clientManager,
EventDispatcherInterface $eventDispatcher,
TwigEnvironment $twig,
SessionInterface $session = null
private RequestStack $requestStack,
private Form $authorizeForm,
private AuthorizeFormHandler $authorizeFormHandler,
private OAuth2 $oAuth2Server,
private TokenStorageInterface $tokenStorage,
private UrlGeneratorInterface $router,
private ClientManagerInterface $clientManager,
private EventDispatcherInterface $eventDispatcher,
private TwigEnvironment $twig,
private ?SessionInterface $session = null
) {
$this->requestStack = $requestStack;
$this->session = $session;
Expand All @@ -130,7 +76,7 @@ public function __construct(
/**
* Authorize.
*/
public function authorizeAction(Request $request)
public function authorizeAction(Request $request): Response
{
$user = $this->tokenStorage->getToken()->getUser();

Expand Down Expand Up @@ -165,10 +111,7 @@ public function authorizeAction(Request $request)
]);
}

/**
* @return Response
*/
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request)
protected function processSuccess(UserInterface $user, AuthorizeFormHandler $formHandler, Request $request): Response
{
if ($this->session && true === $this->session->get('_fos_oauth_server.ensure_logout')) {
$this->tokenStorage->setToken(null);
Expand All @@ -194,17 +137,14 @@ protected function processSuccess(UserInterface $user, AuthorizeFormHandler $for
/**
* Generate the redirection url when the authorize is completed.
*
* @return string
*/
protected function getRedirectionUrl(UserInterface $user)
protected function getRedirectionUrl(UserInterface $user): string
{
return $this->router->generate('fos_oauth_server_profile_show');
}

/**
* @return ClientInterface
*/
protected function getClient()

protected function getClient(): ClientInterface
{
if (null !== $this->client) {
return $this->client;
Expand Down Expand Up @@ -238,11 +178,11 @@ protected function renderAuthorize(array $context): Response
/**
* @return Request|null
*/
private function getCurrentRequest()
private function getCurrentRequest(): ?Request
{
$request = $this->requestStack->getCurrentRequest();
if (null === $request) {
throw new \RuntimeException('No current request.');
throw new RuntimeException('No current request.');
}

return $request;
Expand Down
12 changes: 2 additions & 10 deletions Controller/TokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,12 @@

class TokenController
{
/**
* @var OAuth2
*/
protected $server;

public function __construct(OAuth2 $server)
public function __construct(Private OAuth2 $server)
{
$this->server = $server;
}

/**
* @return Response
*/
public function tokenAction(Request $request)
public function tokenAction(Request $request): Response
{
try {
return $this->server->grantAccessToken($request);
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Configuration implements ConfigurationInterface
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('fos_oauth_server');
$rootNode = $treeBuilder->getRootNode();
Expand Down
20 changes: 19 additions & 1 deletion DependencyInjection/Security/Factory/OAuthFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace FOS\OAuthServerBundle\DependencyInjection\Security\Factory;

use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AuthenticatorFactoryInterface;
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ChildDefinition;
Expand All @@ -24,8 +25,25 @@
*
* @author Arnaud Le Blanc <[email protected]>
*/
class OAuthFactory implements SecurityFactoryInterface
class OAuthFactory implements AuthenticatorFactoryInterface, SecurityFactoryInterface
{

/**
* {@inheritdoc}
*/
public function createAuthenticator(ContainerBuilder $container, string $id, array $config, string $userProviderId)
{
$providerId = 'fos_oauth_server.security.authentication.authenticator.'.$id;
$container
->setDefinition($providerId, new ChildDefinition('fos_oauth_server.security.authentication.authenticator'))
->replaceArgument(0, new Reference('fos_oauth_server.server'))
->replaceArgument(1, new Reference('security.user_checker.'.$id))
->replaceArgument(2, new Reference($userProviderId))
;

return $providerId;
}

/**
* {@inheritdoc}
*/
Expand Down
6 changes: 3 additions & 3 deletions Resources/config/doctrine/AccessToken.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<mapped-superclass name="FOS\OAuthServerBundle\Document\AccessToken">
<field name="token" fieldName="token" type="string" unique="true" />
<field name="expiresAt" fieldName="expiresAt" type="int" nullable="true" />
<field name="scope" fieldName="scope" type="string" nullable="true" />
<field name="token" type="string" unique="true" />
<field name="expiresAt" type="int" nullable="true" />
<field name="scope" type="string" nullable="true" />
</mapped-superclass>
</doctrine-mongo-mapping>
8 changes: 4 additions & 4 deletions Resources/config/doctrine/AuthCode.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<mapped-superclass name="FOS\OAuthServerBundle\Document\AuthCode">
<field name="token" fieldName="token" type="string" unique="true" />
<field name="redirectUri" fieldName="redirectUri" type="string" />
<field name="expiresAt" fieldName="expiresAt" type="int" nullable="true" />
<field name="scope" fieldName="scope" type="string" nullable="true" />
<field name="token" type="string" unique="true" />
<field name="redirectUri" type="string" />
<field name="expiresAt" type="int" nullable="true" />
<field name="scope" type="string" nullable="true" />
</mapped-superclass>
</doctrine-mongo-mapping>
8 changes: 4 additions & 4 deletions Resources/config/doctrine/Client.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<mapped-superclass name="FOS\OAuthServerBundle\Document\Client">
<field name="randomId" fieldName="randomId" type="string" />
<field name="redirectUris" fieldName="redirectUris" type="collection" />
<field name="secret" fieldName="secret" type="string" />
<field name="allowedGrantTypes" fieldName="allowedGrantTypes" type="collection" />
<field name="randomId" type="string" />
<field name="redirectUris" type="collection" />
<field name="secret" type="string" />
<field name="allowedGrantTypes" type="collection" />
</mapped-superclass>
</doctrine-mongo-mapping>
6 changes: 3 additions & 3 deletions Resources/config/doctrine/RefreshToken.mongodb.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd">

<mapped-superclass name="FOS\OAuthServerBundle\Document\RefreshToken">
<field name="token" fieldName="token" type="string" unique="true" />
<field name="expiresAt" fieldName="expiresAt" type="int" nullable="true" />
<field name="scope" fieldName="scope" type="string" nullable="true" />
<field name="token" type="string" unique="true" />
<field name="expiresAt" type="int" nullable="true" />
<field name="scope" type="string" nullable="true" />
</mapped-superclass>
</doctrine-mongo-mapping>
7 changes: 7 additions & 0 deletions Resources/config/security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="fos_oauth_server.security.authentication.authenticator.class">FOS\OAuthServerBundle\Security\Authentication\Authenticator\OAuthAuthenticator</parameter>
<parameter key="fos_oauth_server.security.authentication.provider.class">FOS\OAuthServerBundle\Security\Authentication\Provider\OAuthProvider</parameter>
<parameter key="fos_oauth_server.security.authentication.listener.class">FOS\OAuthServerBundle\Security\Firewall\OAuthListener</parameter>
<parameter key="fos_oauth_server.security.entry_point.class">FOS\OAuthServerBundle\Security\EntryPoint\OAuthEntryPoint</parameter>
</parameters>

<services>
<service id="fos_oauth_server.security.authentication.authenticator" class="%fos_oauth_server.security.authentication.authenticator.class%" public="false">
<argument type="service" id="fos_oauth_server.server" />
<argument type="service" id="security.user_checker" />
<argument /> <!-- user provider -->
</service>

<service id="fos_oauth_server.security.authentication.provider" class="%fos_oauth_server.security.authentication.provider.class%" public="false">
<argument /> <!-- user provider -->
<argument type="service" id="fos_oauth_server.server" />
Expand Down
Loading

0 comments on commit 043183d

Please sign in to comment.