Skip to content

Commit

Permalink
Merge pull request #11 from Daanra/prepare-0.3
Browse files Browse the repository at this point in the history
Release v0.3.0
  • Loading branch information
Daanra authored Oct 29, 2021
2 parents bf6cdab + 25dd96e commit e8f58d5
Show file tree
Hide file tree
Showing 22 changed files with 702 additions and 29 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ Creating a new SSL certificate for a specific domain is easy:
\Daanra\LaravelLetsEncrypt\Facades\LetsEncrypt::createNow('mydomain.com');
```

Alternative syntax available from v0.3.0:

```php
LetsEncrypt::certificate('mydomain.com')
->chain([
new SomeJob()
])
->delay(5)
->retryAfter(4)
->setTries(4)
->setRetryList([1, 5, 10])
->create(); // or ->renew()
```

Where you can specify values for all jobs:

- tries (The number of times the job may be attempted)
- retryAfter (The number of seconds to wait before retrying the job)
- retryList (The list of seconds to wait before retrying the job)
- chain (Chain some jobs after the certificate has successfully been obtained)
- delay (Set the desired delay for the job)

You could also achieve the same by using an artisan command:
```bash
php artisan lets-encrypt:create -d mydomain.com
Expand All @@ -84,6 +106,23 @@ $certificate->delete();
$certificate->forceDelete();
```

## Failure events

If one of the jobs fails, one of the following events will be dispatched:
```php
Daanra\LaravelLetsEncrypt\Events\CleanUpChallengeFailed
Daanra\LaravelLetsEncrypt\Events\ChallengeAuthorizationFailed
Daanra\LaravelLetsEncrypt\Events\RegisterAccountFailed
Daanra\LaravelLetsEncrypt\Events\RequestAuthorizationFailed
Daanra\LaravelLetsEncrypt\Events\RequestCertificateFailed
Daanra\LaravelLetsEncrypt\Events\StoreCertificateFailed
Daanra\LaravelLetsEncrypt\Events\RenewExpiringCertificatesFailed
```

Every event implements the `Daanra\LaravelLetsEncrypt\Interfaces\LetsEncryptCertificateFailed` interface so you can listen for that as well.

## Automatically renewing certificates

Certificates are valid for 90 days. Before those 90 days are over, you will want to renew them. To do so, you
could add the following to your `App\Console\Kernel`:
```php
Expand Down
31 changes: 31 additions & 0 deletions src/Events/ChallengeAuthorizationFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Events;

use Daanra\LaravelLetsEncrypt\Interfaces\LetsEncryptCertificateFailed;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ChallengeAuthorizationFailed implements LetsEncryptCertificateFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var \Throwable */
protected $exception;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\Throwable $exception)
{
$this->exception = $exception;
}

public function getException(): \Throwable
{
return $this->exception;
}
}
31 changes: 31 additions & 0 deletions src/Events/CleanUpChallengeFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Events;

use Daanra\LaravelLetsEncrypt\Interfaces\LetsEncryptCertificateFailed;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class CleanUpChallengeFailed implements LetsEncryptCertificateFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var \Throwable */
protected $exception;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\Throwable $exception)
{
$this->exception = $exception;
}

public function getException(): \Throwable
{
return $this->exception;
}
}
31 changes: 31 additions & 0 deletions src/Events/RegisterAccountFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Events;

use Daanra\LaravelLetsEncrypt\Interfaces\LetsEncryptCertificateFailed;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class RegisterAccountFailed implements LetsEncryptCertificateFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var \Throwable */
protected $exception;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\Throwable $exception)
{
$this->exception = $exception;
}

public function getException(): \Throwable
{
return $this->exception;
}
}
31 changes: 31 additions & 0 deletions src/Events/RenewExpiringCertificatesFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Events;

use Daanra\LaravelLetsEncrypt\Interfaces\LetsEncryptCertificateFailed;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class RenewExpiringCertificatesFailed implements LetsEncryptCertificateFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var \Throwable */
protected $exception;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\Throwable $exception)
{
$this->exception = $exception;
}

public function getException(): \Throwable
{
return $this->exception;
}
}
42 changes: 42 additions & 0 deletions src/Events/RequestAuthorizationFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Events;

use Daanra\LaravelLetsEncrypt\Interfaces\LetsEncryptCertificateFailed;
use Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class RequestAuthorizationFailed implements LetsEncryptCertificateFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var \Throwable */
protected $exception;

