first commit

This commit is contained in:
Claudecio Martins
2026-06-16 10:04:10 -03:00
commit a951944997
4463 changed files with 419677 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace WorkbloomERP\Utils;
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use DateTimeImmutable;
class JwtUtil {
private int $ttl;
private string $issuer;
private string $secretKey;
private string $algorithm;
public function __construct(string $secretKey, string $algorithm = 'HS256', int $ttl = 3600, string $issuer = 'workbloomerp') {
if (empty($secretKey)) {
throw new Exception(message: 'JWT secret não configurado.');
}
$this->ttl = $ttl;
$this->secretKey = $secretKey;
$this->algorithm = $algorithm;
$this->issuer = $issuer;
}
public function generate(array $payload): string {
$defaultClaims = [];
$defaultClaims['iat'] = $payload['iat'] ?? (new DateTimeImmutable())->getTimestamp();
$defaultClaims['exp'] = $payload['exp'] ?? ((new DateTimeImmutable())->modify("+{$this->ttl} seconds")->getTimestamp());
$finalPayload = array_merge($payload, $defaultClaims);
return JWT::encode(
payload: $finalPayload,
key: $this->secretKey,
alg: $this->algorithm
);
}
public function validate(string $token): object {
return JWT::decode(
jwt: $token,
keyOrKeyArray: new Key(
keyMaterial: $this->secretKey,
algorithm: $this->algorithm
)
);
}
}