| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright (c) 2014-2018 Spomky-Labs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This software may be modified and distributed under the terms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * of the MIT license.  See the LICENSE file for details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Jose\Bundle\JoseFramework\DependencyInjection\Source\KeyManagement; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Jose\Bundle\JoseFramework\DependencyInjection\Source\KeyManagement\JWKSource\JWKSource as JWKSourceInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Config\Definition\Builder\NodeDefinition; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Config\FileLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class JWKSource implements Source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var null|JWKSourceInterface[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $jwkSources = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function name(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return 'keys'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function load(array $configs, ContainerBuilder $container) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $sources = $this->getJWKSources(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         foreach ($configs[$this->name()] as $name => $itemConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             foreach ($itemConfig as $sourceName => $sourceConfig) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 if (array_key_exists($sourceName, $sources)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                     $source = $sources[$sourceName]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     $source->create($container, 'key', $name, $sourceConfig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |                     throw new \LogicException(sprintf('The JWK definition "%s" is not configured.', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function getNodeDefinition(NodeDefinition $node) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $sourceNodeBuilder = $node | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ->children() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 ->arrayNode('keys') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                     ->treatFalseLike([]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     ->treatNullLike([]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     ->useAttributeAsKey('name') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     ->arrayPrototype() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                         ->validate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                             ->ifTrue(function ($config) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                                 return count($config) !== 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                             ->thenInvalid('One key type must be set.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                         ->end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                         ->children(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         foreach ($this->getJWKSources() as $name => $source) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $sourceNode = $sourceNodeBuilder->arrayNode($name)->canBeUnset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $source->addConfiguration($sourceNode); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function prepend(ContainerBuilder $container, array $config): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return JWKSourceInterface[] | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     private function getJWKSources(): array | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         if (null !== $this->jwkSources) { | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |             return $this->jwkSources; | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |         // load bundled adapter factories | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |         $tempContainer = new ContainerBuilder(); | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         $loader = new YamlFileLoader($tempContainer, new FileLocator(__DIR__.'/../../../Resources/config')); | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |         $loader->load('jwk_sources.yml'); | 
            
                                                        
            
                                    
            
            
                | 104 |  |  |         $services = $tempContainer->findTaggedServiceIds('jose.jwk_source'); | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         $jwkSources = []; | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |         foreach (array_keys($services) as $id) { | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |             $factory = $tempContainer->get($id); | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             if (!$factory instanceof JWKSourceInterface) { | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |                 throw new \InvalidArgumentException(); | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |             } | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |             $jwkSources[str_replace('-', '_', $factory->getKey())] = $factory; | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |         $this->jwkSources = $jwkSources; | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |         return $jwkSources; | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 119 |  |  |  |