diff --git a/src/Console/CreateCertificate.php b/src/Console/CreateCertificate.php index bf4eeab..846bd2c 100644 --- a/src/Console/CreateCertificate.php +++ b/src/Console/CreateCertificate.php @@ -13,6 +13,8 @@ class CreateCertificate extends Command */ protected $signature = 'samlidp:cert {--days=7300 : Number of days to add from today as the expiration date} + {--subject= : Subj input for OpenSSL request command} + {--overwrite=0 : Overwrite existing PEM files without asking} {--keyname=key.pem : Full name of the certificate key file} {--certname=cert.pem : Full name to the certificate file}'; @@ -44,6 +46,7 @@ public function handle() $days = $this->option('days'); $keyname = $this->option('keyname'); $certname = $this->option('certname'); + $subject = $this->option('subject'); // Create storage/samlidp directory if (!file_exists($storagePath)) { @@ -52,10 +55,31 @@ public function handle() $key = sprintf('%s/%s', $storagePath, $keyname); $cert = sprintf('%s/%s', $storagePath, $certname); - $question = 'The name chosen for the PEM files already exist. Would you like to overwrite existing PEM files?'; - if ((!file_exists($key) && !file_exists($cert)) || $this->confirm($question)) { + if ($this->canCreateFiles($key, $cert)) { $command = 'openssl req -x509 -sha256 -nodes -days %s -newkey rsa:2048 -keyout %s -out %s'; + if ($subject) { + $command .= ' -subj "' . $subject . '"'; + } + exec(sprintf($command, $days, $key, $cert)); } } + + /** + * @param string $key + * @param string $cert + * @return bool + */ + protected function canCreateFiles($key, $cert) + { + $anyFileExists = file_exists($key) || file_exists($cert); + if(!$anyFileExists) { + return true; + } + if($this->option('overwrite')) { + return true; + } + $question = 'The name chosen for the PEM files already exist. Would you like to overwrite existing PEM files?'; + return $this->confirm($question); + } }