Skip to content

Add option to skip interaction when using php artisan samlidp:cert #149

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/Console/CreateCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}';

Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}
}