|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AppBundle\Controller\Api; |
| 6 | + |
| 7 | +use AppBundle\Integration\Rdc\Webhook\RdcIdempotencyChecker; |
| 8 | +use AppBundle\Integration\Rdc\Webhook\WebhookPayloadParser; |
| 9 | +use AppBundle\Message\RdcWebhookMessage; |
| 10 | +use Psr\Log\LoggerInterface; |
| 11 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 12 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 13 | +use Symfony\Component\HttpFoundation\Request; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | +use Symfony\Component\Messenger\MessageBusInterface; |
| 16 | +use Symfony\Component\Routing\Attribute\Route; |
| 17 | + |
| 18 | +class RdcWebhookController extends AbstractController |
| 19 | +{ |
| 20 | + private const WEBHOOK_SOURCE = 'RDC'; |
| 21 | + |
| 22 | + public function __construct( |
| 23 | + private readonly WebhookPayloadParser $payloadParser, |
| 24 | + private readonly RdcIdempotencyChecker $idempotencyChecker, |
| 25 | + private readonly MessageBusInterface $messageBus, |
| 26 | + private readonly LoggerInterface $logger, |
| 27 | + private readonly string $rdcWebhookSecret, |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + #[Route(path: '/api/v1/webhooks/rdc', name: 'api_v1_webhooks_rdc', methods: ['POST'])] |
| 32 | + public function handleWebhook(Request $request): JsonResponse |
| 33 | + { |
| 34 | + // Validate X-webhook-source header |
| 35 | + $source = $request->headers->get('X-webhook-source'); |
| 36 | + if ($source !== self::WEBHOOK_SOURCE) { |
| 37 | + $this->logger->warning('RDC webhook received with invalid source', [ |
| 38 | + 'source' => $source, |
| 39 | + ]); |
| 40 | + return new JsonResponse(['error' => 'Invalid webhook source'], Response::HTTP_FORBIDDEN); |
| 41 | + } |
| 42 | + |
| 43 | + // Validate X-webhook-secret header with timing-attack safe comparison |
| 44 | + $secret = $request->headers->get('X-webhook-Secret'); |
| 45 | + if (!hash_equals($this->rdcWebhookSecret, $secret)) { |
| 46 | + $this->logger->warning('RDC webhook received with invalid secret'); |
| 47 | + return new JsonResponse(['error' => 'Invalid webhook secret'], Response::HTTP_FORBIDDEN); |
| 48 | + } |
| 49 | + |
| 50 | + // Parse JSON payload |
| 51 | + $content = $request->getContent(); |
| 52 | + $payload = json_decode($content, true); |
| 53 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 54 | + $this->logger->warning('RDC webhook received with invalid JSON', [ |
| 55 | + 'json_error' => json_last_error_msg(), |
| 56 | + ]); |
| 57 | + return new JsonResponse(['error' => 'Invalid JSON payload'], Response::HTTP_BAD_REQUEST); |
| 58 | + } |
| 59 | + |
| 60 | + // Parse webhook payload |
| 61 | + $parsed = $this->payloadParser->parse($payload); |
| 62 | + if ($parsed === null) { |
| 63 | + return new JsonResponse(['error' => 'Invalid payload structure'], Response::HTTP_BAD_REQUEST); |
| 64 | + } |
| 65 | + |
| 66 | + $loUri = $parsed['loUri']; |
| 67 | + $eventType = $parsed['eventType']; |
| 68 | + $lo = $parsed['lo']; |
| 69 | + |
| 70 | + // Check idempotency and resolve effective event type |
| 71 | + $effectiveEventType = $this->idempotencyChecker->resolveEventType($loUri, $eventType); |
| 72 | + |
| 73 | + // Mark as processed (will be idempotent due to unique constraint) |
| 74 | + if (!$this->idempotencyChecker->isAlreadyProcessed($loUri)) { |
| 75 | + $this->idempotencyChecker->markAsProcessed($loUri, $effectiveEventType); |
| 76 | + } |
| 77 | + |
| 78 | + // Dispatch message to queue |
| 79 | + $message = new RdcWebhookMessage( |
| 80 | + $loUri, |
| 81 | + $effectiveEventType, |
| 82 | + $lo, |
| 83 | + new \DateTimeImmutable(), |
| 84 | + ); |
| 85 | + $this->messageBus->dispatch($message); |
| 86 | + |
| 87 | + $this->logger->info('RDC webhook accepted', [ |
| 88 | + 'lo_uri' => $loUri, |
| 89 | + 'event_type' => $eventType, |
| 90 | + 'effective_event_type' => $effectiveEventType, |
| 91 | + ]); |
| 92 | + |
| 93 | + return new JsonResponse([ |
| 94 | + 'status' => 'accepted', |
| 95 | + 'lo_uri' => $loUri, |
| 96 | + 'event_type' => $effectiveEventType, |
| 97 | + ]); |
| 98 | + } |
| 99 | +} |
0 commit comments