I'm building a webshop based on the slim3 framework. I need to handle a server-to-server POST-request to confirm if payment was successful. I added csrf to the container like this:
$container['csrf'] = function($container) {
return new \Slim\Csrf\Guard;
};
and added it to the app like this:
$app->add($container->csrf);
And it works good. But now i need to be able to add an exception to a certain route so i get the post reques they are sending. I'couldn't find a working solution so far.
Any advice?
Not if anyone is still pulling their hair out about this (especially if you want to use webhooks).
I found a simpler solution with the help of Georgy's answer.
Just make the following modification to the actual Slim\Csrf\Guard 'Guard.php' file and its __invoke method. Or just copy and paste the code below...
If you need to exclude one route from a middleware, there are two options:
Option 1: group your routes.
You can group all routes except the one:
Option 2: use your own middleware
Instead of using
\Slim\Csrf\Guard
directly, use your own middleware that extends it. Your middleware will check the route, and if route is the "exceptional" one, it will skip.Add this to settings since you need to access route within middleware:
Create the middleware extending orginial
\Slim\Csrf\Guard
:Now simply add the middleware to
\Slim\App
instance: