mirror of
https://github.com/tiennm99/coolify.git
synced 2026-10-15 16:14:35 +00:00
feat(security): support expiration on API tokens with warning notifications
Add optional expiration to personal API tokens. Users pick a duration (1/7/30/60/90 days or Never) at creation time. Expired tokens are rejected by Sanctum, pruned hourly by sanctum:prune-expired, and a team notification fires ~24h before expiry so owners can rotate before API calls start failing. - ApiTokens Livewire component stores expires_at from expiresInDays - Rework issued-tokens UI from card grid to table (matches other views) - New ApiTokenExpirationWarningJob scheduled hourly (idempotent via RateLimiter) - New ApiTokenExpiringNotification (email/discord/telegram/slack/pushover) - api_token_expiring added to alwaysSendEvents so users cannot silence expiry warnings from the per-event notification toggle UI - sanctum:prune-expired cadence moved from daily to hourly Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
1 parent
bff6d85370
commit
90ddbb3572
9 files changed
+419
-35
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Jobs\ApiTokenExpirationWarningJob;
|
||||
use App\Jobs\CheckForUpdatesJob;
|
||||
use App\Jobs\CheckHelperImageJob;
|
||||
use App\Jobs\CheckTraefikVersionJob;
|
||||
@@ -41,6 +42,8 @@ class Kernel extends ConsoleKernel
|
||||
|
||||
// $this->scheduleInstance->job(new CleanupStaleMultiplexedConnections)->hourly();
|
||||
$this->scheduleInstance->command('cleanup:redis --clear-locks')->daily();
|
||||
$this->scheduleInstance->command('sanctum:prune-expired --hours=1')->hourly()->onOneServer();
|
||||
$this->scheduleInstance->job(new ApiTokenExpirationWarningJob)->hourly()->onOneServer();
|
||||
|
||||
if (isDev()) {
|
||||
// Instance Jobs
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Models\PersonalAccessToken;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use App\Notifications\ApiTokenExpiringNotification;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
|
||||
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
|
||||
{
|
||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||
|
||||
public $tries = 1;
|
||||
|
||||
public $timeout = 120;
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
PersonalAccessToken::query()
|
||||
->whereNotNull('expires_at')
|
||||
->where('expires_at', '>', now())
|
||||
->where('expires_at', '<=', now()->addDay())
|
||||
->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,
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -13,10 +13,20 @@ class ApiTokens extends Component
|
||||
|
||||
public ?string $description = null;
|
||||
|
||||
public ?int $expiresInDays = 30;
|
||||
|
||||
public $tokens = [];
|
||||
|
||||
public array $permissions = ['read'];
|
||||
|
||||
public array $expirationOptions = [
|
||||
7 => '7 days',
|
||||
30 => '30 days',
|
||||
60 => '60 days',
|
||||
90 => '90 days',
|
||||
365 => '1 year',
|
||||
];
|
||||
|
||||
public $isApiEnabled;
|
||||
|
||||
public bool $canUseRootPermissions = false;
|
||||
@@ -90,8 +100,10 @@ class ApiTokens extends Component
|
||||
|
||||
$this->validate([
|
||||
'description' => 'required|min:3|max:255',
|
||||
'expiresInDays' => 'nullable|integer|in:7,30,60,90,365',
|
||||
]);
|
||||
$token = auth()->user()->createToken($this->description, array_values($this->permissions));
|
||||
$expiresAt = $this->expiresInDays ? now()->addDays($this->expiresInDays) : null;
|
||||
$token = auth()->user()->createToken($this->description, array_values($this->permissions), $expiresAt);
|
||||
$this->getTokens();
|
||||
session()->flash('token', $token->plainTextToken);
|
||||
} catch (\Exception $e) {
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Models\PersonalAccessToken;
|
||||
use App\Notifications\Dto\DiscordMessage;
|
||||
use App\Notifications\Dto\PushoverMessage;
|
||||
use App\Notifications\Dto\SlackMessage;
|
||||
use Illuminate\Notifications\Messages\MailMessage;
|
||||
|
||||
class ApiTokenExpiringNotification extends CustomEmailNotification
|
||||
{
|
||||
protected string $tokenName;
|
||||
|
||||
protected string $expiresAt;
|
||||
|
||||
protected string $manageUrl;
|
||||
|
||||
public function __construct(public PersonalAccessToken $token)
|
||||
{
|
||||
$this->onQueue('high');
|
||||
$this->tokenName = $token->name;
|
||||
$this->expiresAt = $token->expires_at?->format('Y-m-d H:i:s') ?? '';
|
||||
$this->manageUrl = route('security.api-tokens');
|
||||
}
|
||||
|
||||
public function via(object $notifiable): array
|
||||
{
|
||||
return $notifiable->getEnabledChannels('api_token_expiring');
|
||||
}
|
||||
|
||||
public function toMail(): MailMessage
|
||||
{
|
||||
$mail = new MailMessage;
|
||||
$mail->subject("Coolify: API token '{$this->tokenName}' expires in 24 hours");
|
||||
$mail->view('emails.api-token-expiring', [
|
||||
'tokenName' => $this->tokenName,
|
||||
'expiresAt' => $this->expiresAt,
|
||||
'manageUrl' => $this->manageUrl,
|
||||
]);
|
||||
|
||||
return $mail;
|
||||
}
|
||||
|
||||
public function toDiscord(): DiscordMessage
|
||||
{
|
||||
$message = new DiscordMessage(
|
||||
title: '🔑 API token expiring soon',
|
||||
description: "API token **{$this->tokenName}** expires on {$this->expiresAt}.\n\n**Action Required:** Rotate this token before it expires to avoid API outages.",
|
||||
color: DiscordMessage::warningColor(),
|
||||
);
|
||||
|
||||
$message->addField('Manage tokens', "[Open Security settings]({$this->manageUrl})");
|
||||
|
||||
return $message;
|
||||
}
|
||||
|
||||
public function toTelegram(): array
|
||||
{
|
||||
$message = "Coolify: API token '{$this->tokenName}' expires on {$this->expiresAt}.\n\nAction Required: Rotate this token before it expires to avoid API outages.";
|
||||
|
||||
return [
|
||||
'message' => $message,
|
||||
'buttons' => [
|
||||
[
|
||||
'text' => 'Manage API tokens',
|
||||
'url' => $this->manageUrl,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function toPushover(): PushoverMessage
|
||||
{
|
||||
$message = "API token <b>{$this->tokenName}</b> expires on {$this->expiresAt}.<br/><br/>";
|
||||
$message .= '<b>Action Required:</b> Rotate this token before it expires to avoid API outages.';
|
||||
|
||||
return new PushoverMessage(
|
||||
title: 'API token expiring soon',
|
||||
level: 'warning',
|
||||
message: $message,
|
||||
buttons: [
|
||||
[
|
||||
'text' => 'Manage API tokens',
|
||||
'url' => $this->manageUrl,
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function toSlack(): SlackMessage
|
||||
{
|
||||
$description = "API token *{$this->tokenName}* expires on {$this->expiresAt}.\n\n";
|
||||
$description .= "*Action Required:* Rotate this token before it expires to avoid API outages.\n\n";
|
||||
$description .= "Manage tokens: {$this->manageUrl}";
|
||||
|
||||
return new SlackMessage(
|
||||
title: '🔑 API token expiring soon',
|
||||
description: $description,
|
||||
color: SlackMessage::warningColor(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ trait HasNotificationSettings
|
||||
'test',
|
||||
'ssl_certificate_renewal',
|
||||
'hetzner_deletion_failure',
|
||||
'api_token_expiring',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user