Skip to content

Commit

Permalink
Merge pull request #23 from Muffinman/master
Browse files Browse the repository at this point in the history
Adding subjectAlternativeNames support
  • Loading branch information
Daanra authored Sep 16, 2022
2 parents c820748 + 884577f commit c925b62
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ $certificate->delete();
$certificate->forceDelete();
```


## Subject Alternative Names

It's possible to specify Subject Alternative Names as below:

```php
LetsEncrypt::certificate('mydomain.com')
->setSubjectAlternativeNames(['mydomain2.com'])
->create();
```

## Failure events

If one of the jobs fails, one of the following events will be dispatched:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddLetsEncryptCertificatesSubjectAlternativeNames extends Migration
{
public function up()
{
Schema::table('lets_encrypt_certificates', function (Blueprint $table) {
$table->json('subject_alternative_names')->nullable()->after('domain');
});
}

public function down()
{
Schema::table('lets_encrypt_certificates', function (Blueprint $table) {
$table->dropColumn('subject_alternative_names');
});
}
}
2 changes: 1 addition & 1 deletion src/Jobs/RequestCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(LetsEncryptCertificate $certificate, int $tries = nu

public function handle()
{
$distinguishedName = new DistinguishedName($this->certificate->domain);
$distinguishedName = new DistinguishedName($this->certificate->domain, null, null, null, null, null, null, $this->certificate->subject_alternative_names);
$csr = new CertificateRequest($distinguishedName, (new KeyPairGenerator())->generateKeyPair());
$client = LetsEncrypt::createClient();
$certificateResponse = $client->requestCertificate($this->certificate->domain, $csr);
Expand Down
1 change: 1 addition & 0 deletions src/Models/LetsEncryptCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class LetsEncryptCertificate extends Model

protected $casts = [
'created' => 'boolean',
'subject_alternative_names' => 'json',
];

public function newEloquentBuilder($query): LetsEncryptCertificateBuilder
Expand Down
18 changes: 18 additions & 0 deletions src/PendingCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ class PendingCertificate
*/
protected $delay = 0;

/**
* @var array
*/
protected $subjectAlternativeNames = [];

/**
* PendingCertificate constructor.
* @param string $domain
Expand All @@ -66,6 +71,7 @@ public function create(): LetsEncryptCertificate

$certificate = LetsEncryptCertificate::create([
'domain' => $this->domain,
'subject_alternative_names' => $this->subjectAlternativeNames,
]);

RegisterAccount::withChain(array_merge([
Expand Down Expand Up @@ -117,6 +123,18 @@ public function renew(): LetsEncryptCertificate
return $certificate;
}


/**
* @param array $domains
* @return static
*/
public function setSubjectAlternativeNames(array $domains): self
{
$this->subjectAlternativeNames = $domains;

return $this;
}

/**
* @param int $tries
* @return static
Expand Down
15 changes: 15 additions & 0 deletions tests/Facades/LetsEncryptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,19 @@ public function test_can_create_pending()
RequestCertificate::class,
]);
}

/** @test */
public function test_can_create_now_with_san()
{
Bus::fake();

$certificate = LetsEncrypt::certificate('somedomain.com')
->setSubjectAlternativeNames(['other.somedomain.com'])
->create();

$this->assertEquals('somedomain.com', $certificate->domain);
$this->assertEquals(['other.somedomain.com'], $certificate->subject_alternative_names);

Bus::assertDispatched(RegisterAccount::class);
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function getEnvironmentSetUp($app)
]);

include_once __DIR__.'/../database/migrations/create_lets_encrypt_certificates_table.php.stub';
include_once __DIR__.'/../database/migrations/add_lets_encrypt_certificates_subject_alternative_names.php.stub';
(new \CreateLetsEncryptCertificatesTable())->up();
(new \AddLetsEncryptCertificatesSubjectAlternativeNames())->up();
}
}

0 comments on commit c925b62

Please sign in to comment.