49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
class VerifikasiAbsenLinkService
|
|
{
|
|
const TTL_MENIT_DEFAULT = 7200; // 5 hari, selaras dengan batas kadaluarsa pengajuan
|
|
|
|
public function generate(int $pengajuanId, string $nip, int $ttlMenit = self::TTL_MENIT_DEFAULT): array
|
|
{
|
|
$expires = now()->addMinutes($ttlMenit)->timestamp;
|
|
$signature = $this->hash($pengajuanId, $nip, $expires);
|
|
|
|
return [
|
|
'nip' => $nip,
|
|
'expires' => $expires,
|
|
'signature' => $signature,
|
|
'url' => route('verifikasi-absen.show', [
|
|
'id' => $pengajuanId,
|
|
'nip' => $nip,
|
|
'expires' => $expires,
|
|
'signature' => $signature,
|
|
]),
|
|
// Suffix untuk parameter tombol URL template WhatsApp (domain sudah baku di template).
|
|
'button_param' => "{$pengajuanId}?nip={$nip}&expires={$expires}&signature={$signature}",
|
|
];
|
|
}
|
|
|
|
public function verify(int $pengajuanId, ?string $nip, mixed $expires, ?string $signature): bool
|
|
{
|
|
if (! $nip || ! $expires || ! $signature) {
|
|
return false;
|
|
}
|
|
|
|
if ((int) $expires < now()->timestamp) {
|
|
return false;
|
|
}
|
|
|
|
$expected = $this->hash($pengajuanId, $nip, (int) $expires);
|
|
|
|
return hash_equals($expected, $signature);
|
|
}
|
|
|
|
private function hash(int $pengajuanId, string $nip, int $expires): string
|
|
{
|
|
return hash_hmac('sha256', "{$pengajuanId}|{$nip}|{$expires}", config('app.key'));
|
|
}
|
|
}
|