Skip to content
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

Command to create SP certificates #81

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ php artisan migrate

Once you publish `saml2.php` to `app/config`, you need to configure your SP. Most of options are inherited from [OneLogin Toolkit](https://github.com/onelogin/php-saml), so you can check documentation there.

#### Service Provider Certificate Creation (SP certificate)

```
php artisan saml2:cert
```

Once you create the certificates, configure your SP `SAML2_SP_CERT_x509` and `SAML2_SP_CERT_PRIVATEKEY`.

#### Identity Providers (IdPs)

To distinguish between identity providers there is an entity called Tenant that represent each IdP.
Expand Down
67 changes: 67 additions & 0 deletions src/Commands/CreateCertificate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Slides\Saml2\Commands;

use Illuminate\Console\Command;

/**
* Class CreateCertificate
*
* @package Slides\Saml2\Commands
*/
class CreateCertificate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'saml2:cert
{--days=7300 : Number of days to add from today as the expiration date}
{--keyname=key.pem : Full name of the certificate key file}
{--certname=cert.pem : Full name to the certificate file}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create new certificate and private key for your SP';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$storagePath = storage_path('samlsp');
$days = $this->option('days');
$keyname = $this->option('keyname');
$certname = $this->option('certname');

// Create storage/samlidp directory
if (!file_exists($storagePath)) {
mkdir($storagePath, 0755, true);
}

$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)) {
$command = 'openssl req -x509 -sha256 -nodes -days %s -newkey rsa:2048 -keyout %s -out %s';
exec(sprintf($command, $days, $key, $cert));
$this->info("The certificate was successfully created.");
}
}
}
1 change: 1 addition & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ protected function bootPublishes()
protected function bootCommands()
{
$this->commands([
\Slides\Saml2\Commands\CreateCertificate::class,
\Slides\Saml2\Commands\CreateTenant::class,
\Slides\Saml2\Commands\UpdateTenant::class,
\Slides\Saml2\Commands\DeleteTenant::class,
Expand Down