2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
2019-06-07 06:54:27 +00:00
|
|
|
namespace App\Models\Service;
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
use Carbon\Carbon;
|
2022-04-22 01:47:46 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2022-04-22 04:41:18 +00:00
|
|
|
use Leenooks\Carbon as LeenooksCarbon;
|
2019-06-13 04:32:34 +00:00
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/**
|
|
|
|
* Class SSL (Service)
|
|
|
|
* Services that are provide an SSL Certificate
|
|
|
|
*/
|
2022-04-22 01:47:46 +00:00
|
|
|
class SSL extends Type
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2022-04-19 07:07:39 +00:00
|
|
|
protected $table = 'service_ssl';
|
2019-06-13 04:32:34 +00:00
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
protected $public_key = NULL;
|
2022-04-19 07:07:39 +00:00
|
|
|
protected $crt_parse = NULL;
|
|
|
|
|
|
|
|
/* STATIC */
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
public static function boot()
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2020-04-01 12:35:06 +00:00
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::retrieved(function($model) {
|
|
|
|
if ($model->cert);
|
|
|
|
$model->crt_parse = collect(openssl_x509_parse($model->cert));
|
2018-07-16 05:06:43 +00:00
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
if ($model->csr) {
|
|
|
|
$model->public_key = collect(openssl_pkey_get_details(openssl_csr_get_public_key($model->csr)));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/* INTERFACES */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the Cert DN
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-02-20 11:54:28 +00:00
|
|
|
public function getServiceDescriptionAttribute(): string
|
|
|
|
{
|
2020-04-01 12:35:06 +00:00
|
|
|
if ($this->cert)
|
|
|
|
return Arr::get($this->crt_parse,'name');
|
|
|
|
|
|
|
|
else {
|
|
|
|
$dn = '';
|
|
|
|
$dna = openssl_csr_get_subject($this->csr);
|
|
|
|
|
|
|
|
foreach ($dna as $k=>$v) {
|
|
|
|
if ($dn)
|
|
|
|
$dn .= ',';
|
|
|
|
|
|
|
|
$dn .= sprintf('%s=%s',$k,$v);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dn;
|
|
|
|
}
|
2020-02-20 11:54:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/**
|
|
|
|
* Return the Certificate Expiry Date
|
|
|
|
*/
|
2022-04-22 04:41:18 +00:00
|
|
|
public function getServiceExpireAttribute(): ?LeenooksCarbon
|
2021-07-13 02:31:56 +00:00
|
|
|
{
|
2022-04-22 04:41:18 +00:00
|
|
|
return $this->cert ? LeenooksCarbon::createFromTimestamp($this->crt_parse->get('validTo_time_t')) : NULL;
|
2021-07-13 02:31:56 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/**
|
|
|
|
* Return the Cert Name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-02-20 11:54:28 +00:00
|
|
|
public function getServiceNameAttribute(): string
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2020-04-01 12:35:06 +00:00
|
|
|
return $this->cert
|
|
|
|
? Arr::get($this->crt_parse,'subject.CN')
|
2021-12-24 01:14:01 +00:00
|
|
|
: Arr::get(openssl_csr_get_subject($this->csr),'CN','');
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
2020-02-20 11:54:28 +00:00
|
|
|
|
2022-04-19 07:07:39 +00:00
|
|
|
/**
|
|
|
|
* Certificates have no contract and can be cancelled anytime.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2020-02-20 11:54:28 +00:00
|
|
|
public function inContract(): bool
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2022-04-19 07:07:39 +00:00
|
|
|
|
|
|
|
/* METHODS */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Carbon|null
|
|
|
|
* @deprecated use getServiceExpireAttribute()
|
|
|
|
*/
|
|
|
|
public function getValidToAttribute()
|
|
|
|
{
|
|
|
|
abort(500,'use getServiceExpireAttribute');
|
|
|
|
}
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|