/** @var LetsEncryptCertificate */
protected $certificate;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\Throwable $exception, LetsEncryptCertificate $certificate)
{
$this->exception = $exception;
$this->certificate = $certificate;
}

public function getException(): \Throwable
{
return $this->exception;
}


public function getCertificate(): LetsEncryptCertificate
{
return $this->certificate;
}
}
41 changes: 41 additions & 0 deletions src/Events/RequestCertificateFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Events;

use Daanra\LaravelLetsEncrypt\Interfaces\LetsEncryptCertificateFailed;
use Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class RequestCertificateFailed implements LetsEncryptCertificateFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var \Throwable */
protected $exception;

/** @var LetsEncryptCertificate */
protected $certificate;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\Throwable $exception, LetsEncryptCertificate $certificate)
{
$this->exception = $exception;
$this->certificate = $certificate;
}

public function getException(): \Throwable
{
return $this->exception;
}

public function getCertificate(): LetsEncryptCertificate
{
return $this->certificate;
}
}
41 changes: 41 additions & 0 deletions src/Events/StoreCertificateFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Events;

use Daanra\LaravelLetsEncrypt\Interfaces\LetsEncryptCertificateFailed;
use Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class StoreCertificateFailed implements LetsEncryptCertificateFailed
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/** @var \Throwable */
protected $exception;

/** @var LetsEncryptCertificate */
protected $certificate;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(\Throwable $exception, LetsEncryptCertificate $certificate)
{
$this->exception = $exception;
$this->certificate = $certificate;
}

public function getException(): \Throwable
{
return $this->exception;
}

public function getCertificate(): LetsEncryptCertificate
{
return $this->certificate;
}
}
2 changes: 2 additions & 0 deletions src/Facades/LetsEncrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Daanra\LaravelLetsEncrypt\Facades;

use Daanra\LaravelLetsEncrypt\Models\LetsEncryptCertificate;
use Daanra\LaravelLetsEncrypt\PendingCertificate;
use Illuminate\Foundation\Bus\PendingDispatch;
use Illuminate\Support\Facades\Facade;

Expand All @@ -12,6 +13,7 @@
* @method static LetsEncryptCertificate createNow(string $domain)
* @method static LetsEncryptCertificate renewNow(string|LetsEncryptCertificate $domain)
* @method static PendingDispatch renew(string|LetsEncryptCertificate $domain, array $chain = [])
* @method static PendingCertificate certificate(string $domain)
*/
class LetsEncrypt extends Facade
{
Expand Down
8 changes: 8 additions & 0 deletions src/Interfaces/LetsEncryptCertificateFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Daanra\LaravelLetsEncrypt\Interfaces;

interface LetsEncryptCertificateFailed
{
public function getException(): \Throwable;
}
26 changes: 22 additions & 4 deletions src/Jobs/ChallengeAuthorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
namespace Daanra\LaravelLetsEncrypt\Jobs;

use AcmePhp\Core\Protocol\AuthorizationChallenge;
use Daanra\LaravelLetsEncrypt\Events\ChallengeAuthorizationFailed;
use Daanra\LaravelLetsEncrypt\Facades\LetsEncrypt;
use Daanra\LaravelLetsEncrypt\Traits\Retryable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -12,14 +14,20 @@

class ChallengeAuthorization implements ShouldQueue
{
use Dispatchable, Queueable, InteractsWithQueue, SerializesModels;
use Dispatchable, Queueable, InteractsWithQueue, SerializesModels, Retryable;

/** @var AuthorizationChallenge */
/**
* @var AuthorizationChallenge
*/
protected $challenge;

public function __construct(AuthorizationChallenge $httpChallenge)

public function __construct(AuthorizationChallenge $httpChallenge, int $tries = null, int $retryAfter = null, array $retryList = [])
{
$this->challenge = $httpChallenge;
$this->tries = $tries;
$this->retryAfter = $retryAfter;
$this->retryList = $retryList;
}

/**
Expand All @@ -32,6 +40,16 @@ public function handle()
{
$client = LetsEncrypt::createClient();
$client->challengeAuthorization($this->challenge);
CleanUpChallenge::dispatch($this->challenge);
CleanUpChallenge::dispatch($this->challenge, $this->tries, $this->retryAfter, $this->retryList);
}

/**
* Handle a job failure.
*
* @return void
*/
public function failed(\Throwable $exception)
{
event(new ChallengeAuthorizationFailed($exception));
}
}
Loading

0 comments on commit e8f58d5

Please sign in to comment.