Skip to content
This repository was archived by the owner on Aug 7, 2023. It is now read-only.

Commit 541e76b

Browse files
committed
add support symfony 6
1 parent 0ee61c5 commit 541e76b

File tree

10 files changed

+37
-46
lines changed

10 files changed

+37
-46
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"php": ">=8.0",
1818
"php-istio/jwt-payload-extractor": "^v1.1.1",
1919
"symfony/psr7-pack": "^1.0",
20-
"symfony/security-bundle": "^5.3"
20+
"symfony/security-bundle": "^5.3||^6.0"
2121
},
2222
"autoload": {
2323
"psr-4": {
@@ -30,9 +30,9 @@
3030
}
3131
},
3232
"require-dev": {
33-
"symfony/browser-kit": "^5.3",
34-
"symfony/console": "^5.3",
35-
"symfony/framework-bundle": "^5.3",
36-
"symfony/phpunit-bridge": "^5.3"
33+
"symfony/browser-kit": "^5.3||^6.0",
34+
"symfony/console": "^5.3||^6.0",
35+
"symfony/framework-bundle": "^5.3||^6.0",
36+
"symfony/phpunit-bridge": "^5.3||^6.0"
3737
}
3838
}

src/Authenticator/Authenticator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Symfony\Component\Security\Core\User\UserProviderInterface;
2020
use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
2121
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
22-
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
22+
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
2323
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
2424
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
2525

@@ -56,7 +56,7 @@ public function supports(Request $request): ?bool
5656
return false;
5757
}
5858

59-
public function authenticate(Request $request): PassportInterface
59+
public function authenticate(Request $request): Passport
6060
{
6161
[$userIdentifierClaim, $payload] = $request->attributes->get('_user_identifier_claim_and_payload');
6262
$request->attributes->remove('_user_identifier_claim_and_payload');
@@ -88,17 +88,17 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
8888
throw $exception;
8989
}
9090

91-
public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface
91+
public function createToken(Passport $passport, string $firewallName): TokenInterface
9292
{
9393
/** @var SelfValidatingPassport $passport */
9494
$payload = $passport->getAttribute('_payload');
95-
$token = parent::createAuthenticatedToken($passport, $firewallName);
95+
$token = parent::createToken($passport, $firewallName);
9696
$token->setAttribute('jwt_payload', $payload);
9797

9898
return $token;
9999
}
100100

101-
public function start(Request $request, AuthenticationException $authException = null)
101+
public function start(Request $request, AuthenticationException $authException = null): Response
102102
{
103103
return new Response('Istio JWT in request\'s missing or invalid.', Response::HTTP_UNAUTHORIZED);
104104
}

src/DependencyInjection/Security/AuthenticatorFactory.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use Istio\Symfony\JWTAuthentication\Authenticator\UserIdentifierClaimMapping;
1414
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\AuthenticatorFactoryInterface;
15-
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SecurityFactoryInterface;
1615
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
1716
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1817
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
@@ -21,39 +20,34 @@
2120
use Symfony\Component\DependencyInjection\Definition;
2221
use Symfony\Component\DependencyInjection\Reference;
2322

24-
final class AuthenticatorFactory implements SecurityFactoryInterface, AuthenticatorFactoryInterface
23+
final class AuthenticatorFactory implements AuthenticatorFactoryInterface
2524
{
25+
public const PRIORITY = -40;
26+
2627
public function createAuthenticator(
2728
ContainerBuilder $container,
2829
string $firewallName,
2930
array $config,
3031
string $userProviderId
31-
) {
32+
): string|array {
3233
$authenticator = sprintf('security.authenticator.istio_jwt_authenticator.%s', $firewallName);
3334
$definition = new ChildDefinition('istio.jwt_authentication.authenticator');
34-
$definition->replaceArgument(0, $this->createUserIdentifierClaimMappings($container, $authenticator, $config['rules']));
35+
$definition->replaceArgument(
36+
0,
37+
$this->createUserIdentifierClaimMappings($container, $authenticator, $config['rules'])
38+
);
3539
$definition->replaceArgument(1, new Reference($userProviderId));
3640
$container->setDefinition($authenticator, $definition);
3741

3842
return $authenticator;
3943
}
4044

41-
public function create(
42-
ContainerBuilder $container,
43-
string $id,
44-
array $config,
45-
string $userProviderId,
46-
?string $defaultEntryPointId
47-
) {
48-
throw new \LogicException('Istio JWT Authentication is not supported when "security.enable_authenticator_manager" is not set to true.');
49-
}
50-
5145
public function getPosition()
5246
{
5347
return 'pre_auth';
5448
}
5549

56-
public function getKey()
50+
public function getKey(): string
5751
{
5852
return 'istio_jwt_authenticator';
5953
}
@@ -194,4 +188,9 @@ private function createPayloadExtractor(
194188

195189
return new Reference($id);
196190
}
191+
192+
public function getPriority(): int
193+
{
194+
return self::PRIORITY;
195+
}
197196
}

