I've got a controller action which will work as the callback URL for OAUTH authentication with an external, third-party API. After authenticating, the external API will issue a GET request to my callback URL, which means I need to allow anonymous access for the action method. However, I'd like to use CSRF protection for it.
I've set enableCsrfProtection
to true
in my config/general.php file, and the tokenParam
setting is token
. I've also added protected $allowAnonymous = array('actionAuthCallback');
to my Controller.
Here's how I create the callback URL:
$callbackUrl = UrlHelper::getUrlWithToken(UrlHelper::getSiteUrl('actions/myplugin/mycontroller/authCallback'), craft()->request->getCsrfToken());
The resulting URL looks like this:
https://mysite.com/actions/myplugin/mycontroller/authCallback?token=2423552353djfsjfdsfjsdfsdfrandomrandomawesome
I've validated that the correct action method fires, so all good so far.
Here's the important bits for the action:
public function actionAuthCallback()
{
$this->requireToken();
...
$this->returnJson(array(
'success' => true,
));
}
If I call the action URL without the token
query parameter, stuff works as expected – Craft throws a 400, as it should.
However, if I do include the token
parameter in the URL, Craft throws a 404. What's even more confusing, this happens even if I leave out the $this->requireToken();
call in the actionAuthCallBack()
method – as soon as Craft sees the token
query parameter in the URL, the 404 exception is thrown.
What am I doing wrong? Does CSRF protection require POST requests?