1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Saml2\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class CreateCertificate |
9
|
|
|
* |
10
|
|
|
* @package Slides\Saml2\Commands |
11
|
|
|
*/ |
12
|
|
|
class CreateCertificate extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The name and signature of the console command. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $signature = 'saml2:cert |
20
|
|
|
{--days=7300 : Number of days to add from today as the expiration date} |
21
|
|
|
{--keyname=key.pem : Full name of the certificate key file} |
22
|
|
|
{--certname=cert.pem : Full name to the certificate file}'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The console command description. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $description = 'Create new certificate and private key for your SP'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Create a new command instance. |
33
|
|
|
* |
34
|
|
|
* @return void |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
parent::__construct(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Execute the console command. |
43
|
|
|
* |
44
|
|
|
* @return mixed |
45
|
|
|
*/ |
46
|
|
|
public function handle() |
47
|
|
|
{ |
48
|
|
|
$storagePath = storage_path('samlsp'); |
|
|
|
|
49
|
|
|
$days = $this->option('days'); |
50
|
|
|
$keyname = $this->option('keyname'); |
51
|
|
|
$certname = $this->option('certname'); |
52
|
|
|
|
53
|
|
|
// Create storage/samlidp directory |
54
|
|
|
if (!file_exists($storagePath)) { |
55
|
|
|
mkdir($storagePath, 0755, true); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$key = sprintf('%s/%s', $storagePath, $keyname); |
59
|
|
|
$cert = sprintf('%s/%s', $storagePath, $certname); |
60
|
|
|
$question = 'The name chosen for the PEM files already exist. Would you like to overwrite existing PEM files?'; |
61
|
|
|
if ((!file_exists($key) && !file_exists($cert)) || $this->confirm($question)) { |
62
|
|
|
$command = 'openssl req -x509 -sha256 -nodes -days %s -newkey rsa:2048 -keyout %s -out %s'; |
63
|
|
|
exec(sprintf($command, $days, $key, $cert)); |
64
|
|
|
$this->info("The certificate was successfully created."); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |