-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from tbreuss/develop
CSRF protection
- Loading branch information
Showing
3 changed files
with
193 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace tebe\inertia\web; | ||
|
||
use Yii; | ||
|
||
class Request extends \yii\web\Request | ||
{ | ||
const CSRF_HEADER = 'X-XSRF-TOKEN'; | ||
|
||
public $csrfParam = 'XSRF-TOKEN'; | ||
|
||
public $csrfCookie = ['httpOnly' => false]; | ||
|
||
public function init() | ||
{ | ||
$this->parsers['application/json'] = 'yii\web\JsonParser'; | ||
} | ||
|
||
/** | ||
* @return string the CSRF token sent via [[CSRF_HEADER]] by browser. Null is returned if no such header is sent. | ||
*/ | ||
public function getCsrfTokenFromHeader() | ||
{ | ||
$token = $this->headers->get(static::CSRF_HEADER); | ||
|
||
$data = Yii::$app->getSecurity()->validateData($token, $this->cookieValidationKey); | ||
if ($data === false) { | ||
return null; | ||
} | ||
|
||
if (defined('PHP_VERSION_ID') && PHP_VERSION_ID >= 70000) { | ||
$data = @unserialize($data, ['allowed_classes' => false]); | ||
} else { | ||
$data = @unserialize($data); | ||
} | ||
|
||
if (is_array($data) && isset($data[0], $data[1]) && $data[0] === $this->csrfParam) { | ||
return Yii::$app->security->maskToken($data[1]); | ||
} | ||
|
||
return null; | ||
} | ||
} |