Skip to content

Commit

Permalink
bug #897 Fix unexpected deprecation about Guard (bis) (chalasr)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

Fix unexpected deprecation about Guard (bis)

Fixes #896

Commits
-------

9f27601 Fix unexpected deprecation about Guard (bis)
  • Loading branch information
chalasr committed Jul 28, 2021
2 parents 2433a6d + 9f27601 commit 18b026a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Compiler;

use Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;

/**
* @internal
*/
class DeprecateLegacyGuardAuthenticatorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasParameter('lexik_jwt_authentication.authenticator_manager_enabled') || !$container->getParameter('lexik_jwt_authentication.authenticator_manager_enabled')) {
return;
}

$deprecationArgs = ['The "%service_id%" service is deprecated and will be removed in 3.0, use the new "jwt" authenticator instead.'];
if (method_exists(BaseNode::class, 'getDeprecation')) {
$deprecationArgs = ['lexik/jwt-authentication-bundle', '2.7', 'The "%service_id%" service is deprecated and will be removed in 3.0, use the new "jwt" authenticator instead.'];
}

$container
->getDefinition('lexik_jwt_authentication.security.guard.jwt_token_authenticator')
->setDeprecated(...$deprecationArgs);
}
}

This file was deleted.

1 change: 1 addition & 0 deletions DependencyInjection/LexikJWTAuthenticationExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('response_interceptor.xml');
$loader->load('token_authenticator.xml');
$loader->load('token_extractor.xml');
$loader->load('guard_authenticator.xml');

if (isset($config['private_key_path'])) {
$config['secret_key'] = $config['private_key_path'];
Expand Down
10 changes: 3 additions & 7 deletions DependencyInjection/Security/Factory/JWTAuthenticatorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,10 @@ public function createAuthenticator(ContainerBuilder $container, string $firewal
->setDefinition($authenticatorId, new ChildDefinition('lexik_jwt_authentication.security.jwt_authenticator'))
->replaceArgument(3, new Reference($userProviderId));

$this->removeLegacyGuardServices($container);
// Compile-time parameter removed by RemoveLegacyAuthenticatorPass
// Stop setting it when guard support gets removed (aka when removing Symfony<5.3 support)
$container->setParameter('lexik_jwt_authentication.authenticator_manager_enabled', true);

return $authenticatorId;
}

private function removeLegacyGuardServices(ContainerBuilder $container)
{
$container->removeAlias('lexik_jwt_authentication.jwt_token_authenticator');
$container->removeDefinition('lexik_jwt_authentication.security.guard.jwt_token_authenticator');
}
}
4 changes: 0 additions & 4 deletions DependencyInjection/Security/Factory/JWTUserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ public function create(ContainerBuilder $container, $id, $config)
{
$container->setDefinition($id, new ChildDefinition('lexik_jwt_authentication.security.jwt_user_provider'))
->replaceArgument(0, $config['class']);

// Compile-time parameter removed by RemoveLegacyAuthenticatorPass
// Stop setting it when guard support gets removed (aka when removing Symfony<5.3 support)
$container->setParameter('lexik_jwt_authentication.authenticator_manager_enabled', true);
}

public function getKey()
Expand Down
3 changes: 2 additions & 1 deletion LexikJWTAuthenticationBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Lexik\Bundle\JWTAuthenticationBundle;

use Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Compiler\DeprecateLegacyGuardAuthenticatorPass;
use Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Compiler\RegisterLegacyGuardAuthenticatorPass;
use Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Compiler\WireGenerateTokenCommandPass;
use Lexik\Bundle\JWTAuthenticationBundle\DependencyInjection\Security\Factory\JWTAuthenticatorFactory;
Expand All @@ -28,7 +29,7 @@ public function build(ContainerBuilder $container)
parent::build($container);

$container->addCompilerPass(new WireGenerateTokenCommandPass());
$container->addCompilerPass(new RegisterLegacyGuardAuthenticatorPass());
$container->addCompilerPass(new DeprecateLegacyGuardAuthenticatorPass());

/** @var SecurityExtension $extension */
$extension = $container->getExtension('security');
Expand Down
19 changes: 19 additions & 0 deletions Resources/config/guard_authenticator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="lexik_jwt_authentication.jwt_token_authenticator" alias="lexik_jwt_authentication.security.guard.jwt_token_authenticator" />

<service id="lexik_jwt_authentication.security.guard.jwt_token_authenticator" class="Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator">
<argument type="service" id="lexik_jwt_authentication.jwt_manager"/>
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="lexik_jwt_authentication.extractor.chain_extractor"/>
<argument type="service">
<service class="Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage" />
</argument>
</service>
</services>
</container>

0 comments on commit 18b026a

Please sign in to comment.