Skip to content

Commit 079d515

Browse files
Add validity information for certificates endpoints (#834)
* show all certs validity * show cert validity info in cert endpoint * show validity inside cert endpoint * prettier * return -1 if expired
1 parent 597d0e5 commit 079d515

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

pfSense-pkg-RESTAPI/files/usr/local/pkg/RESTAPI/Models/Certificate.inc

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace RESTAPI\Models;
55
use RESTAPI\Core\Model;
66
use RESTAPI\Fields\Base64Field;
77
use RESTAPI\Fields\StringField;
8+
use RESTAPI\Fields\DateTimeField;
9+
use RESTAPI\Fields\IntegerField;
810
use RESTAPI\Fields\UIDField;
911
use RESTAPI\Responses\ForbiddenError;
1012
use RESTAPI\Responses\ValidationError;
@@ -22,6 +24,9 @@ class Certificate extends Model {
2224
public Base64Field $csr;
2325
public Base64Field $crt;
2426
public Base64Field $prv;
27+
public DateTimeField $valid_from;
28+
public DateTimeField $valid_until;
29+
public IntegerField $valid_days_left;
2530

2631
public function __construct(mixed $id = null, mixed $parent_id = null, mixed $data = [], mixed ...$options) {
2732
# Set model attributes
@@ -69,10 +74,56 @@ class Certificate extends Model {
6974
validators: [new X509Validator(allow_prv: true, allow_ecprv: true, allow_rsa: true)],
7075
help_text: 'The X509 private key string.',
7176
);
77+
$this->valid_from = new DateTimeField(
78+
read_only: true,
79+
representation_only: true,
80+
help_text: 'The start date from which this certificate is valid.',
81+
);
82+
$this->valid_until = new DateTimeField(
83+
read_only: true,
84+
representation_only: true,
85+
help_text: 'The date until which this certificate is valid.',
86+
);
87+
$this->valid_days_left = new IntegerField(
88+
read_only: true,
89+
minimum: 1,
90+
maximum: 12000,
91+
representation_only: true,
92+
help_text: 'The number of days remaining until this certificate expires.',
93+
);
7294

7395
parent::__construct($id, $parent_id, $data, ...$options);
7496
}
7597

98+
/**
99+
* Gets the start date of this certificate.
100+
* @returns string The start date in 'Y-m-d H:i:s' format.
101+
*/
102+
protected function from_internal_valid_from(): string {
103+
return cert_get_dates($this->crt->value, false, false)[0]->format('Y-m-d H:i:s');
104+
}
105+
106+
/**
107+
* Gets the expiration date of this certificate.
108+
* @returns string The expiration date in 'Y-m-d H:i:s' format.
109+
*/
110+
protected function from_internal_valid_until(): string {
111+
return cert_get_dates($this->crt->value, false, false)[1]->format('Y-m-d H:i:s');
112+
}
113+
114+
/**
115+
* Calculates the number of days left until this certificate expires.
116+
* @returns int The number of entire days left until expiration. -1 if already expired.
117+
*/
118+
protected function from_internal_valid_days_left(): int {
119+
$enddate = cert_get_dates($this->crt->value, false, false)[1];
120+
if (!$enddate) {
121+
return false;
122+
}
123+
$interval = (new \DateTime())->diff($enddate);
124+
return $interval->invert ? -1 : $interval->days;
125+
}
126+
76127
/**
77128
* Adds extra validation to the `prv` field.
78129
* @param string $prv The incoming value to be validated.

0 commit comments

Comments
 (0)