Skip to content

Commit bd1325b

Browse files
SamMousasamdark
authored andcommitted
[Yii2] Added ability to sign cookie and send CSRF cookie #4684
1 parent f2caa8c commit bd1325b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/Codeception/Module/Yii2.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,4 +679,32 @@ private function defineConstants()
679679
defined('YII_ENV') or define('YII_ENV', 'test');
680680
defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER', false);
681681
}
682+
683+
/**
684+
* Sets a cookie and, if validation is enabled, signs it.
685+
* @param string $name The name of the cookie
686+
* @param string $value The value of the cookie
687+
* @param array $params Additional cookie params like `domain`, `path`, `expires` and `secure`.
688+
*/
689+
public function setCookie($name, $val, array $params = [])
690+
{
691+
// Sign the cookie.
692+
if ($this->app->request->enableCookieValidation) {
693+
$val = $this->app->security->hashData(serialize([$name, $val]), $this->app->request->cookieValidationKey);
694+
}
695+
parent::setCookie($name, $val, $params);
696+
}
697+
698+
/**
699+
* This function creates the CSRF Cookie.
700+
* @param string $val The value of the CSRF token
701+
* @return string[] Returns an array containing the name of the CSRF param and the masked CSRF token.
702+
*/
703+
public function createAndSetCsrfCookie($val)
704+
{
705+
$masked = $this->app->security->maskToken($val);
706+
$name = $this->app->request->csrfParam;
707+
$this->setCookie($name, $val);
708+
return [$name, $masked];
709+
}
682710
}

0 commit comments

Comments
 (0)