1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Happyr\Auth0Bundle\DependencyInjection; |
6
|
|
|
|
7
|
|
|
use Auth0\SDK\API\Authentication; |
8
|
|
|
use Happyr\Auth0Bundle\Factory\ManagementFactory; |
9
|
|
|
use Happyr\Auth0Bundle\Security\Auth0EntryPoint; |
10
|
|
|
use Happyr\Auth0Bundle\Security\Auth0UserProviderInterface; |
11
|
|
|
use Happyr\Auth0Bundle\Security\Authentication\Auth0Authenticator; |
12
|
|
|
use Symfony\Component\Config\FileLocator; |
13
|
|
|
use Symfony\Component\DependencyInjection\ChildDefinition; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
16
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
17
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
18
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface; |
19
|
|
|
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This is the class that loads and manages your bundle configuration. |
23
|
|
|
* |
24
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html} |
25
|
|
|
*/ |
26
|
|
|
final class HappyrAuth0Extension extends Extension |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function load(array $configs, ContainerBuilder $container) |
32
|
|
|
{ |
33
|
|
|
$configuration = new Configuration(); |
34
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
35
|
|
|
|
36
|
|
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
37
|
|
|
$loader->load('services.yml'); |
38
|
|
|
|
39
|
|
|
$container->setParameter('auth0.domain', $config['domain']); |
40
|
|
|
$container->setParameter('auth0.login_domain', $config['login_domain'] ?? $config['domain']); |
41
|
|
|
$container->setParameter('auth0.client_id', $config['client_id']); |
42
|
|
|
$container->setParameter('auth0.client_secret', $config['client_secret']); |
43
|
|
|
$container->setParameter('auth0.scope', $config['scope']); |
44
|
|
|
$container->setParameter('auth0.audience', $config['audience']); |
45
|
|
|
|
46
|
|
|
if ($config['cache']) { |
47
|
|
|
$container->setAlias('auth0.cache', $config['cache']); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($config['firewall']['enabled']) { |
51
|
|
|
$this->configureFirewall($container, $config['firewall']); |
52
|
|
|
} else { |
53
|
|
|
$container->removeDefinition(Auth0Authenticator::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!empty($config['httplug_client_service'])) { |
57
|
|
|
$container->getDefinition(Authentication::class) |
58
|
|
|
->replaceArgument(5, new Reference($config['httplug_client_service'])); |
59
|
|
|
|
60
|
|
|
$container->getDefinition(ManagementFactory::class) |
61
|
|
|
->replaceArgument(3, new Reference($config['httplug_client_service'])); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function configureFirewall(ContainerBuilder $container, array $config) |
66
|
|
|
{ |
67
|
|
|
if (!(null === $config['success_handler'] xor null === $config['default_target_path'])) { |
68
|
|
|
throw new \LogicException('You must define either "happyr_auth0.firewall.default_target_path" or "happyr_auth0.firewall.success_handler". Exactly one of them, not both.'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!(null === $config['failure_handler'] xor null === $config['failure_path'])) { |
72
|
|
|
throw new \LogicException('You must define either "happyr_auth0.firewall.failure_path" or "happyr_auth0.firewall.failure_handler". Exactly one of them, not both.'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (null === $successHandler = $config['success_handler']) { |
76
|
|
|
$def = $container->setDefinition($successHandler = 'happyr_auth0.success_handler', new ChildDefinition('security.authentication.success_handler')); |
77
|
|
|
$def->replaceArgument(1, ['default_target_path' => $config['default_target_path']]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (null === $failureHandler = $config['failure_handler']) { |
81
|
|
|
$def = $container->setDefinition($failureHandler = 'happyr_auth0.failure_handler', new ChildDefinition('security.authentication.failure_handler')); |
82
|
|
|
$def->replaceArgument(2, ['failure_path' => $config['failure_path']]); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$container->getDefinition(Auth0EntryPoint::class)->replaceArgument(5, $config['check_route']); |
86
|
|
|
$container->setAlias('auth0.entry_point', Auth0EntryPoint::class); |
87
|
|
|
|
88
|
|
|
$container->setAlias('auth0.authenticator', Auth0Authenticator::class); |
89
|
|
|
$def = $container->getDefinition(Auth0Authenticator::class); |
90
|
|
|
$def->setArgument('$checkRoute', $config['check_route']); |
91
|
|
|
$def->addTag('container.service_subscriber', ['key' => AuthenticationFailureHandlerInterface::class, 'id' => $failureHandler]); |
92
|
|
|
$def->addTag('container.service_subscriber', ['key' => AuthenticationSuccessHandlerInterface::class, 'id' => $successHandler]); |
93
|
|
|
|
94
|
|
|
if (!empty($config['user_provider'])) { |
95
|
|
|
$def->addTag('container.service_subscriber', ['key' => Auth0UserProviderInterface::class, 'id' => $config['user_provider']]); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|