Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(api-tokens): persist expiration warning state

Andras Bacsai3911a0305c01
diff --git a/app/Jobs/ApiTokenExpirationWarningJob.php b/app/Jobs/ApiTokenExpirationWarningJob.php
index a8f388c852..810919c6ac 100644
--- a/app/Jobs/ApiTokenExpirationWarningJob.php
+++ b/app/Jobs/ApiTokenExpirationWarningJob.php
@@ -12,7 +12,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
-use Illuminate\Support\Facades\RateLimiter;
use Laravel\Horizon\Contracts\Silenced;
class ApiTokenExpirationWarningJob implements ShouldBeEncrypted, ShouldQueue, Silenced
@@ -29,20 +28,34 @@ class ApiTokenExpirationWarningJob implements ShouldBeEncrypted, ShouldQueue, Si
->whereNotNull('expires_at')
->where('expires_at', '>', now())
->where('expires_at', '<=', now()->addDay())
+ ->whereNull('api_token_expiration_warning_sent_at')
->where('tokenable_type', User::class)
->chunkById(100, function ($tokens) {
foreach ($tokens as $token) {
if (! $token->team_id) {
continue;
}
- RateLimiter::attempt(
- 'api-token-expiring:'.$token->id,
- $maxAttempts = 0,
- function () use ($token) {
- Team::find($token->team_id)?->notify(new ApiTokenExpiringNotification($token));
- },
- $decaySeconds = 7 * 24 * 3600,
- );
+
+ $team = Team::find($token->team_id);
+ if (! $team) {
+ continue;
+ }
+
+ $warningSentAt = now();
+ $markedAsSent = PersonalAccessToken::query()
+ ->whereKey($token->getKey())
+ ->whereNotNull('expires_at')
+ ->where('expires_at', '>', now())
+ ->where('expires_at', '<=', now()->addDay())
+ ->whereNull('api_token_expiration_warning_sent_at')
+ ->update(['api_token_expiration_warning_sent_at' => $warningSentAt]);
+
+ if ($markedAsSent !== 1) {
+ continue;
+ }
+
+ $token->forceFill(['api_token_expiration_warning_sent_at' => $warningSentAt]);
+ $team->notify(new ApiTokenExpiringNotification($token));
}
});
}
diff --git a/app/Models/PersonalAccessToken.php b/app/Models/PersonalAccessToken.php
index 398046a7cd..503377bec1 100644
--- a/app/Models/PersonalAccessToken.php
+++ b/app/Models/PersonalAccessToken.php
@@ -11,6 +11,14 @@ class PersonalAccessToken extends SanctumPersonalAccessToken
'token',
'abilities',
'expires_at',
+ 'api_token_expiration_warning_sent_at',
'team_id',
];
+
+ protected function casts(): array
+ {
+ return [
+ 'api_token_expiration_warning_sent_at' => 'datetime',
+ ];
+ }
}
diff --git a/database/migrations/2026_05_13_000000_add_expiration_warning_sent_at_to_personal_access_tokens_table.php b/database/migrations/2026_05_13_000000_add_expiration_warning_sent_at_to_personal_access_tokens_table.php
new file mode 100644
index 0000000000..7281154820
--- /dev/null
+++ b/database/migrations/2026_05_13_000000_add_expiration_warning_sent_at_to_personal_access_tokens_table.php
@@ -0,0 +1,30 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+ /**
+ * Run the migrations.
+ */
+ public function up(): void
+ {
+ Schema::table('personal_access_tokens', function (Blueprint $table) {
+ $table->timestamp('api_token_expiration_warning_sent_at')->nullable()->after('expires_at');
+ $table->index(['expires_at', 'api_token_expiration_warning_sent_at'], 'personal_access_tokens_expiration_warning_index');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::table('personal_access_tokens', function (Blueprint $table) {
+ $table->dropIndex('personal_access_tokens_expiration_warning_index');
+ $table->dropColumn('api_token_expiration_warning_sent_at');
+ });
+ }
+};
diff --git a/tests/Feature/ApiTokenExpirationWarningTest.php b/tests/Feature/ApiTokenExpirationWarningTest.php
index 5255581dd7..67380f0473 100644
--- a/tests/Feature/ApiTokenExpirationWarningTest.php
+++ b/tests/Feature/ApiTokenExpirationWarningTest.php
@@ -29,11 +29,12 @@ beforeEach(function () {
Notification::fake();
});
-function createTokenExpiring(User $user, Team $team, ?Carbon $expiresAt): PersonalAccessToken
+function createTokenExpiring(User $user, Team $team, ?Carbon $expiresAt, ?Carbon $warningSentAt = null): PersonalAccessToken
{
$plain = $user->createToken('t-'.uniqid(), ['read'], $expiresAt);
$token = $plain->accessToken;
$token->team_id = $team->id;
+ $token->api_token_expiration_warning_sent_at = $warningSentAt;
$token->save();
return $token->fresh();
@@ -41,22 +42,52 @@ function createTokenExpiring(User $user, Team $team, ?Carbon $expiresAt): Person
describe('ApiTokenExpirationWarningJob', function () {
test('notifies team when token expires within 24h', function () {
- createTokenExpiring($this->user, $this->team, now()->addHours(23));
+ $token = createTokenExpiring($this->user, $this->team, now()->addHours(23));
(new ApiTokenExpirationWarningJob)->handle();
Notification::assertSentTo($this->team, ApiTokenExpiringNotification::class);
+ expect($token->fresh()->api_token_expiration_warning_sent_at)->not->toBeNull();
+ });
+
+ test('database marker prevents duplicate warnings on repeat runs', function () {
+ createTokenExpiring($this->user, $this->team, now()->addHours(12));
+
+ (new ApiTokenExpirationWarningJob)->handle();
+ (new ApiTokenExpirationWarningJob)->handle();
+
+ Notification::assertSentToTimes($this->team, ApiTokenExpiringNotification::class, 1);
});
- test('rate limiter prevents duplicate warnings on repeat runs', function () {
+ test('database marker prevents duplicate warnings after cache is flushed', function () {
createTokenExpiring($this->user, $this->team, now()->addHours(12));
(new ApiTokenExpirationWarningJob)->handle();
+
+ Cache::flush();
+
(new ApiTokenExpirationWarningJob)->handle();
Notification::assertSentToTimes($this->team, ApiTokenExpiringNotification::class, 1);
});
+ test('skips tokens that already have an expiration warning marker', function () {
+ createTokenExpiring($this->user, $this->team, now()->addHours(12), now()->subHour());
+
+ (new ApiTokenExpirationWarningJob)->handle();
+
+ Notification::assertNothingSent();
+ });
+
+ test('notifies once for each unmarked expiring token', function () {
+ createTokenExpiring($this->user, $this->team, now()->addHours(12));
+ createTokenExpiring($this->user, $this->team, now()->addHours(23));
+
+ (new ApiTokenExpirationWarningJob)->handle();
+
+ Notification::assertSentToTimes($this->team, ApiTokenExpiringNotification::class, 2);
+ });
+
test('skips tokens expiring more than 24h out', function () {
createTokenExpiring($this->user, $this->team, now()->addDays(3));