ZoomApiWebhooksController.php in Zoom API 8
File
src/Controller/ZoomApiWebhooksController.php
View source
<?php
namespace Drupal\zoomapi\Controller;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\key\KeyRepositoryInterface;
use Drupal\zoomapi\Event\ZoomApiEvents;
use Drupal\zoomapi\Event\ZoomApiWebhookEvent;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
class ZoomApiWebhooksController extends ControllerBase {
protected $config;
protected $keyRepository;
protected $logger;
protected $requestStack;
protected $eventDispatcher;
protected $debug = FALSE;
protected $webhookVerificationToken;
public function __construct(LoggerInterface $logger, KeyRepositoryInterface $key_repository, ConfigFactoryInterface $config_factory, RequestStack $request_stack, EventDispatcherInterface $event_dispatcher) {
$this->logger = $logger;
$this->requestStack = $request_stack;
$this->eventDispatcher = $event_dispatcher;
$this->keyRepository = $key_repository;
$this->config = $config_factory
->get('zoomapi.settings');
$this->webhookVerificationToken = $this
->getKeyValue('webhook_verification_token');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('logger.channel.zoomapi'), $container
->get('key.repository'), $container
->get('config.factory'), $container
->get('request_stack'), $container
->get('event_dispatcher'));
}
public function capture(Request $request) {
$payload = $request
->getContent();
if (empty($payload)) {
$message = 'The Zoom webhook payload was missing.';
$this->logger
->notice($message);
$response = [
'success' => FALSE,
'message' => $message,
'data' => [],
];
return new JsonResponse($response, 400);
}
$data = Json::decode($payload);
if ($this->debug) {
$this->logger
->debug('<pre><code>' . print_r($data, TRUE) . '</code></pre>');
}
$dispatch = new ZoomApiWebhookEvent($data['event'], $data['payload'], $request);
$this->eventDispatcher
->dispatch(ZoomApiEvents::WEBHOOK_POST, $dispatch);
$response = [
'success' => TRUE,
'message' => 'Webhook payload captured!',
'data' => [],
];
return new JsonResponse($response);
}
public function authorize() {
$request = $this->requestStack
->getCurrentRequest();
if (!($zoomToken = $this
->getZoomVerificationToken($request))) {
$this->logger
->debug('The Zoom API webhook post could not be verified.');
return AccessResult::forbidden();
}
if ($zoomToken === $this->webhookVerificationToken) {
return AccessResult::allowed();
}
$this->logger
->debug('The Zoom API webhook post could not be verified.');
return AccessResult::forbidden();
}
protected function getZoomVerificationToken(Request $request) {
if (!$request->headers
->has('authorization')) {
return FALSE;
}
return $request->headers
->get('authorization');
}
protected function getKeyValue($whichConfig) {
if (empty($this->config
->get($whichConfig))) {
return NULL;
}
$whichKey = $this->config
->get($whichConfig);
$keyValue = $this->keyRepository
->getKey($whichKey)
->getKeyValue();
if (empty($keyValue)) {
return NULL;
}
return $keyValue;
}
}