| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BenTools\Shh\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BenTools\Shh\Shh; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Style\SymfonyStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\DependencyInjection\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpKernel\Kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | final class ChangePassphraseCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected static $defaultName = 'shh:change:passphrase'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var Shh | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $shh; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var Filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     private $fs; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $keysDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $privateKey; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     public function __construct(Shh $shh, Filesystem $fs, string $keysDir, ?string $privateKey) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         $this->shh = $shh; | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         $this->fs = $fs; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         $this->keysDir = $keysDir; | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->privateKey = $privateKey; | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             ->setDescription('Change passphrase, generate a new private key.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             ->addOption('old-passphrase', '', InputOption::VALUE_OPTIONAL, 'Your current passhrase.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ->addOption('new-passphrase', '', InputOption::VALUE_OPTIONAL, '(Optional) your new passhrase.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ->addOption('overwrite', '', InputOption::VALUE_NONE, 'Overwrite the existing key.') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected function interact(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $io = new SymfonyStyle($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         if (null === $input->getOption('old-passphrase')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $input->setOption('old-passphrase', $io->askHidden( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 'Enter your old passphrase:', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 function ($passphrase) use ($io) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                     if (0 === \strlen($passphrase)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     return $passphrase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (null === $input->getOption('new-passphrase')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $input->setOption('new-passphrase', $io->askHidden( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 'Enter your new passphrase:', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 function ($passphrase) use ($io) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                     if (0 === \strlen($passphrase)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                     if ($passphrase !== $io->askHidden('Confirm:')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                         throw new \InvalidArgumentException("Both passphrases don't match."); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                     return $passphrase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $input->setOption('overwrite', $io->confirm('Overwrite current private key?')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return int|void|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $io = new SymfonyStyle($input, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if (null === $this->privateKey) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             $io->error('Private key was not found.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $passphrase = $input->getOption('new-passphrase'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $dir = $this->keysDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $oldPrivateKey = $this->privateKey; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $newPrivateKey = Shh::changePassphrase($oldPrivateKey, $input->getOption('old-passphrase'), $passphrase); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $io->comment('Here is your new private key:'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $io->writeln($newPrivateKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         if (true === $input->getOption('overwrite')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             $this->fs->dumpFile($dir . '/private.pem', $newPrivateKey); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $io->success(\sprintf('%s was successfully updated.', $dir . '/private.pem')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         $io->caution('Don\'t forget to report your new passphrase into the SHH_PASSPHRASE environment variable, and to deploy the new private key to everywhere it\'s needed!'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  |