osb/app/Models/Service/SSL.php

106 lines
1.9 KiB
PHP
Raw Normal View History

2018-05-20 12:53:14 +00:00
<?php
namespace App\Models\Service;
2018-05-20 12:53:14 +00:00
2020-04-01 12:35:06 +00:00
use Carbon\Carbon;
use Illuminate\Support\Arr;
use Leenooks\Carbon as LeenooksCarbon;
2022-04-19 07:07:39 +00:00
/**
* Class SSL (Service)
* Services that are provide an SSL Certificate
*/
class SSL extends Type
2018-05-20 12:53:14 +00:00
{
2022-04-19 07:07:39 +00:00
protected $table = 'service_ssl';
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 METHODS */
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');
elseif ($this->csr) {
2020-04-01 12:35:06 +00:00
$dn = '';
$dna = openssl_csr_get_subject($this->csr);
dump(['dna'=>$dna,'csr'=>$this->csr,'id',$this->id]);
2020-04-01 12:35:06 +00:00
foreach ($dna as $k=>$v) {
if ($dn)
$dn .= ',';
$dn .= sprintf('%s=%s',$k,$v);
}
return $dn;
} else {
return '[NO DN]';
2020-04-01 12:35:06 +00:00
}
2020-02-20 11:54:28 +00:00
}
2022-04-19 07:07:39 +00:00
/**
* Return the Certificate Expiry Date
*/
public function getServiceExpireAttribute(): ?LeenooksCarbon
{
return $this->cert ? LeenooksCarbon::createFromTimestamp($this->crt_parse->get('validTo_time_t')) : NULL;
}
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')
: 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
}