class ZoomApiWebhooksController in Zoom API 2.0.x
Same name and namespace in other branches
- 8 src/Controller/ZoomApiWebhooksController.php \Drupal\zoomapi\Controller\ZoomApiWebhooksController
Class ZoomApiWebhooksController.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\zoomapi\Controller\ZoomApiWebhooksController
Expanded class hierarchy of ZoomApiWebhooksController
File
- src/
Controller/ ZoomApiWebhooksController.php, line 22
Namespace
Drupal\zoomapi\ControllerView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ZoomApiWebhooksController:: |
protected | property | The Immutable Config Object. | |
ZoomApiWebhooksController:: |
protected | property | Enable or disable debugging. | |
ZoomApiWebhooksController:: |
protected | property | Symfony\Component\EventDispatcher\EventDispatcherInterface definition. | |
ZoomApiWebhooksController:: |
protected | property | The KeyRepositoryInterface. | |
ZoomApiWebhooksController:: |
protected | property | Psr\Log\LoggerInterface definition. | |
ZoomApiWebhooksController:: |
protected | property | Symfony\Component\HttpFoundation\RequestStack definition. | |
ZoomApiWebhooksController:: |
protected | property | Zoom webhook verification token. | |
ZoomApiWebhooksController:: |
public | function | Compares local webhook token to incoming. | |
ZoomApiWebhooksController:: |
public | function | Capture the incoming payload. | |
ZoomApiWebhooksController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
ZoomApiWebhooksController:: |
protected | function | Return a KeyValue. | |
ZoomApiWebhooksController:: |
protected | function | Gets the Zoom authorize header from the incoming request. | |
ZoomApiWebhooksController:: |
public | function | Constructs a new WebhookController object. |