ozo2003 /
HelperBundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Sludio\HelperBundle\DependencyInjection\Component; |
||
| 4 | |||
| 5 | use Sludio\HelperBundle\Openidconnect\Provider\OpenIDConnectProvider; |
||
| 6 | use Sludio\HelperBundle\Openidconnect\Provider\BaseProvider; |
||
| 7 | use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
||
| 8 | use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
||
| 9 | use Symfony\Component\Config\Definition\Processor; |
||
| 10 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
| 11 | use Symfony\Component\DependencyInjection\Reference; |
||
| 12 | |||
| 13 | class Openidconnect extends AbstractComponent implements ExtensionInterface |
||
| 14 | { |
||
| 15 | protected $alias; |
||
| 16 | |||
| 17 | public function buildClientConfiguration(NodeDefinition $node) |
||
| 18 | { |
||
| 19 | $node->addDefaultsIfNotSet(); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 20 | $optionsNode = $node->children(); |
||
| 21 | |||
| 22 | // @formatter:off |
||
| 23 | $optionsNode |
||
| 24 | ->scalarNode('client_key')->isRequired()->defaultValue(null)->end() |
||
| 25 | ->scalarNode('client_secret')->defaultValue(null)->end() |
||
| 26 | ->scalarNode('id_token_issuer')->isRequired()->defaultValue(null)->end() |
||
| 27 | ->scalarNode('public_key')->isRequired()->cannotBeEmpty()->end() |
||
| 28 | ->scalarNode('base_uri')->isRequired()->end() |
||
| 29 | ->scalarNode('user_provider')->defaultValue(BaseProvider::class)->end() |
||
| 30 | ->scalarNode('use_session')->defaultValue(false)->end() |
||
| 31 | ->scalarNode('verify')->defaultValue(true)->end() |
||
| 32 | ->arrayNode('redirect') |
||
| 33 | ->addDefaultsIfNotSet() |
||
| 34 | ->children() |
||
| 35 | ->enumNode('type') |
||
| 36 | ->values(array('route', 'uri')) |
||
| 37 | ->defaultValue('route') |
||
| 38 | ->end() |
||
| 39 | ->scalarNode('route')->defaultValue(null)->end() |
||
| 40 | ->scalarNode('uri')->defaultValue(null)->end() |
||
| 41 | ->arrayNode('params')->prototype('variable')->end()->end() |
||
| 42 | ->end() |
||
| 43 | ->end() |
||
| 44 | ->arrayNode('uris') |
||
| 45 | ->prototype('array') |
||
| 46 | ->prototype('variable')->end() |
||
| 47 | ->end() |
||
| 48 | ->end() |
||
| 49 | ; |
||
| 50 | // @formatter:on |
||
| 51 | |||
| 52 | $optionsNode->end(); |
||
| 53 | } |
||
| 54 | |||
| 55 | private function buildUri(NodeDefinition $node) |
||
| 56 | { |
||
| 57 | $node->addDefaultsIfNotSet(); |
||
| 58 | $optionsNode = $node->children(); |
||
| 59 | |||
| 60 | // @formatter:off |
||
| 61 | $optionsNode |
||
| 62 | ->arrayNode('params') |
||
| 63 | ->prototype('variable')->end() |
||
| 64 | ->end() |
||
| 65 | ->arrayNode('url_params') |
||
| 66 | ->prototype('variable')->end() |
||
| 67 | ->end() |
||
| 68 | ->enumNode('method')->values(array(OpenIDConnectProvider::METHOD_GET, OpenIDConnectProvider::METHOD_POST))->cannotBeEmpty()->end() |
||
| 69 | ; |
||
| 70 | // @formatter:on |
||
| 71 | |||
| 72 | $optionsNode->end(); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function configureClient(ContainerBuilder $container, $clientServiceKey, array $options = []) |
||
| 76 | { |
||
| 77 | $clientDefinition = $container->register($clientServiceKey, $container->getParameter($clientServiceKey.'.user_provider')); |
||
| 78 | $clientDefinition->setArguments([ |
||
| 79 | $container->getParameter($clientServiceKey), |
||
| 80 | [], |
||
| 81 | new Reference('router'), |
||
| 82 | new Reference('session'), |
||
| 83 | ]); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function configure(ContainerBuilder $container, $alias) |
||
| 87 | { |
||
| 88 | $this->alias = $alias.'.openidconnect'; |
||
| 89 | $clientConfigurations = $container->getParameter($this->alias.'.clients'); |
||
| 90 | $clientServiceKeys = []; |
||
| 91 | /** @var $clientConfigurations array */ |
||
| 92 | foreach ($clientConfigurations as $key => $clientConfig) { |
||
| 93 | $tree = new TreeBuilder(); |
||
| 94 | $processor = new Processor(); |
||
| 95 | $node = $tree->root('sludio_helper_openidconnect_client/clients/'.$key); |
||
| 96 | $this->buildClientConfiguration($node); |
||
| 97 | /** @var array $config */ |
||
| 98 | $config = $processor->process($tree->buildTree(), [$clientConfig]); |
||
| 99 | $clientServiceKey = $this->alias.'.client.'.$key; |
||
| 100 | $container->setParameter($clientServiceKey, $clientConfig); |
||
| 101 | $service = [ |
||
| 102 | 'key' => $clientServiceKey, |
||
| 103 | ]; |
||
| 104 | if (isset($config['options']['name'])) { |
||
| 105 | $service['name'] = $config['options']['name']; |
||
| 106 | } else { |
||
| 107 | $service['name'] = ucfirst($key); |
||
| 108 | } |
||
| 109 | |||
| 110 | $clientServiceKeys[$key] = $service; |
||
| 111 | foreach ($config as $configKey => $configValue) { |
||
| 112 | if ('options' === $configKey) { |
||
| 113 | if (\is_array($configValue)) { |
||
| 114 | foreach ($configValue as $parameterKey => $parameterValue) { |
||
| 115 | $container->setParameter($clientServiceKey.'.option.'.$parameterKey, $parameterValue); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | $container->setParameter($clientServiceKey.'.'.$configKey, $configValue); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | $uriConfigurations = $container->getParameter($this->alias.'.client.'.$key.'.uris'); |
||
| 123 | /** @var $uriConfigurations array */ |
||
| 124 | foreach ($uriConfigurations as $subKey => $uriConfig) { |
||
| 125 | $tree = new TreeBuilder(); |
||
| 126 | $processor = new Processor(); |
||
| 127 | $node = $tree->root('sludio_helper_openidconnect_client/clients/'.$key.'/uris/'.$subKey); |
||
| 128 | $this->buildUri($node); |
||
| 129 | $config = $processor->process($tree->buildTree(), [$uriConfig]); |
||
| 130 | $params = []; |
||
| 131 | foreach ($config as $subConfigKey => $subConfigValue) { |
||
| 132 | if ($subConfigKey === 'params') { |
||
| 133 | if (\is_array($subConfigValue)) { |
||
| 134 | foreach ($subConfigValue as $subParameterKey => $subParameterValue) { |
||
| 135 | $params[$subParameterKey] = $subParameterValue; |
||
| 136 | } |
||
| 137 | if (!empty($params)) { |
||
| 138 | $params['client_id'] = $container->getParameter($this->alias.'.client.'.$key.'.client_key'); |
||
| 139 | $container->setParameter($clientServiceKey.'.'.$subKey.'.'.$subConfigKey, $params); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } else { |
||
| 143 | $container->setParameter($clientServiceKey.'.'.$subKey.'.'.$subConfigKey, $subConfigValue); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | $this->configureClient($container, $clientServiceKey); |
||
| 148 | } |
||
| 149 | $container->getDefinition($this->alias.'.registry')->replaceArgument(0, $clientServiceKeys); |
||
| 150 | } |
||
| 151 | } |
||
| 152 |