Skip to content

Commit f605856

Browse files
✨feat: Certificate environment validation
1 parent 78cd614 commit f605856

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/Efi/Request.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ private function validateCertificate(string $fileContents, string $certPath): vo
110110
$this->throwEfiException('Certificado inválido ou inativo', 403);
111111
}
112112

113-
$this->checkCertificateExpiration($publicKey);
113+
$this->checkCertificateEnviroment($publicKey['issuer']['CN']);
114+
$this->checkCertificateExpiration($publicKey['validTo_time_t']);
114115
}
115116

116117
/**
@@ -129,17 +130,31 @@ private function readP12Certificate(string $fileContents): array
129130
return $certData;
130131
}
131132

133+
/**
134+
* Checks if the certificate is valid to environment chosen.
135+
*
136+
* @param string $issuerCn The certificate issuer.
137+
* @throws EfiException If the certificate is not valid to environment chosed.
138+
*/
139+
private function checkCertificateEnviroment(string $issuerCn): void
140+
{
141+
if ($this->config['sandbox'] === true && ($issuerCn === 'apis.sejaefi.com.br' || $issuerCn === 'apis.efipay.com.br' || $issuerCn === 'api-pix.gerencianet.com.br')) {
142+
$this->throwEfiException('Certificado de produção inválido para o ambiente escolhido [homologação].', 403);
143+
} elseif (!$this->config['sandbox'] && ($issuerCn === 'apis-h.sejaefi.com.br' || $issuerCn === 'apis-h.efipay.com.br' || $issuerCn === 'api-pix-h.gerencianet.com.br')) {
144+
$this->throwEfiException('Certificado de homologação inválido para o ambiente escolhido [produção].', 403);
145+
}
146+
}
147+
132148
/**
133149
* Checks if the certificate has expired.
134150
*
135-
* @param array $publicKey The parsed public key data from the certificate.
151+
* @param string $validToTime Certificate validity data.
136152
* @throws EfiException If the certificate has expired.
137153
*/
138-
139-
private function checkCertificateExpiration(array $publicKey): void
154+
private function checkCertificateExpiration(string $validToTime): void
140155
{
141156
$today = date("Y-m-d H:i:s");
142-
$validTo = date('Y-m-d H:i:s', $publicKey['validTo_time_t']);
157+
$validTo = date('Y-m-d H:i:s', $validToTime);
143158
if ($validTo <= $today) {
144159
$this->throwEfiException('O certificado de autenticação expirou em ' . $validTo, 403);
145160
}

0 commit comments

Comments
 (0)