171 lines
5.3 KiB
PHP
171 lines
5.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Verifikator\VerifikasiAbsenToken;
|
|
use Illuminate\Contracts\Encryption\DecryptException;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Crypt;
|
|
use Illuminate\Support\Str;
|
|
|
|
class VerifikasiAbsenLinkService
|
|
{
|
|
const TTL_MENIT_DEFAULT = 7200;
|
|
|
|
// TODO: sementara 1 menit untuk testing, kembalikan ke 1440 (1 hari).
|
|
const SESI_MENIT = 1;
|
|
|
|
const PURPOSE = 'verifikasi-presensi';
|
|
|
|
public function generate(int $pengajuanId, string $nip, int $ttlMenit = self::TTL_MENIT_DEFAULT): array
|
|
{
|
|
$tokenPlaintext = $this->buatToken($pengajuanId, $nip, now()->addMinutes($ttlMenit));
|
|
|
|
return [
|
|
'nip' => $nip,
|
|
'url' => $this->url($pengajuanId, $tokenPlaintext),
|
|
'button_param' => "{$pengajuanId}?token={$tokenPlaintext}",
|
|
];
|
|
}
|
|
|
|
public function url(int $pengajuanId, ?string $token = null): string
|
|
{
|
|
$base = rtrim(config('services.verifikasi_absen_wa.url'), '/')."/{$pengajuanId}";
|
|
|
|
if (! $token) {
|
|
return $base;
|
|
}
|
|
|
|
return $base.'?'.http_build_query(['token' => $token]);
|
|
}
|
|
|
|
/**
|
|
* @return array{ok: bool, nip: ?string, reason: ?string}
|
|
*/
|
|
public function verify(int $pengajuanId, ?string $token): array
|
|
{
|
|
$gagal = fn (string $reason) => ['ok' => false, 'nip' => null, 'reason' => $reason];
|
|
|
|
if (! $token) {
|
|
return $gagal('invalid');
|
|
}
|
|
|
|
$row = VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId)
|
|
->where('token', $this->hash($token))
|
|
->where('purpose', self::PURPOSE)
|
|
->first();
|
|
|
|
if (! $row) {
|
|
return $gagal('invalid');
|
|
}
|
|
|
|
if (now()->greaterThan($row->expires_at)) {
|
|
return $gagal('link_expired');
|
|
}
|
|
|
|
if ($row->first_accessed_at === null) {
|
|
$row->update(['first_accessed_at' => now()]);
|
|
} elseif (now()->greaterThan($row->first_accessed_at->copy()->addMinutes(self::SESI_MENIT))) {
|
|
return $gagal('session_expired');
|
|
}
|
|
|
|
return ['ok' => true, 'nip' => $row->nip, 'reason' => null];
|
|
}
|
|
|
|
/**
|
|
* Sisa detik sesi untuk token ini (0 kalau sudah habis), null kalau token tidak dikenal atau belum pernah dibuka.
|
|
*/
|
|
public function sisaSesiDetik(int $pengajuanId, ?string $token): ?int
|
|
{
|
|
if (! $token) {
|
|
return null;
|
|
}
|
|
|
|
$row = VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId)
|
|
->where('token', $this->hash($token))
|
|
->where('purpose', self::PURPOSE)
|
|
->first();
|
|
|
|
if (! $row || $row->first_accessed_at === null) {
|
|
return null;
|
|
}
|
|
|
|
$berakhir = $row->first_accessed_at->copy()->addMinutes(self::SESI_MENIT);
|
|
|
|
return max(0, (int) now()->diffInSeconds($berakhir, false));
|
|
}
|
|
|
|
/**
|
|
* Kalau token lama sudah lewat sesi (tapi tautannya belum kedaluwarsa), buat token baru
|
|
* untuk atasan & pengajuan yang sama. Masa berlaku tautan tidak diperpanjang.
|
|
*
|
|
* @return ?string Token plaintext baru, atau null kalau tidak perlu/tidak bisa diperbarui.
|
|
*/
|
|
public function perbaruiSesiJikaHabis(int $pengajuanId, ?string $token): ?string
|
|
{
|
|
if (! $token) {
|
|
return null;
|
|
}
|
|
|
|
$row = VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId)
|
|
->where('token', $this->hash($token))
|
|
->where('purpose', self::PURPOSE)
|
|
->first();
|
|
|
|
if (! $row || now()->greaterThan($row->expires_at) || $row->first_accessed_at === null) {
|
|
return null;
|
|
}
|
|
|
|
if (! now()->greaterThan($row->first_accessed_at->copy()->addMinutes(self::SESI_MENIT))) {
|
|
return null;
|
|
}
|
|
|
|
// Token pengganti yang sesinya masih jalan dipakai ulang, jangan buat baru tiap tombol WA diklik.
|
|
// Token di DB hanya berupa hash, jadi plaintext pengganti disimpan (terenkripsi) di cache selama sesinya.
|
|
$kunciCache = "verifikasi-absen:pengganti:{$row->id}";
|
|
|
|
if ($tersimpan = Cache::get($kunciCache)) {
|
|
try {
|
|
return Crypt::decryptString($tersimpan);
|
|
} catch (DecryptException) {
|
|
Cache::forget($kunciCache);
|
|
}
|
|
}
|
|
|
|
$tokenBaru = $this->buatToken($pengajuanId, $row->nip, $row->expires_at);
|
|
|
|
Cache::put($kunciCache, Crypt::encryptString($tokenBaru), now()->addMinutes(self::SESI_MENIT));
|
|
|
|
return $tokenBaru;
|
|
}
|
|
|
|
public function markUsed(int $pengajuanId, string $token): void
|
|
{
|
|
VerifikasiAbsenToken::where('pengajuan_id', $pengajuanId)
|
|
->where('token', $this->hash($token))
|
|
->where('purpose', self::PURPOSE)
|
|
->update(['used_at' => now()]);
|
|
}
|
|
|
|
private function buatToken(int $pengajuanId, string $nip, Carbon $expiresAt): string
|
|
{
|
|
$tokenPlaintext = Str::random(40);
|
|
|
|
VerifikasiAbsenToken::create([
|
|
'pengajuan_id' => $pengajuanId,
|
|
'nip' => $nip,
|
|
'token' => $this->hash($tokenPlaintext),
|
|
'purpose' => self::PURPOSE,
|
|
'expires_at' => $expiresAt,
|
|
]);
|
|
|
|
return $tokenPlaintext;
|
|
}
|
|
|
|
private function hash(string $token): string
|
|
{
|
|
return hash('sha256', $token);
|
|
}
|
|
}
|