CaiWuXiTong/app/Services/HxbOtpRelayService.php

175 lines
5.8 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
class HxbOtpRelayService
{
public const STATUS_PENDING = 'pending';
public const STATUS_READY = 'ready';
public const STATUS_CONSUMED = 'consumed';
public const STATUS_EXPIRED = 'expired';
private const TABLE = 'finance_verification_requests';
private function connection(): ConnectionInterface
{
return DB::connection();
}
/**
* @return array{run_id:string,fill_url:string,poll_token:string,expires_at:string}
*/
public function create(string $clientRunId): array
{
$clientRunId = trim($clientRunId);
if (! preg_match('/^[A-Za-z0-9-]{8,64}$/', $clientRunId)) {
throw new \InvalidArgumentException('任务编号格式不正确。');
}
$fillToken = bin2hex(random_bytes(32));
$pollToken = bin2hex(random_bytes(32));
$expiresAt = now()->addSeconds(max(
60,
(int) config('hxb_otp.request_ttl_seconds', 300),
));
$this->connection()->table(self::TABLE)->insert([
'client_run_id' => $clientRunId,
'fill_token_hash' => hash('sha256', $fillToken),
'poll_token_hash' => hash('sha256', $pollToken),
'status' => self::STATUS_PENDING,
'expires_at' => $expiresAt,
'created_at' => now(),
'updated_at' => now(),
]);
return [
'run_id' => $clientRunId,
'fill_url' => route('hxb-otp.form', ['token' => $fillToken]),
'poll_token' => $pollToken,
'expires_at' => $expiresAt->toIso8601String(),
];
}
public function findByFillToken(string $token): ?object
{
if (! preg_match('/^[a-f0-9]{64}$/', $token)) {
return null;
}
$row = $this->connection()->table(self::TABLE)
->where('fill_token_hash', hash('sha256', $token))
->first();
return $row === null ? null : $this->expireIfNeeded($row);
}
public function submit(string $token, string $code, ?string $ip): ?object
{
if (! preg_match('/^[a-f0-9]{64}$/', $token) || ! preg_match('/^\d{6}$/', $code)) {
return null;
}
return $this->connection()->transaction(function () use ($token, $code, $ip): ?object {
$row = $this->connection()->table(self::TABLE)
->where('fill_token_hash', hash('sha256', $token))
->lockForUpdate()
->first();
if ($row === null) {
return null;
}
$row = $this->expireIfNeeded($row);
if ((string) $row->status !== self::STATUS_PENDING) {
return $row;
}
$this->connection()->table(self::TABLE)
->where('id', (int) $row->id)
->update([
'status' => self::STATUS_READY,
'code_encrypted' => Crypt::encryptString($code),
'filled_at' => now(),
'filled_ip' => $ip !== null ? mb_substr($ip, 0, 45) : null,
'updated_at' => now(),
]);
return $this->connection()->table(self::TABLE)->where('id', (int) $row->id)->first();
});
}
/**
* @return array{status:string,code?:string}
*/
public function consume(string $clientRunId, string $pollToken): array
{
if (! preg_match('/^[A-Za-z0-9-]{8,64}$/', $clientRunId)
|| ! preg_match('/^[a-f0-9]{64}$/', $pollToken)) {
return ['status' => 'not_found'];
}
return $this->connection()->transaction(function () use ($clientRunId, $pollToken): array {
$row = $this->connection()->table(self::TABLE)
->where('client_run_id', $clientRunId)
->lockForUpdate()
->first();
if ($row === null || ! hash_equals((string) $row->poll_token_hash, hash('sha256', $pollToken))) {
return ['status' => 'not_found'];
}
$row = $this->expireIfNeeded($row);
$status = (string) $row->status;
if ($status === self::STATUS_PENDING) {
return ['status' => self::STATUS_PENDING];
}
if ($status !== self::STATUS_READY || $row->code_encrypted === null) {
return ['status' => $status];
}
try {
$code = Crypt::decryptString((string) $row->code_encrypted);
} catch (DecryptException) {
return ['status' => self::STATUS_EXPIRED];
}
if (! preg_match('/^\d{6}$/', $code)) {
return ['status' => self::STATUS_EXPIRED];
}
$this->connection()->table(self::TABLE)
->where('id', (int) $row->id)
->update([
'status' => self::STATUS_CONSUMED,
'code_encrypted' => null,
'consumed_at' => now(),
'updated_at' => now(),
]);
return ['status' => self::STATUS_CONSUMED, 'code' => $code];
});
}
private function expireIfNeeded(object $row): object
{
if ((string) $row->status === self::STATUS_PENDING
&& now()->greaterThanOrEqualTo($row->expires_at)) {
$this->connection()->table(self::TABLE)
->where('id', (int) $row->id)
->where('status', self::STATUS_PENDING)
->update([
'status' => self::STATUS_EXPIRED,
'updated_at' => now(),
]);
$row->status = self::STATUS_EXPIRED;
}
return $row;
}
}