Skip to content

Commit a077905

Browse files
authored
Merge pull request #24 from Daanra/feat/subject-alternative-names
Add Subject Alternative Names support
2 parents 46964d8 + 76e5612 commit a077905

File tree

9 files changed

+79
-3
lines changed

9 files changed

+79
-3
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ $certificate->delete();
111111
$certificate->forceDelete();
112112
```
113113

114+
115+
## Subject Alternative Names
116+
117+
It's also possible to specify Subject Alternative Names as below (requires >= 0.5.0):
118+
119+
```php
120+
LetsEncrypt::certificate('mydomain.com')
121+
->setSubjectAlternativeNames(['mydomain2.com'])
122+
->create();
123+
```
124+
114125
## Failure events
115126

116127
If one of the jobs fails, one of the following events will be dispatched:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddLetsEncryptCertificatesSubjectAlternativeNames extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table('lets_encrypt_certificates', function (Blueprint $table) {
12+
$table->json('subject_alternative_names')->default('[]')->after('domain');
13+
});
14+
}
15+
16+
public function down()
17+
{
18+
Schema::table('lets_encrypt_certificates', function (Blueprint $table) {
19+
$table->dropColumn('subject_alternative_names');
20+
});
21+
}
22+
}

src/Jobs/RequestCertificate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function __construct(LetsEncryptCertificate $certificate, int $tries = nu
3838

3939
public function handle()
4040
{
41-
$distinguishedName = new DistinguishedName($this->certificate->domain);
41+
$distinguishedName = new DistinguishedName($this->certificate->domain, null, null, null, null, null, null, $this->certificate->subject_alternative_names);
4242
$csr = new CertificateRequest($distinguishedName, (new KeyPairGenerator())->generateKeyPair());
4343
$client = LetsEncrypt::createClient();
4444
$certificateResponse = $client->requestCertificate($this->certificate->domain, $csr);

src/LetsEncryptServiceProvider.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ public function boot()
2626
__DIR__ . "/../database/migrations/{$migrationFileName}.stub" => database_path('migrations/' . date('Y_m_d_His', time()) . '_' . $migrationFileName),
2727
], 'lets-encrypt');
2828
}
29+
30+
$sanMigrationFileName = 'add_lets_encrypt_certificates_subject_alternative_names.php';
31+
if (! $this->migrationFileExists($sanMigrationFileName)) {
32+
$this->publishes([
33+
__DIR__ . "/../database/migrations/{$sanMigrationFileName}.stub" => database_path('migrations/' . date('Y_m_d_His', time() + 1) . '_' . $sanMigrationFileName),
34+
], ['lets-encrypt', 'lets-encrypt-0.5']);
35+
}
2936
}
3037

3138
$this->commands([

src/Models/LetsEncryptCertificate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* @property \Illuminate\Support\Carbon|null $created_at
2121
* @property \Illuminate\Support\Carbon|null $updated_at
2222
* @property \Illuminate\Support\Carbon|null $deleted_at
23+
* @property array $subject_alternative_names
2324
* @property-read bool $has_expired
2425
* @method static \Daanra\LaravelLetsEncrypt\Builders\LetsEncryptCertificateBuilder|\Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate query()
2526
* @method static \Daanra\LaravelLetsEncrypt\Builders\LetsEncryptCertificateBuilder|\Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate newQuery()
@@ -42,6 +43,7 @@ class LetsEncryptCertificate extends Model
4243

4344
protected $casts = [
4445
'created' => 'boolean',
46+
'subject_alternative_names' => 'array',
4547
];
4648

4749
public function newEloquentBuilder($query): LetsEncryptCertificateBuilder

src/PendingCertificate.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
class PendingCertificate
1414
{
15-
1615
/**
1716
* @var string
1817
*/
@@ -43,6 +42,11 @@ class PendingCertificate
4342
*/
4443
protected $delay = 0;
4544

45+
/**
46+
* @var array
47+
*/
48+
protected $subjectAlternativeNames = [];
49+
4650
/**
4751
* PendingCertificate constructor.
4852
* @param string $domain
@@ -67,6 +71,7 @@ public function create(): LetsEncryptCertificate
6771

6872
$certificate = LetsEncryptCertificate::create([
6973
'domain' => $this->domain,
74+
'subject_alternative_names' => $this->subjectAlternativeNames,
7075
]);
7176

7277
RegisterAccount::withChain(array_merge([
@@ -118,6 +123,18 @@ public function renew(): LetsEncryptCertificate
118123
return $certificate;
119124
}
120125

126+
127+
/**
128+
* @param array $domains
129+
* @return static
130+
*/
131+
public function setSubjectAlternativeNames(array $domains): self
132+
{
133+
$this->subjectAlternativeNames = $domains;
134+
135+
return $this;
136+
}
137+
121138
/**
122139
* @param int $tries
123140
* @return static

src/Traits/Retryable.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
trait Retryable
66
{
7-
87
/**
98
* The number of times the job may be attempted.
109
*

tests/Facades/LetsEncryptTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,26 @@ public function test_can_create_pending()
6969
$certificate = LetsEncrypt::certificate('test.test')->create();
7070

7171
$this->assertEquals('test.test', $certificate->domain);
72+
$this->assertEquals([], $certificate->subject_alternative_names);
7273

7374
Queue::assertPushedWithChain(RegisterAccount::class, [
7475
RequestAuthorization::class,
7576
RequestCertificate::class,
7677
]);
7778
}
79+
80+
/** @test */
81+
public function test_can_create_now_with_san()
82+
{
83+
Bus::fake();
84+
85+
$certificate = LetsEncrypt::certificate('somedomain.com')
86+
->setSubjectAlternativeNames(['other.somedomain.com'])
87+
->create();
88+
89+
$this->assertEquals('somedomain.com', $certificate->domain);
90+
$this->assertEquals(['other.somedomain.com'], $certificate->subject_alternative_names);
91+
92+
Bus::assertDispatched(RegisterAccount::class);
93+
}
7894
}

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function getEnvironmentSetUp($app)
2929
]);
3030

3131
include_once __DIR__.'/../database/migrations/create_lets_encrypt_certificates_table.php.stub';
32+
include_once __DIR__.'/../database/migrations/add_lets_encrypt_certificates_subject_alternative_names.php.stub';
3233
(new \CreateLetsEncryptCertificatesTable())->up();
34+
(new \AddLetsEncryptCertificatesSubjectAlternativeNames())->up();
3335
}
3436
}

0 commit comments

Comments
 (0)