src/JWTAuthenticationBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ public function build(ContainerBuilder $container)
2525
/** @var SecurityExtension $extension */
2626
$extension = $container->getExtension('security');
2727
$extension->addUserProviderFactory(new StatelessUserProviderFactory());
28-
$extension->addSecurityListenerFactory(new AuthenticatorFactory());
28+
$extension->addAuthenticatorFactory(new AuthenticatorFactory());
2929
}
3030
}

src/User/JWTPayloadAwareUserProviderInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010

1111
namespace Istio\Symfony\JWTAuthentication\User;
1212

13+
use Symfony\Component\Security\Core\User\UserInterface;
1314
use Symfony\Component\Security\Core\User\UserProviderInterface;
1415

1516
interface JWTPayloadAwareUserProviderInterface extends UserProviderInterface
1617
{
17-
public function loadUserByIdentifier(string $identifier, array $payload = null);
18+
public function loadUserByIdentifier(string $identifier, array $payload = null): UserInterface;
1819
}

src/User/StatelessUserProvider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function __construct(private string $statelessUserClass)
2121
}
2222
}
2323

24-
public function loadUserByIdentifier(string $identifier, array $payload = null)
24+
public function loadUserByIdentifier(string $identifier, array $payload = null): UserInterface
2525
{
2626
if (null === $payload) {
2727
throw new \LogicException(sprintf('`$payload` must be set when use %s!', self::class));
@@ -37,12 +37,12 @@ public function loadUserByIdentifier(string $identifier, array $payload = null)
3737
return $cache[$key];
3838
}
3939

40-
public function refreshUser(UserInterface $user)
40+
public function refreshUser(UserInterface $user): UserInterface
4141
{
4242
return $user;
4343
}
4444

45-
public function supportsClass(string $class)
45+
public function supportsClass(string $class): bool
4646
{
4747
return is_a($class, StatelessUserInterface::class, true);
4848
}

tests/Fixtures/StatelessUser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function fromPayload(array $payload): static
2424
return $instance;
2525
}
2626

27-
public function getRoles()
27+
public function getRoles(): array
2828
{
2929
return [];
3030
}
@@ -49,7 +49,7 @@ public function getUsername()
4949
// TODO: Implement getUsername() method.
5050
}
5151

52-
public function getUserIdentifier()
52+
public function getUserIdentifier(): string
5353
{
5454
// TODO: Implement getUsername() method.
5555
}

tests/TestKernel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
class TestKernel extends Kernel implements CompilerPassInterface
2929
{
30-
public function registerBundles()
30+
public function registerBundles(): array
3131
{
3232
return [
3333
new FrameworkBundle(),
@@ -128,15 +128,15 @@ function (ContainerBuilder $container) {
128128
/**
129129
* {@inheritdoc}
130130
*/
131-
public function getCacheDir()
131+
public function getCacheDir(): string
132132
{
133133
return sprintf('%s/tests/.kernel/cache', $this->getProjectDir());
134134
}
135135

136136
/**
137137
* {@inheritdoc}
138138
*/
139-
public function getLogDir()
139+
public function getLogDir(): string
140140
{
141141
return sprintf('%s/tests/.kernel/logs', $this->getProjectDir());
142142
}

tests/Unit/Authenticator/AuthenticatorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ public function testAuthenticateWithJWTPayloadAwareUserProvider(Request $request
9696
* @dataProvider validRequests
9797
* @depends testAuthenticate
9898
*/
99-
public function testCreateAuthenticatedToken(Request $request)
99+
public function testCreateToken(Request $request)
100100
{
101101
$authenticator = $this->createAuthenticatorWithInMemoryUserProvider(['valid' => []]);
102102
$authenticator->supports($request);
103103
$payload = $request->attributes->get('_user_identifier_claim_and_payload')[1];
104104
$passport = $authenticator->authenticate($request);
105-
$token = $authenticator->createAuthenticatedToken($passport, 'test');
105+
$token = $authenticator->createToken($passport, 'test');
106106

107107
$this->assertTrue($token->hasAttribute('jwt_payload'));
108108
$this->assertSame($payload, $token->getAttribute('jwt_payload'));

tests/Unit/DepdendencyInjection/Security/AuthenticatorFactoryTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1616
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1717
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
18-
use Symfony\Component\DependencyInjection\ContainerBuilder;
1918
use Symfony\Component\DependencyInjection\Reference;
2019

2120
class AuthenticatorFactoryTest extends TestCase
@@ -56,14 +55,6 @@ public function testAddInvalidConfiguration(array $inputConfig)
5655
$node->finalize($normalizedConfig);
5756
}
5857

59-
public function testExceptionWhenCallCreate()
60-
{
61-
$this->expectException(\LogicException::class);
62-
63-
$factory = new AuthenticatorFactory();
64-
$factory->create(new ContainerBuilder(), 'test', [], 'test', 'test');
65-
}
66-
6758
public function testCreateAuthenticator()
6859
{
6960
$config = [

0 commit comments

Comments
 (0)