-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Laravel api route and Invalid CSRF Token #115
Comments
+1 I had this same issue with non API routes (where sessions were being utilised..) Commenting out the CSRF checks solves the issue but when deploying and running composer install your changes will be wiped out. My temporary solution was to manually set the state using SessionPersistentDataStore class: From your callback code:
|
I ended up creating a SessionPersistantDataStore class
and then I use it by setting the config:
|
You can use your own persistent data store as documented in the Advanced Configuration section. Also, see @ryross's sample implementation. |
I am facing the same problem described in issue #85 , using Laravel 5.4 and passing an endpoint in my API (routes/api.php) as redirect_uri to dropbox.
After digging some possible causes to this problem, I have identified that line 178 of /Dropbox/Authentication/DropboxAuthHelper.php is likely to be the issue:
$tokenInStore = $this->getPersistentDataStore()->get('state');
As it seems, $tokenInStore relies in a variable stored in session, as @kunalvarma05 mentioned above the need to include
session_start()
on top of the script. However, Laravel's api routes do not use sessions (it also seems to be bad practice to do so) and I've tried to create a custom middleware in my api route so as to enable the use of sessions, such as:Route::group(['middleware'=>'sessions'], function () { //precisa de session Route::get('dropbox/generatetoken', 'DropboxController@generateAccessToken'); });
Additionally, I had to create a middleware in Http/Middleware/Kernel.php:
protected $middlewareGroups = [ 'web' => [ ... ], 'api' => [ ... ], 'sessions' => [ \Illuminate\Session\Middleware\StartSession::class, ] ];
I was able to access the general $_SESSION variable this way, but the error remains. Logging $tokenInStore also shows that the variable is empty.
I was able, however, to get the access token string after commenting the following lines in the validateCSRFToken function of DropboxAuthHelper.php as even mentioned in issue #105
//Unable to fetch CSRF Token if (!$tokenInStore || !$csrfToken) { throw new DropboxClientException("Invalid CSRF Token. Unable to validate CSRF Token."); } //CSRF Token Mismatch if ($tokenInStore !== $csrfToken) { throw new DropboxClientException("Invalid CSRF Token. CSRF Token Mismatch."); }
Obviously, this approach appears to be incorrect as CSRF validation is not being made. I wonder if there are other Laravel users who could put together this authentication using api.php or if there is any other solution.
The text was updated successfully, but these errors were encountered: