| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (c) 2014-2016 Spomky-Labs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This software may be modified and distributed under the terms | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * of the MIT license.  See the LICENSE file for details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace SpomkyLabs\JoseBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Jose\Object\JWKSetInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Jose\Object\RotatableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class RotateCommand extends ContainerAwareCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ->setName('spomky-labs:jose:rotate') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ->setDescription('Rotate a key or keys in the key set') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |                 'service', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 InputArgument::REQUIRED | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                 'ttl', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |                 InputArgument::OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 3600 * 24 * 7 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             ->setHelp(<<<'EOT' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | The <info>%command.name%</info> command will rotate a key or keys in the key set. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |   <info>php %command.full_name%</info> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | EOT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         $service_name = $input->getArgument('service'); | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         if (!$this->getContainer()->has($service_name)) { | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |             $output->writeln(sprintf('<error>The service "%s" does not exist.</error>', $service_name)); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |             return 1; | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |         $service = $this->getContainer()->get($service_name); | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         if (!$service instanceof JWKSetInterface) { | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             $output->writeln(sprintf('<error>The service "%s" is not a key set.</error>', $service_name)); | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |             return 2; | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         if (!$service instanceof RotatableInterface) { | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             $output->writeln(sprintf('<error>The service "%s" is not a rotatable key set.</error>', $service_name)); | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |             return 3; | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         $mtime = $service->getLastModificationTime(); | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         if (null === $mtime) { | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             $service->regen(); | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |             $output->writeln('Done.'); | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |         } elseif ($mtime + $input->getArgument('ttl') <= time()) { | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |             $service->rotate(); | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |             $output->writeln('Done.'); | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         } else { | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             $output->writeln(sprintf('The key set "%s" has not expired.', $service_name)); | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  |