- First commit to issue fork.
- Assigned to Panchuk
- Merge request !23Issue #3175875: Created submodule to trigger Event upon receiving Mailgun Webhook. → (Open) created by Panchuk
- last update
about 1 year ago 5 pass - Issue was unassigned.
- Status changed to Needs review
about 1 year ago 2:08pm 5 November 2023 - 🇺🇦Ukraine Panchuk Volyn, Lutsk
I've created a submodule to trigger the event. I think there are a few things that must be fixed before merging.
1) Autotests
It will be nice to cover some bases with tests.2) Routing URL
I'm not sure if it is okay to keep static URL in route.3) Sample subscriber for debugging
I've committed the subscriber, probably it useless, and better to remove it. - 🇬🇧United Kingdom altcom_neil
@Panchuk
Good work on this.For number 2 raised above you can use a dynamic path. So change the routing to:
# Endpoint for the webhooks. mailgun_webhooks.endpoint: path: '/api/mailgun/{webhook_key}' defaults: _title: 'Mailgun Webhook' _controller: '\Drupal\mailgun_webhooks\Controller\MailgunWebhookController::processWebhook' methods: [POST] requirements: # Allow the endpoint to be accessed without authentication. # This is required for Mailgun to be able to access the endpoint. _access: 'TRUE' _format: 'json'
And then the
\Drupal\mailgun_webhooks\Controller\MailgunWebhookController::processWebhook
is changed to expect a $webhook_key variable and compare it with a config var so it can be set to a unique value in every site.public function processWebhook($webhook_key): Response { // If the webhook_key value provided in the URL does not match the one in // the current config throw a 401: Unauthorized error. if ($webhook_key !== $this->config->get('webhook_key')) { $response = new Response('Unauthorized: Webhook key does not match.'); $response->setStatusCode('401'); return $response; }
I added a hook_install to generate the URL when the module is installed:
function mailgun_webhooks_install() { // On install generate a random string to use in the callback URL. $random = new Random(); $webhook_key = $random->name(48, TRUE); $config = \Drupal::configFactory()->getEditable('mailgun_webhooks.settings'); $config->set('webhook_key', $webhook_key); $config->save(); }
If someone tries to use the wrong webhook_key you get the 401 error from MailgunWebhookController and if you are missing the webhook_key entirely (using path /api/mailgun) then you just get the standard Drupal 404 page.
Cheers, Neil
- 🇬🇧United Kingdom altcom_neil
In \Drupal\mailgun_webhooks\Controller\MailgunWebhookController the old RequestStack use needs to be replaced with the symfony version.
- use Drupal\Core\Http\RequestStack; use Drupal\Component\Serialization\Json; use Drupal\Core\Controller\ControllerBase; use Symfony\Component\HttpFoundation\Response; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\mailgun_webhooks\Event\MailgunWebhookEvent; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; + use Symfony\Component\HttpFoundation\RequestStack; use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
- 🇺🇦Ukraine Panchuk Volyn, Lutsk
Thanks for review @Altcom_Neil
Do you want to commit these improvements/fixes? If not I'll prepare the update for MR.
- 🇺🇦Ukraine Panchuk Volyn, Lutsk
Added code suggested by @altcom_neil and fixed a few issues.
Next time going to add some tests, but for now it's enough.