diff --git a/app/Services/HxbOtpRelayService.php b/app/Services/HxbOtpRelayService.php index e44e550ff..ee591ace2 100644 --- a/app/Services/HxbOtpRelayService.php +++ b/app/Services/HxbOtpRelayService.php @@ -29,6 +29,8 @@ class HxbOtpRelayService */ public function create(string $clientRunId): array { + $this->expireStale(); + $clientRunId = trim($clientRunId); if (! preg_match('/^[A-Za-z0-9-]{8,64}$/', $clientRunId)) { throw new \InvalidArgumentException('任务编号格式不正确。'); @@ -157,13 +159,14 @@ class HxbOtpRelayService private function expireIfNeeded(object $row): object { - if ((string) $row->status === self::STATUS_PENDING + 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) - ->where('status', self::STATUS_PENDING) + ->whereIn('status', [self::STATUS_PENDING, self::STATUS_READY]) ->update([ 'status' => self::STATUS_EXPIRED, + 'code_encrypted' => null, 'updated_at' => now(), ]); $row->status = self::STATUS_EXPIRED; @@ -171,4 +174,16 @@ class HxbOtpRelayService 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(), + ]); + } } diff --git a/tests/Feature/HxbOtpRelayTest.php b/tests/Feature/HxbOtpRelayTest.php index 953862207..b95b61ecf 100644 --- a/tests/Feature/HxbOtpRelayTest.php +++ b/tests/Feature/HxbOtpRelayTest.php @@ -88,6 +88,30 @@ class HxbOtpRelayTest extends FeatureTestCase ->assertSee('验证码链接已过期'); } + public function test_submitted_code_is_cleared_after_link_expires(): void + { + Carbon::setTestNow('2026-12-02 09:00:00'); + $created = $this->postSignedJson('/api/hxb-verification/requests', [ + 'client_run_id' => (string) Str::uuid(), + ])->assertCreated()->json(); + $fillToken = basename((string) parse_url($created['fill_url'], PHP_URL_PATH)); + $this->post('/hxb-verification/' . $fillToken, ['code' => '112233']) + ->assertRedirect(); + + Carbon::setTestNow('2026-12-02 09:06:00'); + $pollUrl = '/api/hxb-verification/requests/' . $created['run_id'] . '/code'; + $this->postJson($pollUrl, [], [ + 'Authorization' => 'Bearer ' . $created['poll_token'], + ])->assertStatus(410) + ->assertJsonPath('status', 'expired'); + + $this->assertDatabaseHas('finance_verification_requests', [ + 'client_run_id' => $created['run_id'], + 'status' => 'expired', + 'code_encrypted' => null, + ]); + } + private function postSignedJson(string $path, array $payload) { $body = json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);