Implement webhook inspector
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\WebhookEndpoint;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends Factory<WebhookEndpoint>
|
||||
*/
|
||||
class WebhookEndpointFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
$token = Str::random(48);
|
||||
|
||||
return [
|
||||
'user_id' => null,
|
||||
'name' => 'Test endpoint',
|
||||
'token' => $token,
|
||||
'token_hash' => WebhookEndpoint::tokenHash($token),
|
||||
'is_public' => true,
|
||||
'is_active' => true,
|
||||
'expires_at' => now()->addDays((int) config('webhooks.anonymous_ttl_days', 7)),
|
||||
'response_status' => 200,
|
||||
'response_headers' => ['Content-Type' => 'application/json'],
|
||||
'response_body' => '{}',
|
||||
'last_request_at' => null,
|
||||
];
|
||||
}
|
||||
|
||||
public function ownedBy(User $user): static
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'user_id' => $user->getKey(),
|
||||
'name' => 'Private endpoint',
|
||||
'is_public' => false,
|
||||
'expires_at' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function expired(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'expires_at' => now()->subMinute(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function inactive(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'is_active' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\Models\WebhookEndpoint;
|
||||
use App\Models\WebhookRequest;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends Factory<WebhookRequest>
|
||||
*/
|
||||
class WebhookRequestFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'webhook_endpoint_id' => WebhookEndpoint::factory(),
|
||||
'method' => 'POST',
|
||||
'request_uri' => '/hook/test',
|
||||
'headers' => ['content-type' => ['application/json']],
|
||||
'query_parameters' => [],
|
||||
'body' => '{}',
|
||||
'json_payload' => [],
|
||||
'content_type' => 'application/json',
|
||||
'body_size' => 2,
|
||||
'ip_address' => '127.0.0.1',
|
||||
'user_agent' => 'Webhook Inspector Test',
|
||||
'received_at' => now(),
|
||||
];
|
||||
}
|
||||
|
||||
public function forEndpoint(WebhookEndpoint $endpoint): static
|
||||
{
|
||||
return $this->state(fn (array $attributes): array => [
|
||||
'webhook_endpoint_id' => $endpoint->getKey(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?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::create('webhook_endpoints', function (Blueprint $table) {
|
||||
$table->ulid('id')->primary();
|
||||
$table->foreignId('user_id')->nullable()->constrained()->nullOnDelete();
|
||||
$table->string('name')->nullable();
|
||||
$table->text('token');
|
||||
$table->char('token_hash', 64)->unique();
|
||||
$table->boolean('is_public')->default(true);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->timestamp('expires_at')->nullable()->index();
|
||||
$table->unsignedSmallInteger('response_status')->default(200);
|
||||
$table->longText('response_headers')->nullable();
|
||||
$table->longText('response_body')->nullable();
|
||||
$table->timestamp('last_request_at')->nullable()->index();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['user_id', 'created_at']);
|
||||
$table->index(['is_public', 'expires_at']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('webhook_endpoints');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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::create('webhook_requests', function (Blueprint $table) {
|
||||
$table->ulid('id')->primary();
|
||||
$table->foreignUlid('webhook_endpoint_id')
|
||||
->constrained('webhook_endpoints')
|
||||
->cascadeOnDelete();
|
||||
$table->string('method', 10);
|
||||
$table->text('request_uri');
|
||||
$table->longText('headers')->nullable();
|
||||
$table->longText('query_parameters')->nullable();
|
||||
$table->longText('body')->nullable();
|
||||
$table->longText('json_payload')->nullable();
|
||||
$table->string('content_type')->nullable();
|
||||
$table->unsignedInteger('body_size')->default(0);
|
||||
$table->ipAddress('ip_address')->nullable();
|
||||
$table->text('user_agent')->nullable();
|
||||
$table->timestamp('received_at')->index();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['webhook_endpoint_id', 'received_at']);
|
||||
$table->index(['webhook_endpoint_id', 'method', 'received_at']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('webhook_requests');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user