| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace lepiaf\SapientBundle\DependencyInjection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use lepiaf\SapientBundle\Exception\ConfigurationRequiredException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\DependencyInjection\Extension\Extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Config\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class SapientExtension extends Extension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function load(array $configs, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $loader = new YamlFileLoader($container, new FileLocator(dirname(__DIR__) . '/Resources/config')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $loader->load('console.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         $loader->load('service.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $configuration = $this->getConfiguration($configs, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $config = $this->processConfiguration($configuration, $configs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $container->setParameter('sapient.sealing_public_keys', $config['sealing_public_keys']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $container->setParameter('sapient.verifying_public_keys', $config['verifying_public_keys']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->loadSignCapability($config, $loader, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->loadSealCapability($config, $loader, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->loadGuzzleMiddlewareCapability($config, $loader, $container); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->loadVerifyRequestCapability($config, $loader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->loadUnsealRequestCapability($config, $loader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private function loadSignCapability(array $config, YamlFileLoader $loader, ContainerBuilder $container): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         if ($config['sign']['enabled']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $container->setParameter('sapient.sign.public', $config['sign']['public']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             $container->setParameter('sapient.sign.private', $config['sign']['private']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $container->setParameter('sapient.sign.host', $config['sign']['host']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             if ($config['sign']['response']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 $loader->load('sign.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     private function loadSealCapability(array $config, YamlFileLoader $loader, ContainerBuilder $container): void | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         if ($config['seal']['enabled']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             $container->setParameter('sapient.seal.public', $config['seal']['public']); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |             $container->setParameter('sapient.seal.private', $config['seal']['private']); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |             if ($config['seal']['response'] && (!$config['sign']['enabled'] || !$config['sign']['response'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |                 throw new ConfigurationRequiredException('You must enable "sign" option with "sign.response" as true before using "seal.response" feature.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             $loader->load('seal.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private function loadGuzzleMiddlewareCapability(array $config, YamlFileLoader $loader, ContainerBuilder $container): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         if ($config['guzzle_middleware']['enabled']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             if ($config['guzzle_middleware']['verify']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $loader->load('guzzle_middleware/verify_response.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             if ($config['guzzle_middleware']['unseal']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 if (!$config['seal']['enabled'] || !$config['seal']['private']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     throw new ConfigurationRequiredException('You must enable "seal" option and configure a "seal.private" key before using "guzzle_middleware.unseal" feature.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $loader->load('guzzle_middleware/unseal_response.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if ($config['guzzle_middleware']['requester_host']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $container->setParameter('sapient.guzzle_middleware.requester_host', $config['guzzle_middleware']['requester_host']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $loader->load('guzzle_middleware/requester_header.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             if ($config['guzzle_middleware']['sign_request']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 if (!$config['sign']['enabled']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     throw new ConfigurationRequiredException('You must enable "sign" option and configure a "sign.private" key before using "guzzle_middleware.sign_request" feature.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 $loader->load('guzzle_middleware/sign_request.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |             if ($config['guzzle_middleware']['seal_request']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $loader->load('guzzle_middleware/seal_request.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     private function loadUnsealRequestCapability(array $config, YamlFileLoader $loader): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         if ($config['unseal_request']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             if (!$config['seal']['enabled']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 throw new ConfigurationRequiredException('You must enable "seal" option before using "unseal_request" feature.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $loader->load('unseal_request.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     private function loadVerifyRequestCapability(array $config, YamlFileLoader $loader): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if ($config['verify_request']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $loader->load('verify_request.yml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |  |