expireStale(); $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 (in_array((string) $row->status, [self::STATUS_PENDING, self::STATUS_READY], true) && now()->greaterThanOrEqualTo($row->expires_at)) { $this->connection()->table(self::TABLE) ->where('id', (int) $row->id) ->whereIn('status', [self::STATUS_PENDING, self::STATUS_READY]) ->update([ 'status' => self::STATUS_EXPIRED, 'code_encrypted' => null, 'updated_at' => now(), ]); $row->status = self::STATUS_EXPIRED; } return $row; } private function expireStale(): void { $this->connection()->table(self::TABLE) ->whereIn('status', [self::STATUS_PENDING, self::STATUS_READY]) ->where('expires_at', '<=', now()) ->update([ 'status' => self::STATUS_EXPIRED, 'code_encrypted' => null, 'updated_at' => now(), ]); } }