You are here

class ZoomApiWebhooksController in Zoom API 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/ZoomApiWebhooksController.php \Drupal\zoomapi\Controller\ZoomApiWebhooksController

Class ZoomApiWebhooksController.

Hierarchy

Expanded class hierarchy of ZoomApiWebhooksController

File

src/Controller/ZoomApiWebhooksController.php, line 22

Namespace

Drupal\zoomapi\Controller
View source
class ZoomApiWebhooksController extends ControllerBase {

  /**
   * The Immutable Config Object.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The KeyRepositoryInterface.
   *
   * @var \Drupal\key\KeyRepositoryInterface
   */
  protected $keyRepository;

  /**
   * Psr\Log\LoggerInterface definition.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Symfony\Component\HttpFoundation\RequestStack definition.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Symfony\Component\EventDispatcher\EventDispatcherInterface definition.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * Enable or disable debugging.
   *
   * @var bool
   */
  protected $debug = FALSE;

  /**
   * Zoom webhook verification token.
   *
   * @var string
   */
  protected $webhookVerificationToken;

  /**
   * Constructs a new WebhookController object.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   Logger interface.
   * @param \Drupal\key\KeyRepositoryInterface $key_repository
   *   Key repository interface.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory interface.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   Request stack.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   Event dispatcher interface.
   */
  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');
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * Capture the incoming payload.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request object.
   *
   * @return \Symfony\Component\HttpFoundation\JsonResponse
   *   A simple JSON response.
   */
  public function capture(Request $request) {

    // Capture the payload.
    $payload = $request
      ->getContent();

    // Check if the payload is empty.
    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);
    }

    // JSON decode the payload.
    // TODO: We could do additional error checking of the payload.
    $data = Json::decode($payload);

    // Ability to debug the incoming payload.
    if ($this->debug) {
      $this->logger
        ->debug('<pre><code>' . print_r($data, TRUE) . '</code></pre>');
    }

    // Dispatch Event.
    // Allows other modules to respond.
    // Var $data['event'] = Name of webhook event from Zoom.
    // Var $data['payload'] = Payload data from Zoom.
    // Var $request = The complete request from Zoom.
    $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);
  }

  /**
   * Compares local webhook token to incoming.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   AccessResult allowed or forbidden.
   */
  public function authorize() {
    $request = $this->requestStack
      ->getCurrentRequest();

    // Token was not retreieved.
    if (!($zoomToken = $this
      ->getZoomVerificationToken($request))) {
      $this->logger
        ->debug('The Zoom API webhook post could not be verified.');
      return AccessResult::forbidden();
    }

    // Saved token matches incoming from zoom.
    if ($zoomToken === $this->webhookVerificationToken) {
      return AccessResult::allowed();
    }
    $this->logger
      ->debug('The Zoom API webhook post could not be verified.');
    return AccessResult::forbidden();
  }

  /**
   * Gets the Zoom authorize header from the incoming request.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The incoming request.
   *
   * @return mixed
   *   FALSE or the authorization header.
   */
  protected function getZoomVerificationToken(Request $request) {

    // Check for the authorization header provided by Zoom.
    if (!$request->headers
      ->has('authorization')) {
      return FALSE;
    }
    return $request->headers
      ->get('authorization');
  }

  /**
   * Return a KeyValue.
   *
   * @param string $whichConfig
   *   Name of the config in which the key name is stored.
   *
   * @return mixed
   *   Null or string.
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
ZoomApiWebhooksController::$config protected property The Immutable Config Object.
ZoomApiWebhooksController::$debug protected property Enable or disable debugging.
ZoomApiWebhooksController::$eventDispatcher protected property Symfony\Component\EventDispatcher\EventDispatcherInterface definition.
ZoomApiWebhooksController::$keyRepository protected property The KeyRepositoryInterface.
ZoomApiWebhooksController::$logger protected property Psr\Log\LoggerInterface definition.
ZoomApiWebhooksController::$requestStack protected property Symfony\Component\HttpFoundation\RequestStack definition.
ZoomApiWebhooksController::$webhookVerificationToken protected property Zoom webhook verification token.
ZoomApiWebhooksController::authorize public function Compares local webhook token to incoming.
ZoomApiWebhooksController::capture public function Capture the incoming payload.
ZoomApiWebhooksController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
ZoomApiWebhooksController::getKeyValue protected function Return a KeyValue.
ZoomApiWebhooksController::getZoomVerificationToken protected function Gets the Zoom authorize header from the incoming request.
ZoomApiWebhooksController::__construct public function Constructs a new WebhookController object.